ncbo / goo

Graph Oriented Objects (GOO) for Ruby. A RDF/SPARQL based ORM.
http://ncbo.github.io/goo/
Other
15 stars 6 forks source link

Auto-populate date fields (optional) #24

Closed palexander closed 11 years ago

palexander commented 11 years ago

Would be cool to have an option to have date fields populated on save or instantiation. Here's what I imagine:

class Category < Goo::Base::Resource
  model :project
  attribute :creator, :cardinality => { :max => 1, :min => 1 }
  attribute :created, :date_time_xsd => true, :populate_date => :create, :cardinality => { :max => 1, :min => 1 }
  attribute :updated, :date_time_xsd => true, :populate_date => :update, :cardinality => { :max => 1, :min => 1 }
end

This would save a lot of boilerplate code. In ActiveRecord you get the "created" and "updated" fields for every object unless you specify otherwise.

:+1:

msalvadores commented 11 years ago

:+1:

msalvadores commented 11 years ago

The way it has been implemented is:

attribute :created, :date_time_xsd => true, :single_value=> true, :default => lambda { |record| DateTime.now }

if needed the record is passed to the lambda in case one needs to access any attributes to set the default value.