@api private
# File lib/aws/record/abstract_base.rb, line 612 def add_attribute attribute attr_name = attribute.name attributes[attr_name] = attribute # setter define_method("#{attr_name}=") do |value| self[attr_name] = value end # getter define_method(attr_name) do self[attr_name] end # before type-cast getter define_method("#{attr_name}_before_type_cast") do @_data[attr_name] end # dirty tracking methods define_method("#{attr_name}_changed?") do attribute_changed?(attr_name) end define_method("#{attr_name}_change") do attribute_change(attr_name) end define_method("#{attr_name}_was") do attribute_was(attr_name) end define_method("#{attr_name}_will_change!") do attribute_will_change!(attr_name) end define_method("reset_#{attr_name}!") do reset_attribute!(attr_name) end attribute end
@api private
# File lib/aws/record/abstract_base.rb, line 604 def attribute_for attribute_name, &block unless attribute = attributes[attribute_name.to_s] raise UndefinedAttributeError.new(attribute_name.to_s) end block_given? ? yield(attribute) : attribute end
@return [Hash<String,Attribute>] Returns a hash of all of the
configured attributes for this class.
# File lib/aws/record/abstract_base.rb, line 599 def attributes @attributes ||= {} end
Creates an object (or multiple if you pass an array of attributes). The {#save} method is called on the object(s) after construction. The object(s) are returned wether or not the object(s) are valid.
class Book < AWS::Record::Model string_attr :title end book = Book.create(:title => "The big book of tests") book.persisted? #=> true books = Book.create([{:title => 'abc'}, {:title => 'xyz'}]) books.each(&:persisted?) #=> [true, true]
# File lib/aws/record/abstract_base.rb, line 552 def create attributes = {} create_impl(attributes, :create, :save) end
Creates an object (or multiple if you pass an array of attributes). The {#save!} method is called on the object(s) after construction. If the object(s) are not valid, then an error is raised.
class Book < AWS::Record::Model string_attr :title validates_presence_of :title end book = Book.create!(:title => "The big book of tests") book.persisted? #=> true book = Book.create!() #=> raises AWS::Record::InvalidRecordError
# File lib/aws/record/abstract_base.rb, line 572 def create! attributes = {} create_impl(attributes, :create!, :save!) end
@api private
# File lib/aws/record/abstract_base.rb, line 577 def new_scope self::Scope.new(self) end
# File lib/aws/record/abstract_base.rb, line 581 def optimistic_locking attribute_name = :version_id attribute = integer_attr(attribute_name) @optimistic_locking_attr = attribute end
@return [Boolean] Returns true if this class is configured to
perform optimistic locking.
# File lib/aws/record/abstract_base.rb, line 588 def optimistic_locking? !!@optimistic_locking_attr end
# File lib/aws/record/abstract_base.rb, line 593 def optimistic_locking_attr @optimistic_locking_attr end
@api private
# File lib/aws/record/abstract_base.rb, line 660 def remove_attribute(attribute) send(:remove_method, attribute.name) send(:remove_method, "#{attribute.name}=") send(:remove_method, "#{attribute.name}_before_type_cast") send(:remove_method, "#{attribute.name}_changed?") send(:remove_method, "#{attribute.name}_change") send(:remove_method, "#{attribute.name}_was") send(:remove_method, "#{attribute.name}_will_change!") send(:remove_method, "reset_#{attribute.name}!") validators.each do |validator| validator.attribute_names.delete(attribute.name) end attributes.delete(attribute.name) end
Adds a scoped finder to this class.
class Book < AWS::Record::Model scope :top_10, order(:popularity, :desc).limit(10) end Book.top_10.to_a #=> [#<Book...>, #<Book...>] Book.top_10.first #=> #<Book...>
You can also provide a block that accepts params for the scoped finder. This block should return a scope.
class Book < AWS::Record::Model scope :by_author, lambda {|name| where(:author => name) } end # top 10 books by the author 'John Doe' Book.by_author('John Doe').top_10
@param [Symbol] name The name of the scope. Scope names should be
method-safe and should not conflict with any other class methods.
@param [Scope] scope
# File lib/aws/record/abstract_base.rb, line 528 def scope name, scope = nil, &block method_definition = scope ? lambda { scope } : block extend(Module.new { define_method(name, &method_definition) }) end
Allows you to override the default shard name for this class. The shard name defaults to the class name. @param [String] name
# File lib/aws/record/abstract_base.rb, line 477 def set_shard_name name @_shard_name = name end
Returns the name of the shard this class will persist records into by default.
@param [String] name Defaults to the name of this class. @return [String] Returns the full prefixed domain name for this class.
# File lib/aws/record/abstract_base.rb, line 488 def shard_name name = nil case name when nil @_shard_name || self.name when AWS::DynamoDB::Table name.name.gsub(/^#{Record::table_prefix}/, '') when AWS::SimpleDB::Domain name.name.gsub(/^#{Record::domain_prefix}/, '') else name end end
# File lib/aws/record/abstract_base.rb, line 677 def create_impl attributes = {}, create_method = :create, save_method = :save if attributes.is_a?(Array) attributes.map {|attr| send(create_method, attr) } else obj = new(attributes) obj.send(save_method) obj end end