Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Initializer callback support #42

Merged
merged 5 commits into from

2 participants

@afrojas

Added support for initializer callbacks, so now you can

class User
  include Dynamoid::Document

  after_initialize :set_defaults
end

and it'll work as expected.

@Veraticus
Owner

Another good patch, thank you!

@Veraticus Veraticus merged commit 4c9c3c3 into Veraticus:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 lib/dynamoid/components.rb
@@ -1,6 +1,6 @@
# encoding: utf-8
module Dynamoid
-
+
# All modules that a Document is composed of are defined in this
# module, to keep the document class from getting too cluttered.
module Components
@@ -10,8 +10,8 @@ module Components
extend ActiveModel::Translation
extend ActiveModel::Callbacks
- define_model_callbacks :create, :save, :destroy
-
+ define_model_callbacks :create, :save, :destroy, :initialize
+
before_create :set_created_at
before_save :set_updated_at
end
View
26 lib/dynamoid/document.rb
@@ -101,44 +101,46 @@ def exists?(id)
#
# @return [Dynamoid::Document] the new document
#
- # @since 0.2.0
+ # @since 0.2.0
def initialize(attrs = {})
- self.class.send(:field, self.class.hash_key) unless self.respond_to?(self.class.hash_key)
-
- @new_record = true
- @attributes ||= {}
- @associations ||= {}
+ run_callbacks :initialize do
+ self.class.send(:field, self.class.hash_key) unless self.respond_to?(self.class.hash_key)
+
+ @new_record = true
+ @attributes ||= {}
+ @associations ||= {}
- self.class.undump(attrs).each {|key, value| send "#{key}=", value }
+ self.class.undump(attrs).each {|key, value| send "#{key}=", value }
+ end
end
# An object is equal to another object if their ids are equal.
#
- # @since 0.2.0
+ # @since 0.2.0
def ==(other)
return false if other.nil?
other.respond_to?(:hash_key) && other.hash_key == self.hash_key
end
- # Reload an object from the database -- if you suspect the object has changed in the datastore and you need those
+ # Reload an object from the database -- if you suspect the object has changed in the datastore and you need those
# changes to be reflected immediately, you would call this method.
#
# @return [Dynamoid::Document] the document this method was called on
#
- # @since 0.2.0
+ # @since 0.2.0
def reload
self.attributes = self.class.find(self.hash_key).attributes
@associations.values.each(&:reset)
self
end
-
+
# Return an object's hash key, regardless of what it might be called to the object.
#
# @since 0.4.0
def hash_key
self.send(self.class.hash_key)
end
-
+
# Assign an object's hash key, regardless of what it might be called to the object.
#
# @since 0.4.0
View
12 lib/dynamoid/persistence.rb
@@ -107,6 +107,15 @@ def undump_field(value, options)
end
+ # Set updated_at and any passed in field to current DateTime. Useful for things like last_login_at, etc.
+ #
+ def touch(name = nil)
+ now = DateTime.now
+ self.updated_at = now
+ attributes[name] = now if name
+ save
+ end
+
# Is this object persisted in the datastore? Required for some ActiveModel integration stuff.
#
# @since 0.2.0
@@ -146,7 +155,8 @@ def destroy
# @since 0.2.0
def delete
delete_indexes
- Dynamoid::Adapter.delete(self.class.table_name, self.id)
+ options = range_key ? {:range_key => attributes[range_key]} : {}
+ Dynamoid::Adapter.delete(self.class.table_name, self.hash_key, options)
end
# Dump this object's attributes into hash form, fit to be persisted into the datastore.
Something went wrong with that request. Please try again.