class ActiveSupport::OrderedOptions
Ordered Options¶ ↑
OrderedOptions
inherits from Hash
and provides dynamic accessor methods.
With a Hash
, key-value pairs are typically managed like this:
h = {} h[:boy] = 'John' h[:girl] = 'Mary' h[:boy] # => 'John' h[:girl] # => 'Mary' h[:dog] # => nil
Using OrderedOptions
, the above code can be written as:
h = ActiveSupport::OrderedOptions.new h.boy = 'John' h.girl = 'Mary' h.boy # => 'John' h.girl # => 'Mary' h.dog # => nil
To raise an exception when the value is blank, append a bang to the key name, like:
h.dog! # => raises KeyError: :dog is blank
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/active_support/ordered_options.rb, line 41 def [](key) super(key.to_sym) end
Also aliased as: _get
[]=(key, value)
click to toggle source
Calls superclass method
# File lib/active_support/ordered_options.rb, line 37 def []=(key, value) super(key.to_sym, value) end
dig(key, *identifiers)
click to toggle source
Calls superclass method
# File lib/active_support/ordered_options.rb, line 45 def dig(key, *identifiers) super(key.to_sym, *identifiers) end
extractable_options?()
click to toggle source
# File lib/active_support/ordered_options.rb, line 68 def extractable_options? true end
inspect()
click to toggle source
# File lib/active_support/ordered_options.rb, line 72 def inspect "#<#{self.class.name} #{super}>" end
method_missing(name, *args)
click to toggle source
# File lib/active_support/ordered_options.rb, line 49 def method_missing(name, *args) name_string = +name.to_s if name_string.chomp!("=") self[name_string] = args.first else bangs = name_string.chomp!("!") if bangs self[name_string].presence || raise(KeyError.new(":#{name_string} is blank")) else self[name_string] end end end
respond_to_missing?(name, include_private)
click to toggle source
# File lib/active_support/ordered_options.rb, line 64 def respond_to_missing?(name, include_private) true end