@private
# File lib/aws/record/abstract_base.rb, line 598 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
@private
# File lib/aws/record/abstract_base.rb, line 590 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 585 def attributes @attributes ||= {} end
@private
# File lib/aws/record/abstract_base.rb, line 563 def new_scope self::Scope.new(self) end
# File lib/aws/record/abstract_base.rb, line 567 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 574 def optimistic_locking? !!@optimistic_locking_attr end
# File lib/aws/record/abstract_base.rb, line 579 def optimistic_locking_attr @optimistic_locking_attr 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 554 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 503 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 514 def shard_name name = nil case name when nil @_shard_name || self.name when AWS::DynamoDB::Table name.name.gsub(%r^#{Record::table_prefix}/, '') when AWS::SimpleDB::Domain name.name.gsub(%r^#{Record::domain_prefix}/, '') else name end end