lib/dynamoid/document.rb in dynamoid-1.3.4 vs lib/dynamoid/document.rb in dynamoid-2.0.0
- old
+ new
@@ -70,22 +70,30 @@
#
# @return [Dynamoid::Document] the saved document
#
# @since 0.2.0
def create(attrs = {})
- build(attrs).tap(&:save)
+ if attrs.is_a?(Array)
+ attrs.map { |attr| create(attr) }
+ else
+ build(attrs).tap(&:save)
+ end
end
# Initialize a new object and immediately save it to the database. Raise an exception if persistence failed.
#
# @param [Hash] attrs Attributes with which to create the object.
#
# @return [Dynamoid::Document] the saved document
#
# @since 0.2.0
def create!(attrs = {})
- build(attrs).tap(&:save!)
+ if attrs.is_a?(Array)
+ attrs.map { |attr| create!(attr) }
+ else
+ build(attrs).tap(&:save!)
+ end
end
# Initialize a new object.
#
# @param [Hash] attrs Attributes with which to create the object.
@@ -104,14 +112,18 @@
# @return [Boolean] true/false
#
# @since 0.2.0
def exists?(id_or_conditions = {})
case id_or_conditions
- when Hash then ! where(id_or_conditions).all.empty?
- else !! find(id_or_conditions)
+ when Hash then where(id_or_conditions).first.present?
+ else !! find_by_id(id_or_conditions)
end
end
+
+ def deep_subclasses
+ subclasses + subclasses.map(&:deep_subclasses).flatten
+ end
end
# Initialize a new object.
#
# @param [Hash] attrs Attributes with which to create the object.
@@ -165,10 +177,10 @@
# @return [Dynamoid::Document] the document this method was called on
#
# @since 0.2.0
def reload
range_key_value = range_value ? dumped_range_value : nil
- self.attributes = self.class.find(hash_key, :range_key => range_key_value, :consistent_read => true).attributes
+ self.attributes = self.class.find(hash_key, range_key: range_key_value, consistent_read: true).attributes
@associations.values.each(&:reset)
self
end
# Return an object's hash key, regardless of what it might be called to the object.