Module: ResourceStruct::Extensions::IndifferentLookup
- Extended by:
- Forwardable
- Included in:
- FlexStruct, StrictStruct
- Defined in:
- lib/resource_struct/extensions/indifferent_lookup.rb
Overview
Common code between FirmStruct and LooseStruct
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dig(key, *sub_keys) ⇒ Object (also: #[])
- #initialize(hash = {}) ⇒ Object
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(obj) ⇒ Object
Instance Method Details
#==(other) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 27 def ==(other) other_hash = case other when Hash then other when LooseStruct, FirmStruct then other.instance_variable_get(:@hash) else return false end ___all_keys_equal(other_hash) end |
#dig(key, *sub_keys) ⇒ Object Also known as: []
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 37 def dig(key, *sub_keys) ckey = ___convert_key(key) result = if @ro_struct.key?(ckey) @ro_struct[ckey] elsif @hash.key?(ckey) @ro_struct[ckey] = ___convert_value(@hash[ckey]) end return result if sub_keys.empty? return unless result raise TypeError, "#{result.class.name} does not have #dig method" unless result.respond_to?(:dig) result.dig(*sub_keys) end |
#initialize(hash = {}) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 15 def initialize(hash = {}) hash = {} if hash.nil? raise ::ArgumentError, "first argument must be a Hash, found #{hash.class.name}" unless hash.is_a?(Hash) @hash = ___canonicalize_hash(hash) @ro_struct = {} end |
#inspect ⇒ Object
23 24 25 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 23 def inspect "#{self.class.name}<#{@hash.inspect}>" end |
#marshal_dump ⇒ Object
56 57 58 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 56 def marshal_dump { data: @hash } end |
#marshal_load(obj) ⇒ Object
60 61 62 63 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 60 def marshal_load(obj) @ro_struct = {} @hash = ___canonicalize_hash(obj[:data] || {}) end |