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
26 27 28 29 30 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 26 def ==(other) other.is_a?(Hash) && ___all_keys_equal(other) || (other.is_a?(LooseStruct) || other.is_a?(FirmStruct)) && ___all_keys_equal(other.instance_variable_get(:@hash)) end |
#dig(key, *sub_keys) ⇒ Object Also known as: []
32 33 34 35 36 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 32 def dig(key, *sub_keys) ckey = ___convert_key(key) result = if @ro_struct.key?(ckey) @ro_struct[ckey] elsif @hash.key?(key) @ro_struct[ckey] = ___convert_value(@hash[key]) elsif key.is_a?(String) && @hash.key?(key.to_sym) @ro_struct[ckey] = ___convert_value(@hash[key.to_sym]) elsif key.is_a?(Symbol) && @hash.key?(key.to_s) @ro_struct[ckey] = ___convert_value(@hash[key.to_s]) 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 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 15 def initialize(hash = {}) @hash = hash || {} @ro_struct = {} raise ::ArgumentError, "first argument must be a Hash, found #{@hash.class.name}" unless @hash.is_a?(Hash) end |
#inspect ⇒ Object
22 23 24 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 22 def inspect "#{self.class.name}<#{@hash.inspect}>" end |
#marshal_dump ⇒ Object
56 57 58 59 60 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 56 def marshal_dump { data: @hash } end |
#marshal_load(obj) ⇒ Object
62 63 64 65 |
# File 'lib/resource_struct/extensions/indifferent_lookup.rb', line 62 def marshal_load(obj) @ro_struct = {} @hash = obj[:data] end |