You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

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.



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

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]


551
552
553
# File 'lib/aws/record/abstract_base.rb', line 551

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


571
572
573
# File 'lib/aws/record/abstract_base.rb', line 571

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

#optimistic_locking(attribute_name = :version_id) ⇒ Object



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

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.



587
588
589
# File 'lib/aws/record/abstract_base.rb', line 587

def optimistic_locking?
  !!@optimistic_locking_attr
end

#optimistic_locking_attrObject



592
593
594
# File 'lib/aws/record/abstract_base.rb', line 592

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)


527
528
529
530
531
532
533
# File 'lib/aws/record/abstract_base.rb', line 527

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)


476
477
478
# File 'lib/aws/record/abstract_base.rb', line 476

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.



487
488
489
490
491
492
493
494
495
496
497
# File 'lib/aws/record/abstract_base.rb', line 487

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