123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::InheritableOptions

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveSupport::OrderedOptions
Defined in: activesupport/lib/active_support/ordered_options.rb

Overview

Inheritable Options

InheritableOptions provides a constructor to build an OrderedOptions hash inherited from another hash.

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'

If the existing hash has string keys, call Hash#symbolize_keys on it.

h = ActiveSupport::InheritableOptions.new({ 'girl' => 'Mary', 'boy' => 'John' }.symbolize_keys)
h.girl # => 'Mary'
h.boy  # => 'John'

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

OrderedOptions - Inherited

::Hash - Inherited

#extractable_options?

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

#present?

Instance Method Summary

OrderedOptions - Inherited

#[], #[]=, #dig, #inspect, #method_missing, #respond_to_missing?,
#_get

preserve the original #[] method.

::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:

#_deep_transform_keys_in_object

Support methods for deep transforming nested hashes and arrays.

#_deep_transform_keys_in_object!,
#_deep_transform_values_in_object

Support methods for deep transforming nested hashes and arrays.

#_deep_transform_values_in_object!, #as_json,
#compact_blank

Hash#reject has its own definition, so this needs one too.

#deep_merge?

DeepMergeable - Included

#deep_merge

Returns a new instance with the values from other merged recursively.

#deep_merge!

Same as #deep_merge, but modifies self.

#deep_merge?

Returns true if other can be deep merged into self.

Constructor Details

.new(parent = nil) ⇒ InheritableOptions

[ GitHub ]

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

def initialize(parent = nil)
  @parent = parent
  if @parent.kind_of?(OrderedOptions)
    # use the faster _get when dealing with OrderedOptions
    super() { |h, k| @parent._get(k) }
  elsif @parent
    super() { |h, k| @parent[k] }
  else
    super()
    @parent = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveSupport::OrderedOptions

Instance Method Details

#==(other)

[ GitHub ]

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

def ==(other)
  to_h == other.to_h
end

#each(&block)

[ GitHub ]

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

def each(&block)
  to_h.each(&block)
  self
end

#inheritable_copy

[ GitHub ]

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

def inheritable_copy
  self.class.new(self)
end

#inspect

[ GitHub ]

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

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

#key?(key) ⇒ Boolean

[ GitHub ]

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

def key?(key)
  super || @parent.key?(key)
end

#overridden?(key) ⇒ Boolean

[ GitHub ]

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

def overridden?(key)
  !!(@parent && @parent.key?(key) && own_key?(key.to_sym))
end

#own_key? (private)

[ GitHub ]

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

alias_method :own_key?, :key?

#pretty_print(pp)

[ GitHub ]

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

def pretty_print(pp)
  pp.pp_hash(to_h)
end

#to_a

[ GitHub ]

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

def to_a
  entries
end

#to_h

[ GitHub ]

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

def to_h
  @parent.merge(self)
end

#to_s

[ GitHub ]

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

def to_s
  to_h.to_s
end