Class: AWS::Record::Model::Scope

Inherits:
Scope
  • Object
show all
Defined in:
lib/aws/record/model/scope.rb

Overview

The primary interface for finding records with AWS::Record::Model.

Getting a Scope Object

You should normally never need to construct a Scope object directly. Scope objects are returned from the AWS::Record::Model finder methods (e.g. shard, where, order, limit, etc).

books = Book.where(:author => 'John Doe')
books.class #=> AWS::Record::Scope, not Array

Scopes are also returned from methods defined with the scope method.

Chaining Scopes

Scope objects represent a request, but do not actualy make a request until required. This allows you to chain requests

# no request made by the following 2 statements
books = Book.where(:author => 'John Doe')
books = books.limit(10)

books.each do |book|
  # yields up to 10 books
end

Each of the following methods returns a scope that can be chained.

Terminating Scopes

To terminate a scope you can enumerate it or call #first.

# terminate a scope by enumerating
Book.limit(10).each {|book| ... }

# terminate a scope by getting the first value
Book.where('author' => 'John Doe').first

Instance Attribute Summary

Attributes inherited from Scope

#base_class

Instance Method Summary collapse

Methods inherited from Scope

#count, #each, #find, #first, #limit, #shard

Constructor Details

#initialize(base_class, options = {}) ⇒ Scope

Returns a new instance of Scope.


64
65
66
67
# File 'lib/aws/record/model/scope.rb', line 64

def initialize base_class, options = {}
  super
  @options[:where] ||= []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AWS::Record::Scope

Instance Method Details

#new(attributes = {}) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aws/record/model/scope.rb', line 69

def new attributes = {}

  attributes = attributes.dup

  @options[:where].each do |conditions|
    if conditions.size == 1 and conditions.first.is_a?(Hash)
      attributes.merge!(conditions.first)
    end
  end
  
  super(attributes)
  
end

#order(attribute_name, order = :asc) ⇒ Object

Specifies how to sort records returned.

# enumerate books, starting with the most recently published ones
Book.order(:published_at, :desc).each do |book|
  # ...
end

Only one order may be applied. If order is specified more than once the last one in the chain takes precedence:

# books returned by this scope will be ordered by :published_at
# and not :author.
Book.where(:read => false).order(:author).order(:published_at)

Parameters:

  • attribute_name (String, Symbol)

    The attribute to sort by.

  • order (:asc, :desc) (defaults to: :asc)

    (:asc) The direct to sort.


132
133
134
# File 'lib/aws/record/model/scope.rb', line 132

def order attribute_name, order = :asc
  _with(:order => [attribute_name, order])
end

#where(conditions_hash) ⇒ Scope #where(conditions_string, *values) ⇒ Scope

Applies conditions to the scope that limit which records are returned. Only those matching all given conditions will be returned.

Overloads:

  • #where(conditions_hash) ⇒ Scope

    Specify a hash of conditions to query with. Multiple conditions are joined together with AND.

    Book.where(:author => 'John Doe', :softcover => true)
    # where `author` = `John Doe` AND `softcover` = `1`
    

    Parameters:

    • conditions (Hash)
  • #where(conditions_string, *values) ⇒ Scope

    A sql-like query fragment with optional placeholders and values. Placeholders are replaced with properly quoted values.

    Book.where('author = ?', 'John Doe')
    

    Parameters:

    • conditions_string (String)

      A sql-like where string with question mark placeholders. For each placeholder there should be a value that will be quoted into that position.

    • *values (String)

      A value that should be quoted into the corresponding (by position) placeholder.

Returns:

  • (Scope)

    Returns a new scope with the passed conditions applied.


108
109
110
111
112
113
# File 'lib/aws/record/model/scope.rb', line 108

def where *conditions
  if conditions.empty?
    raise ArgumentError, 'missing required condition'
  end
  _with(:where => @options[:where] + [conditions])
end