Class: DataMapper::Machinist::DataMapperExtentions::Lathe

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-machinist/machinist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, attributes) ⇒ Lathe

Returns a new instance of Lathe.



47
48
49
50
51
52
53
54
# File 'lib/dm-machinist/machinist.rb', line 47

def initialize(object, attributes)
  @object = object.new
  @assigned_attributes = []
  attributes.each do |key, value|
    @object.send("#{key}=", value)
    @assigned_attributes << key
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dm-machinist/machinist.rb', line 58

def method_missing(symbol, *args, &block)
  if @assigned_attributes.include?(symbol)
    @object.send(symbol)
  else
    value = if block
      block.call
    elsif args.first.is_a?(Hash) || args.empty?
      symbol.to_s.camelize.constantize.make(args.first || {})
    else
      args.first
    end
    @object.send("#{symbol}=", value)
    @assigned_attributes << symbol
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



56
57
58
# File 'lib/dm-machinist/machinist.rb', line 56

def object
  @object
end