Permalink
Browse files

change all the examples to use attr_reader.

  • Loading branch information...
1 parent dd65f94 commit b3d03c00ad4a6f226ab064afe41d3406192e97bf @pitluga pitluga committed Feb 27, 2012
Showing with 52 additions and 28 deletions.
  1. +7 −1 lib/curator/model.rb
  2. +6 −7 lib/curator/repository.rb
  3. +38 −19 spec/curator/model_spec.rb
  4. +1 −1 spec/spec_helper.rb
@@ -7,7 +7,7 @@ module Model
include ActiveModel::Conversion
included do
- attr_accessor :created_at, :updated_at
+ attr_reader :created_at, :updated_at
attr_writer :version
end
@@ -25,6 +25,12 @@ def persisted?
id.present?
end
+ def touch
+ now = Time.now.utc
+ @created_at = now if @created_at.nil?
+ @updated_at = now
+ end
+
def version
@version || self.class.version
end
@@ -59,7 +59,7 @@ def migrator
end
def save(object)
- _update_timestamps(object)
+ object.touch
save_without_timestamps(object)
end
@@ -74,7 +74,7 @@ def save_without_timestamps(object)
hash[:key] = object.id
data_store.save(hash)
else
- object.id = data_store.save(hash).key
+ object.instance_variable_set("@id", data_store.save(hash).key)
end
end
@@ -109,11 +109,10 @@ def deserialize(attributes)
def _deserialize(id, data)
attributes = data.with_indifferent_access
migrated_attributes = migrator.migrate(attributes)
- object = deserialize(migrated_attributes)
- object.id = id
- object.created_at = Time.parse(attributes[:created_at].to_s) if attributes[:created_at].present?
- object.updated_at = Time.parse(attributes[:updated_at].to_s) if attributes[:updated_at].present?
- object
+ migrated_attributes[:id] = id
+ migrated_attributes[:created_at] = Time.parse(migrated_attributes[:created_at]) if migrated_attributes[:created_at]
+ migrated_attributes[:updated_at] = Time.parse(migrated_attributes[:updated_at]) if migrated_attributes[:updated_at]
+ deserialize(migrated_attributes)
end
def _format_time_for_index(time)
@@ -1,17 +1,6 @@
require "spec_helper"
describe Curator::Model do
- describe "self.included" do
- it "adds accessors for created_at and updated_at" do
- instance = TestModel.new
- instance.created_at = :created_at
- instance.updated_at = :updated_at
-
- instance.created_at.should == :created_at
- instance.updated_at.should == :updated_at
- end
- end
-
describe "initialize" do
it "sets instance values provided in the args" do
model_class = Class.new do
@@ -27,24 +16,54 @@
describe "==" do
it "is equals if ids match" do
- instance1 = TestModel.new
- instance1.id = "id"
- instance2 = TestModel.new
- instance2.id = "id"
+ instance1 = TestModel.new(:id => "id")
+ instance2 = TestModel.new(:id => "id")
instance1.should == instance2
end
it "is not equals if ids don't match" do
- instance1 = TestModel.new
- instance1.id = "id"
- instance2 = TestModel.new
- instance2.id = "id2"
+ instance1 = TestModel.new(:id => "id")
+ instance2 = TestModel.new(:id => "id2")
instance1.should_not == instance2
end
end
+ describe "touch" do
+ it "updates the models timestamps to now" do
+ model = TestModel.new
+ model.created_at.should be_nil
+ model.updated_at.should be_nil
+ model.touch
+ model.created_at.should_not be_nil
+ model.updated_at.should_not be_nil
+ end
+
+ it "does not change created_at after it has been created" do
+ model = TestModel.new
+ model.touch
+ created_at = model.created_at
+ model.touch
+ model.created_at.should == created_at
+ end
+
+ it "changes updated_at each time" do
+ model = TestModel.new
+ model.touch
+ updated_at = model.updated_at
+ model.touch
+ model.updated_at.should_not == updated_at
+ end
+
+ it "saves times in utc" do
+ model = TestModel.new
+ model.touch
+ model.updated_at.zone.should == "UTC"
+ model.created_at.zone.should == "UTC"
+ end
+ end
+
describe "version" do
it "defaults to 0" do
instance = TestModel.new
View
@@ -19,7 +19,7 @@
class TestModel
include Curator::Model
- attr_accessor :id, :some_field
+ attr_reader :id, :some_field
end
def test_repository(&block)

0 comments on commit b3d03c0

Please sign in to comment.