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

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

Instance Method Summary collapse

Instance Method Details

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/couchrest/model/properties.rb', line 173

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.



194
195
196
197
198
199
200
201
202
# File 'lib/couchrest/model/properties.rb', line 194

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