2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/object/blank"
|
2016-06-13 13:00:43 -04:00
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
module ActiveSupport
|
2020-06-30 11:37:47 -04:00
|
|
|
# +OrderedOptions+ inherits from +Hash+ and provides dynamic accessor methods.
|
|
|
|
#
|
|
|
|
# With a +Hash+, key-value pairs are typically managed like this:
|
2012-09-17 01:12:11 -04:00
|
|
|
#
|
|
|
|
# h = {}
|
|
|
|
# h[:boy] = 'John'
|
|
|
|
# h[:girl] = 'Mary'
|
|
|
|
# h[:boy] # => 'John'
|
|
|
|
# h[:girl] # => 'Mary'
|
2015-07-04 15:21:54 -04:00
|
|
|
# h[:dog] # => nil
|
2012-09-17 01:12:11 -04:00
|
|
|
#
|
2020-06-30 11:37:47 -04:00
|
|
|
# Using +OrderedOptions+, the above code can be written as:
|
2012-09-17 01:12:11 -04:00
|
|
|
#
|
|
|
|
# h = ActiveSupport::OrderedOptions.new
|
|
|
|
# h.boy = 'John'
|
|
|
|
# h.girl = 'Mary'
|
|
|
|
# h.boy # => 'John'
|
|
|
|
# h.girl # => 'Mary'
|
2015-07-04 15:21:54 -04:00
|
|
|
# h.dog # => nil
|
|
|
|
#
|
|
|
|
# To raise an exception when the value is blank, append a
|
|
|
|
# bang to the key name, like:
|
|
|
|
#
|
2017-09-20 03:38:00 -04:00
|
|
|
# h.dog! # => raises KeyError: :dog is blank
|
2015-07-04 15:21:54 -04:00
|
|
|
#
|
2012-02-21 12:38:36 -05:00
|
|
|
class OrderedOptions < Hash
|
2010-09-27 08:48:06 -04:00
|
|
|
alias_method :_get, :[] # preserve the original #[] method
|
|
|
|
protected :_get # make it protected
|
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def []=(key, value)
|
|
|
|
super(key.to_sym, value)
|
|
|
|
end
|
2006-05-31 18:43:53 -04:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def [](key)
|
|
|
|
super(key.to_sym)
|
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
def method_missing(name, *args)
|
2019-09-26 04:24:17 -04:00
|
|
|
name_string = +name.to_s
|
2016-08-06 11:58:50 -04:00
|
|
|
if name_string.chomp!("=")
|
2012-01-21 05:15:08 -05:00
|
|
|
self[name_string] = args.first
|
2008-06-03 14:32:53 -04:00
|
|
|
else
|
2016-08-06 11:58:50 -04:00
|
|
|
bangs = name_string.chomp!("!")
|
2015-05-26 12:21:28 -04:00
|
|
|
|
2015-05-17 06:51:34 -04:00
|
|
|
if bangs
|
2017-09-23 05:25:16 -04:00
|
|
|
self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
|
2015-05-17 06:51:34 -04:00
|
|
|
else
|
2015-05-26 12:21:28 -04:00
|
|
|
self[name_string]
|
2015-05-27 13:56:33 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2011-06-14 01:45:34 -04:00
|
|
|
|
2012-05-05 02:24:57 -04:00
|
|
|
def respond_to_missing?(name, include_private)
|
2011-06-14 01:45:34 -04:00
|
|
|
true
|
|
|
|
end
|
2019-11-08 17:32:55 -05:00
|
|
|
|
|
|
|
def extractable_options?
|
|
|
|
true
|
|
|
|
end
|
2020-08-22 15:08:55 -04:00
|
|
|
|
|
|
|
def inspect
|
2020-08-26 13:44:44 -04:00
|
|
|
"#<#{self.class.name} #{super}>"
|
2020-08-22 15:08:55 -04:00
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2010-03-02 20:18:01 -05:00
|
|
|
|
2013-05-11 05:17:34 -04:00
|
|
|
# +InheritableOptions+ provides a constructor to build an +OrderedOptions+
|
2013-08-17 07:42:15 -04:00
|
|
|
# hash inherited from another hash.
|
2013-05-11 05:17:34 -04:00
|
|
|
#
|
|
|
|
# Use this if you already have some hash and you want to create a new one based on it.
|
|
|
|
#
|
|
|
|
# h = ActiveSupport::InheritableOptions.new({ girl: 'Mary', boy: 'John' })
|
|
|
|
# h.girl # => 'Mary'
|
|
|
|
# h.boy # => 'John'
|
2010-03-02 20:18:01 -05:00
|
|
|
class InheritableOptions < OrderedOptions
|
2010-09-27 08:51:31 -04:00
|
|
|
def initialize(parent = nil)
|
2010-09-27 08:48:06 -04:00
|
|
|
if parent.kind_of?(OrderedOptions)
|
|
|
|
# use the faster _get when dealing with OrderedOptions
|
2016-10-28 23:05:58 -04:00
|
|
|
super() { |h, k| parent._get(k) }
|
2010-09-27 08:48:06 -04:00
|
|
|
elsif parent
|
2016-10-28 23:05:58 -04:00
|
|
|
super() { |h, k| parent[k] }
|
2010-09-27 08:51:31 -04:00
|
|
|
else
|
|
|
|
super()
|
|
|
|
end
|
2010-03-02 20:18:01 -05:00
|
|
|
end
|
2010-09-27 08:50:39 -04:00
|
|
|
|
|
|
|
def inheritable_copy
|
|
|
|
self.class.new(self)
|
|
|
|
end
|
2010-03-02 20:18:01 -05:00
|
|
|
end
|
2006-05-31 18:43:53 -04:00
|
|
|
end
|