Class: Metadata
- Includes:
- MetaInstance
- Defined in:
- lib/metahash/metadata.rb
Overview
MetadataHash - A specific use of ruby’s Hash
overrides Hash’s method missing, providing the following functionality:
-
Access Nested hashes using the method / attribute syntax
i.e.: h = {}
h.middle.inner == {}
-
Access to values stored in nested hashes via method call syntax
i.e.: h = { middle: { inner: { key: "value" } } }
h.middle.inner.key == "value"
-
Set values for nested hash structures without middle nested hashes
having to be defined
i.e.: h = {}
h.middle.inner = 3
h == { middle: { inner: 3 } }
-
Old hash square bracket access still works
i.e.: h = { inner: { key: "value" } }
h[:inner][:key] == "value"
Constant Summary collapse
- METHOD_BACKUP_KEY =
in the event we are overriding a method, have a way to get back to the original
"metadata_original_"
Instance Method Summary collapse
-
#[](key) ⇒ Object
Metdata has indifferent access.
-
#[]=(key, value) ⇒ Object
# Metadata has indifferent access, # so just say that all the keys are symbols.
-
#initialize(hash = {}) ⇒ Metadata
constructor
the hash being passed in will have all its subhashes converted to metadata hashes.
-
#key_not_in_use?(key) ⇒ Boolean
tests the ability to use this key as a key in a hash.
-
#method_missing(method_name, *args) ⇒ Object
this is what allows functionality mentioned in the class comment to happen.
- #to_ary ⇒ Object (also: #to_a)
-
#to_hash ⇒ Object
convert to regular hash, recursively.
Methods inherited from Hash
Constructor Details
#initialize(hash = {}) ⇒ Metadata
the hash being passed in will have all its subhashes converted to metadata hashes. this is needed to we can have the
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/metahash/metadata.rb', line 35 def initialize(hash = {}) # for maybe instantiating nested hashes that we # aren't yet sure if they are going to have values or not @empty_nested_hashes = [] if hash.is_a?(Metadata) # we have nothing to do return hash elsif hash.is_a?(Hash) # recursively create nested metadata objects hash.each do |key, value| self[ key ] = ( if value.is_a?(Hash) Metadata.new(value) elsif value.is_a?(Array) # ensure hashes kept in an array are also converted to metadata array = value.map{ |element| element.is_a?(Hash) ? Metadata.new(element) : element } else value end ) end else raise ArgumentError.new("Field must be a Hash or Metadata") end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
this is what allows functionality mentioned in the class comment to happen
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/metahash/metadata.rb', line 68 def method_missing(method_name, *args) # check for assignment if method_name.to_s[-1] == "=" assign_value(method_name, args[0]) else value = self[method_name] if value.nil? @empty_nested_hashes << method_name value = self end value end end |
Instance Method Details
#[](key) ⇒ Object
Metdata has indifferent access
84 85 86 87 |
# File 'lib/metahash/metadata.rb', line 84 def [](key) # self.send(key) super(key.to_sym) end |
#[]=(key, value) ⇒ Object
# Metadata has indifferent access, # so just say that all the keys are symbols.
91 92 93 94 95 96 |
# File 'lib/metahash/metadata.rb', line 91 def []=(key, value) if value.is_a?(Hash) && !value.is_a?(Metadata) value = Metadata.new(value) end super(key.to_sym, value) end |
#key_not_in_use?(key) ⇒ Boolean
tests the ability to use this key as a key in a hash
101 102 103 |
# File 'lib/metahash/metadata.rb', line 101 def key_not_in_use?(key) not self.respond_to?(key) end |
#to_ary ⇒ Object Also known as: to_a
123 124 125 |
# File 'lib/metahash/metadata.rb', line 123 def to_ary self.to_hash.to_a end |
#to_hash ⇒ Object
convert to regular hash, recursively
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/metahash/metadata.rb', line 106 def to_hash hash = {} self.each do |k,v| hash[k] = ( if v.is_a?(Metadata) v.to_hash elsif v.is_a?(Array) v.map{ |e| e.is_a?(Metadata) ? e.to_hash : e } else v end ) end hash end |