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.



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

def attributes
  @attributes ||= {}
end

#optimistic_locking(attribute_name = :version_id) ⇒ Object



564
565
566
567
# File 'lib/aws/record/abstract_base.rb', line 564

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.



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

def optimistic_locking?
  !!@optimistic_locking_attr
end

#optimistic_locking_attrObject



576
577
578
# File 'lib/aws/record/abstract_base.rb', line 576

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)


551
552
553
554
555
556
557
# File 'lib/aws/record/abstract_base.rb', line 551

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)


500
501
502
# File 'lib/aws/record/abstract_base.rb', line 500

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.



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/aws/record/abstract_base.rb', line 511

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