Class: Interpol::DynamicStruct
- Inherits:
-
Object
- Object
- Interpol::DynamicStruct
- Includes:
- DefineSingletonMethod
- Defined in:
- lib/interpol/dynamic_struct.rb
Overview
Transforms an arbitrarily deeply nested hash into a dot-syntax object. Useful as an alternative to a hash since it is “strongly typed” in the sense that fat-fingered property names result in a NoMethodError, rather than getting a nil as you would with a hash.
Instance Attribute Summary collapse
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
-
#to_hash ⇒ Object
readonly
Returns the value of attribute to_hash.
Instance Method Summary collapse
-
#initialize(hash) ⇒ DynamicStruct
constructor
A new instance of DynamicStruct.
Methods included from DefineSingletonMethod
Constructor Details
#initialize(hash) ⇒ DynamicStruct
Returns a new instance of DynamicStruct.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/interpol/dynamic_struct.rb', line 11 def initialize(hash) @to_hash = hash @attribute_names = hash.keys.map(&:to_sym) hash.each do |key, value| value = method_value_for(value) define_singleton_method(key) { value } define_singleton_method("#{key}?") { !!value } end end |
Instance Attribute Details
#attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names.
9 10 11 |
# File 'lib/interpol/dynamic_struct.rb', line 9 def attribute_names @attribute_names end |
#to_hash ⇒ Object (readonly)
Returns the value of attribute to_hash.
9 10 11 |
# File 'lib/interpol/dynamic_struct.rb', line 9 def to_hash @to_hash end |