Module: CouchRest::Model::Properties::ClassMethods

Defined in:
lib/couchrest/model/properties.rb

Instance Method Summary collapse

Instance Method Details

#property(name, *options, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/couchrest/model/properties.rb', line 132

def property(name, *options, &block)
  raise "Invalid property definition, '#{name}' already used for CouchRest Model type field" if name.to_s == model_type_key.to_s && CouchRest::Model::Base >= self
  opts = { }
  type = options.shift
  if type.class != Hash
    opts[:type] = type
    opts.merge!(options.shift || {})
  else
    opts.update(type)
  end
  existing_property = self.properties.find{|p| p.name == name.to_s}
  if existing_property.nil? || (existing_property.default != opts[:default])
    define_property(name, opts, &block)
  end
end

#timestamps!Object

Automatically set updated_at and created_at fields on the document whenever saving occurs.

These properties are casted as Time objects, so they should always be set to UTC.



153
154
155
156
157
158
159
160
161
# File 'lib/couchrest/model/properties.rb', line 153

def timestamps!
  property(:updated_at, Time, :read_only => true, :protected => true, :auto_validation => false)
  property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false)

  set_callback :save, :before do |object|
    write_attribute('updated_at', Time.now)
    write_attribute('created_at', Time.now) if object.new?
  end
end