Module: LooseChange::Attributes

Included in:
Base
Defined in:
lib/loose_change/attributes.rb

Instance Method Summary collapse

Instance Method Details

#default(property, value) ⇒ Object

:nodoc:



22
23
24
25
# File 'lib/loose_change/attributes.rb', line 22

def default(property, value)
  self.defaults ||= {}
  self.defaults[property] = value
end

#property(name, opts = {}) ⇒ Object

Set a field to be stored in a CouchDB document. Any JSON-encodable value can be used; there is no explicit typing. The :default option can be used to always store a value on documents where this property has not been set.

class Recipe < LooseChange::Base
  property :name
  property :popularity, :default => 0
end


14
15
16
17
18
19
# File 'lib/loose_change/attributes.rb', line 14

def property(name, opts = {})
  attr_accessor name.to_sym
  self.properties = ((self.properties || []) << name.to_sym)
  default(name.to_sym, opts[:default]) if opts[:default]
  define_attribute_methods [name]
end

#timestamps!Object

Automatically set up :created_at and :updated_at properties which are set on creation and save, respectively.



30
31
32
33
34
35
36
37
# File 'lib/loose_change/attributes.rb', line 30

def timestamps!
  property :created_at
  property :updated_at
  
  before_create :touch_created_at
  before_create :touch_updated_at
  before_save :touch_updated_at
end