Class: Metadata

Inherits:
Hash
  • Object
show all
Includes:
Metaclass
Defined in:
lib/metahash/metadata.rb

Overview

MetadataHash - A specific use of ruby’s Hash

overrides Hash’s method missing, providing the following functionality:

  1. Access Nested hashes using the method / attribute syntax

i.e.: h = {}
  h.middle.inner == {}
  1. Access to values stored in nested hashes via method call syntax

i.e.: h = { middle: { inner: { key: "value" } } }
  h.middle.inner.key == "value"
  1. 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 } }
  1. 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

Methods included from Metaclass

#class_def, #meta_def, #meta_eval, #metaclass

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

Parameters:

  • hash (Hash) (defaults to: {})

    the structure to convert to Metadata

Raises:

  • (ArgumentError)

    if one of the keys is method of Hash

  • (ArgumentError)

    if hash is not a type of Hash or Metadata



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

Raises:

  • (ArgumentError)

    if one of the keys is method of Hash



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/metahash/metadata.rb', line 68

def method_missing(method_name, *args)
  # check for assignment
  if (key = method_name.to_s).include?("=")
    key = key.chop.to_sym

     = self
    if not @empty_nested_hashes.empty?
      @empty_nested_hashes.each do |key|
         = [key] = Metadata.new
      end
      @empty_nested_hashes = []
      [key] = args[0]
      # override any existing method with the key
      .meta_def(key){ self[key]}
    else
      self[key] = args[0]
      # override any existing method with the key
      self.meta_def(key){ args[0] }
    end
  else
    value = self[method_name]
    if not value
      @empty_nested_hashes << method_name.to_sym
      value = self
    end
    value
  end

end

Instance Method Details

#[](key) ⇒ Object

Metdata has indifferent access



99
100
101
# File 'lib/metahash/metadata.rb', line 99

def [](key)
  super(key.to_sym)
end

#[]=(key, value) ⇒ Object

# Metadata has indifferent access, # so just say that all the keys are symbols.



105
106
107
# File 'lib/metahash/metadata.rb', line 105

def []=(key, value)
  super(key.to_sym, value)
end

#to_aryObject



134
135
136
# File 'lib/metahash/metadata.rb', line 134

def to_ary
  self.to_hash.to_a
end

#to_hashObject

convert to regular hash, recursively



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/metahash/metadata.rb', line 117

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

#valid_key?(key) ⇒ Boolean

tests the ability to use this key as a key in a hash

Parameters:

  • key (Symbol)

Returns:

  • (Boolean)

    whether or not this can be used as a hash key



112
113
114
# File 'lib/metahash/metadata.rb', line 112

def valid_key?(key)
  not self.respond_to?(key)
end