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

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

Instance Method Summary collapse

Instance Method Details

#add_attribute(attribute) ⇒ Object



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/aws/record/abstract_base.rb', line 595

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

#attribute_for(attribute_name, &block) ⇒ Object



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

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

#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

#new_scopeObject



560
561
562
# File 'lib/aws/record/abstract_base.rb', line 560

def new_scope
  self::Scope.new(self)
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