Module: Attribrutal::Model::ClassMethods

Defined in:
lib/attribrutal/model.rb

Instance Method Summary collapse

Instance Method Details

#attribute(sym, coercer = nil, attrs = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/attribrutal/model.rb', line 29

def attribute (sym, coercer=nil, attrs = {})

  define_method(sym) do
    if coercer
      coercer.send(:coerce, @attributes[sym], attrs[:default])
    else
      @attributes[sym] || default
    end
  end

  define_method("#{sym}=".to_sym) do |value|
    @attributes[sym] = value
  end

  if @attributes
    @attributes.merge!({ sym => coercer })
  else
    @attributes = { sym => coercer }
  end
end

#attributesObject



50
51
52
# File 'lib/attribrutal/model.rb', line 50

def attributes
  @attributes
end