lib/dynamoid/document.rb in dynamoid-3.1.0 vs lib/dynamoid/document.rb in dynamoid-3.2.0

- old
+ new

@@ -31,10 +31,11 @@ self.options = options super if defined? super end def attr_readonly(*read_only_attributes) + ActiveSupport::Deprecation.warn('[Dynamoid] .attr_readonly is deprecated! Call .find instead of') self.read_only_attributes.concat read_only_attributes.map(&:to_s) end # Returns the read_capacity for this table. # @@ -110,18 +111,32 @@ choose_right_class(attrs).new(attrs) end # Does this object exist? # + # Supports primary key in format that `find` call understands. + # Multiple keys and single compound primary key should be passed only as Array explicitily. + # + # Supports conditions in format that `where` call understands. + # # @param [Mixed] id_or_conditions the id of the object or a hash with the options to filter from. # # @return [Boolean] true/false # + # @example With id + # + # Post.exist?(713) + # Post.exist?([713, 210]) + # + # @example With attributes conditions + # + # Post.exist?(version: 1, 'created_at.gt': Time.now - 1.day) + # # @since 0.2.0 def exists?(id_or_conditions = {}) case id_or_conditions - when Hash then where(id_or_conditions).first.present? + when Hash then where(id_or_conditions).count >= 1 else begin find(id_or_conditions) true rescue Dynamoid::Errors::RecordNotFound @@ -203,11 +218,10 @@ new(attrs_undumped) rescue Dynamoid::Errors::ConditionalCheckFailedException end end - # Update existing document or create new one. # Similar to `.update_fields`. The only diffirence is creating new document. # # Uses efficient low-level `UpdateItem` API call. # Changes attibutes and loads new document version with one API call. @@ -265,11 +279,11 @@ new(attrs_undumped) rescue Dynamoid::Errors::ConditionalCheckFailedException end end - def inc(hash_key_value, range_key_value=nil, counters) + def inc(hash_key_value, range_key_value = nil, counters) options = if range_key value_casted = TypeCasting.cast_field(range_key_value, attributes[range_key]) value_dumped = Dumping.dump_field(value_casted, attributes[range_key]) { range_key: value_dumped } else @@ -301,26 +315,16 @@ # # @return [Dynamoid::Document] the new document # # @since 0.2.0 def initialize(attrs = {}) - # we need this hack for Rails 4.0 only - # because `run_callbacks` calls `attributes` getter while it is still nil - @attributes = {} - run_callbacks :initialize do @new_record = true @attributes ||= {} @associations ||= {} @attributes_before_type_cast ||= {} - self.class.attributes.each do |_, options| - if options[:type].is_a?(Class) && options[:default] - raise 'Dynamoid class-type fields do not support default values' - end - end - attrs_with_defaults = {} self.class.attributes.each do |attribute, options| attrs_with_defaults[attribute] = if attrs.key?(attribute) attrs[attribute] elsif options.key?(:default) @@ -346,9 +350,10 @@ def ==(other) if self.class.identity_map_on? super else return false if other.nil? + other.is_a?(Dynamoid::Document) && hash_key == other.hash_key && range_value == other.range_value end end def eql?(other)