123456789_123456789_123456789_123456789_123456789_

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

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

Class Method Summary

::Hash - Inherited

.from_trusted_xml

Builds a ::Hash from XML just like Hash.from_xml, but also allows ::Symbol and YAML.

.from_xml

Returns a ::Hash containing a collection of pairs when the key is the node name and the value is its content.

Instance Attribute Summary

::Hash - Inherited

#extractable_options?

By default, only instances of ::Hash itself are extractable.

Instance Method Summary

::Hash - Inherited

#assert_valid_keys

Validates all keys in a hash match *valid_keys, raising ArgumentError on a mismatch.

#blank?

A hash is blank if it’s empty:

#compact_blank!

Removes all blank values from the ::Hash in place and returns self.

#deep_dup

Returns a deep copy of hash.

#deep_merge

Returns a new hash with self and other_hash merged recursively.

#deep_merge!

Same as #deep_merge, but modifies self.

#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 to_sym.

#deep_symbolize_keys!

Destructively converts all keys to symbols, as long as they respond to to_sym.

#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.

#deep_transform_values

Returns a new hash with all values converted by the block operation.

#deep_transform_values!

Destructively converts all values by using the block operation.

#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
#reverse_merge

Merges the caller into other_hash.

#reverse_merge!

Destructive reverse_merge.

#reverse_update
#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 to_sym.

#symbolize_keys!

Destructively converts all keys to symbols, as long as they respond to to_sym.

#to_options
#to_options!
#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:

#with_defaults
#with_defaults!
#with_indifferent_access

Returns an HashWithIndifferentAccess out of its receiver:

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args)

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 49

def method_missing(method, *args)
  if method.end_with?("=")
    self[method.name.chomp("=")] = args.first
  elsif method.end_with?("!")
    name_string = method.name.chomp("!")
    self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
  else
    self[method.name]
  end
end

Instance Attribute Details

#extractable_options?Boolean (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 64

def extractable_options?
  true
end

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 41

def [](key)
  super(key.to_sym)
end

#[]=(key, value)

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 37

def []=(key, value)
  super(key.to_sym, value)
end

#dig(key, *identifiers)

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 45

def dig(key, *identifiers)
  super(key.to_sym, *identifiers)
end

#inspect

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 68

def inspect
  "#<#{self.class.name} #{super}>"
end

#respond_to_missing?(name, include_private) ⇒ Boolean

[ GitHub ]

  
# File 'activesupport/lib/active_support/ordered_options.rb', line 60

def respond_to_missing?(name, include_private)
  true
end