lib/dynamoid/document.rb in dynamoid-3.8.0 vs lib/dynamoid/document.rb in dynamoid-3.9.0
- old
+ new
@@ -158,18 +158,34 @@
false
end
end
end
+ attr_accessor :abstract_class
+
+ def abstract_class?
+ defined?(@abstract_class) && @abstract_class == true
+ end
+
+ def sti_name
+ name
+ end
+
+ def sti_class_for(type_name)
+ type_name.constantize
+ rescue NameError
+ raise Errors::SubclassNotFound, "STI subclass does not found. Subclass: '#{type_name}'"
+ end
+
# @private
def deep_subclasses
subclasses + subclasses.map(&:deep_subclasses).flatten
end
# @private
def choose_right_class(attrs)
- attrs[inheritance_field] ? attrs[inheritance_field].constantize : self
+ attrs[inheritance_field] ? sti_class_for(attrs[inheritance_field]) : self
end
end
# Initialize a new object.
#
@@ -204,11 +220,11 @@
attrs_virtual = attrs.slice(*(attrs.keys - self.class.attributes.keys))
load(attrs_with_defaults.merge(attrs_virtual))
if block
- block.call(self)
+ yield(self)
end
end
end
# Check equality of two models.
@@ -274,9 +290,29 @@
# Assign a model's range key value.
def range_value=(value)
if self.class.range_key
self[self.class.range_key.to_sym] = value
end
+ end
+
+ def inspect
+ # attributes order is:
+ # - partition key
+ # - sort key
+ # - user defined attributes
+ # - timestamps - created_at/updated_at
+ names = [self.class.hash_key]
+ names << self.class.range_key if self.class.range_key
+ names += self.class.attributes.keys - names - %i[created_at updated_at]
+ names << :created_at if self.class.attributes.key?(:created_at)
+ names << :updated_at if self.class.attributes.key?(:updated_at)
+
+ inspection = names.map do |name|
+ value = read_attribute(name)
+ "#{name}: #{value.inspect}"
+ end.join(', ')
+
+ "#<#{self.class.name} #{inspection}>"
end
private
def dumped_range_value