Class: Maven::Model::ModelHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/maven/model/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(clazz) ⇒ ModelHash

Returns a new instance of ModelHash.



162
163
164
# File 'lib/maven/model/utils.rb', line 162

def initialize(clazz)
  @clazz = clazz
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



194
195
196
# File 'lib/maven/model/utils.rb', line 194

def method_missing(method, *args, &block)
  default_model.send(method, *args, &block)
end

Instance Method Details

#default_modelObject



185
186
187
188
189
190
191
192
# File 'lib/maven/model/utils.rb', line 185

def default_model
  @default_model ||= 
    begin
      model = @clazz.new
      self[nil] = model
      model
    end
end

#get(key, &block) ⇒ Object Also known as: new, add



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/maven/model/utils.rb', line 170

def get(key, &block)
  key = key.to_sym if key
  result = self[key]
  if result.nil?
    keys << key
    result = (self[key] = @clazz.new(key))
  end
  if block
    block.call(result)
  end
  result
end

#keysObject



166
167
168
# File 'lib/maven/model/utils.rb', line 166

def keys
  @keys ||= []
end