Module: AWS::Record::AbstractBase::ClassMethods

Defined in:
lib/aws/record/abstract_base.rb

Instance Method Summary collapse

Instance Method Details

#attributesHash<String,Attribute>

Returns a hash of all of the configured attributes for this class.

Returns:

  • (Hash<String,Attribute>)

    Returns a hash of all of the configured attributes for this class.



626
627
628
# File 'lib/aws/record/abstract_base.rb', line 626

def attributes
  @attributes ||= {}
end

#create(attributes = {}) ⇒ Object

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]


579
580
581
# File 'lib/aws/record/abstract_base.rb', line 579

def create attributes = {}
  create_impl(attributes, :create, :save)
end

#create!(attributes = {}) ⇒ Object

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


599
600
601
# File 'lib/aws/record/abstract_base.rb', line 599

def create! attributes = {}
  create_impl(attributes, :create!, :save!)
end

#optimistic_locking(attribute_name = :version_id) ⇒ Object



608
609
610
611
# File 'lib/aws/record/abstract_base.rb', line 608

def optimistic_locking attribute_name = :version_id
  attribute = integer_attr(attribute_name)
  @optimistic_locking_attr = attribute
end

#optimistic_locking?Boolean

Returns true if this class is configured to perform optimistic locking.

Returns:

  • (Boolean)

    Returns true if this class is configured to perform optimistic locking.



615
616
617
# File 'lib/aws/record/abstract_base.rb', line 615

def optimistic_locking?
  !!@optimistic_locking_attr
end

#optimistic_locking_attrObject



620
621
622
# File 'lib/aws/record/abstract_base.rb', line 620

def optimistic_locking_attr
  @optimistic_locking_attr
end

#scope(name, scope = nil, &block) ⇒ Object

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

Parameters:

  • name (Symbol)

    The name of the scope. Scope names should be method-safe and should not conflict with any other class methods.

  • scope (Scope) (defaults to: nil)


555
556
557
558
559
560
561
# File 'lib/aws/record/abstract_base.rb', line 555

def scope name, scope = nil, &block

  method_definition = scope ? lambda { scope } : block

  extend(Module.new { define_method(name, &method_definition) })

end

#set_shard_name(name) ⇒ Object Also known as: set_domain_name, shard_name=

Allows you to override the default shard name for this class. The shard name defaults to the class name.

Parameters:

  • name (String)


504
505
506
# File 'lib/aws/record/abstract_base.rb', line 504

def set_shard_name name
  @_shard_name = name
end

#shard_name(name = nil) ⇒ String Also known as: domain_name

Returns the name of the shard this class will persist records into by default.

Parameters:

  • name (String) (defaults to: nil)

    Defaults to the name of this class.

Returns:

  • (String)

    Returns the full prefixed domain name for this class.



515
516
517
518
519
520
521
522
523
524
525
# File 'lib/aws/record/abstract_base.rb', line 515

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