Class: ActiveSupport::OrderedOptions
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Hash
|
|
Instance Chain:
self,
::Hash
|
|
Inherits: | Hash |
Defined in: | activesupport/lib/active_support/ordered_options.rb |
Overview
Usually key value pairs are handled something like this:
h = {}
h[:boy] = 'John'
h[:girl] = 'Mary'
h[:boy] # => 'John'
h[:girl] # => 'Mary'
h[:dog] # => nil
Using OrderedOptions
, the above code could be reduced to:
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
Class Method Summary
::Hash
- Inherited
.from_trusted_xml | Builds a |
.from_xml | Returns a |
Instance Attribute Summary
::Hash
- Inherited
#extractable_options? | By default, only instances of |
Instance Method Summary
- #[](key)
- #[]=(key, value)
- #method_missing(name, *args)
- #respond_to_missing?(name, include_private) ⇒ Boolean
::Hash
- Inherited
#assert_valid_keys | Validates all keys in a hash match |
#blank? | A hash is blank if it’s empty: |
#compact | Returns a hash with non |
#compact! | Replaces current hash with non |
#deep_dup | Returns a deep copy of hash. |
#deep_merge | Returns a new hash with |
#deep_merge! | Same as |
#deep_stringify_keys | Returns a new hash with all keys converted to strings. |
#deep_stringify_keys! | Destructively converts all keys to strings. |
#deep_symbolize_keys | Returns a new hash with all keys converted to symbols, as long as they respond to |
#deep_symbolize_keys! | Destructively converts all keys to symbols, as long as they respond to |
#deep_transform_keys | Returns a new hash with all keys converted by the block operation. |
#deep_transform_keys! | Destructively converts all keys by using the block operation. |
#except | Returns a hash that includes everything except given keys. |
#except! | Removes the given keys from hash and returns it. |
#extract! | Removes and returns the key/value pairs matching the given keys. |
#nested_under_indifferent_access | Alias for Hash#with_indifferent_access. |
#reverse_merge | Merges the caller into |
#reverse_merge! | Destructive |
#reverse_update | Alias for Hash#reverse_merge!. |
#slice | Slices a hash to include only the given keys. |
#slice! | Replaces the hash with only the given keys. |
#stringify_keys | Returns a new hash with all keys converted to strings. |
#stringify_keys! | Destructively converts all keys to strings. |
#symbolize_keys | Returns a new hash with all keys converted to symbols, as long as they respond to |
#symbolize_keys! | Destructively converts all keys to symbols, as long as they respond to |
#to_options | Alias for Hash#symbolize_keys. |
#to_options! | Alias for Hash#symbolize_keys!. |
#to_param | Alias for Hash#to_query. |
#to_query | Returns a string representation of the receiver suitable for use as a URL query string: |
#to_xml | Returns a string containing an XML representation of its receiver: |
#transform_keys | Returns a new hash with all keys converted using the |
#transform_keys! | Destructively converts all keys using the |
#transform_values | Returns a new hash with the results of running |
#transform_values! | Destructively converts all values using the |
#with_defaults | Alias for Hash#reverse_merge. |
#with_defaults! | Alias for Hash#reverse_merge!. |
#with_indifferent_access | Returns an |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args)
[ GitHub ]# File 'activesupport/lib/active_support/ordered_options.rb', line 41
def method_missing(name, *args) name_string = name.to_s.dup 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
Instance Method Details
#[](key)
[ GitHub ]# File 'activesupport/lib/active_support/ordered_options.rb', line 37
def [](key) super(key.to_sym) end
#[]=(key, value)
[ GitHub ]# File 'activesupport/lib/active_support/ordered_options.rb', line 33
def []=(key, value) super(key.to_sym, value) end
#respond_to_missing?(name, include_private) ⇒ Boolean
# File 'activesupport/lib/active_support/ordered_options.rb', line 56
def respond_to_missing?(name, include_private) true end