Class: Machinist::Lathe

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attributes = {}) ⇒ Lathe

Returns a new instance of Lathe.



55
56
57
58
59
60
# File 'lib/machinist.rb', line 55

def initialize(klass, attributes = {})
  @save = !!attributes.delete(:_save_)
  @object = klass.new
  attributes.each {|key, value| @object.send("#{key}=", value) }
  @assigned_attributes = attributes.keys.map(&:to_sym)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/machinist.rb', line 69

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?
      meth = @save ? :make : :make_unsaved
      symbol.to_s.camelize.constantize.send(meth, (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.



67
68
69
# File 'lib/machinist.rb', line 67

def object
  @object
end