Module: DynamicAccessors

Included in:
Sprout::ERBContext, Sprout::ToolTaskModel
Defined in:
lib/sprout/dynamic_accessors.rb

Overview

DynamicAccessors provides support for simply setting and getting values of any kind from any object that includes it.

require 'dynamic_accessors'

class Foo
  include DynamicAccessors
end

foo = Foo.new
foo.bar = "Hello World"
foo.friends = ['a', 'b', 'c']
puts "foo.bar: #{foo.bar}"
puts "foo.friends: #{foo.friends.join(', ')}"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



31
32
33
34
35
36
37
38
39
# File 'lib/sprout/dynamic_accessors.rb', line 31

def method_missing(method, *params, &block)
  if(method.to_s.match(/=$/))
    method = method.to_s.gsub('=', '').to_sym
    return @missing_params_hash[method] = params.shift
  else
    return @missing_params_hash[method] if @missing_params_hash.keys.collect(&:to_sym).include?(method)
  end
  super
end

Instance Method Details

#each_attributeObject



25
26
27
28
29
# File 'lib/sprout/dynamic_accessors.rb', line 25

def each_attribute
  @missing_params_hash.each do |param|
    yield param if block_given?
  end
end

#initialize(*params) ⇒ Object



20
21
22
23
# File 'lib/sprout/dynamic_accessors.rb', line 20

def initialize(*params)
  super
  @missing_params_hash = Hash.new
end