Class: Dynamoid::Criteria::Chain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dynamoid/criteria/chain.rb

Overview

The criteria chain is equivalent to an ActiveRecord relation (and realistically I should change the name from chain to relation). It is a chainable object that builds up a query and eventually executes it either on an index or by a full table scan.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Chain) initialize(source)

Create a new criteria chain.

Parameters:

  • source (Class)

    the class upon which the ultimate query will be performed.



15
16
17
18
19
# File 'lib/dynamoid/criteria/chain.rb', line 15

def initialize(source)
  @query = {}
  @source = source
  @consistent_read = false
end

Instance Attribute Details

- (Object) consistent_read

Returns the value of attribute consistent_read



9
10
11
# File 'lib/dynamoid/criteria/chain.rb', line 9

def consistent_read
  @consistent_read
end

- (Object) limit(limit)

Returns the value of attribute limit



9
10
11
12
# File 'lib/dynamoid/criteria/chain.rb', line 9

def limit(limit)
  @limit = limit
  records
end

- (Object) query

Returns the value of attribute query



9
10
11
# File 'lib/dynamoid/criteria/chain.rb', line 9

def query
  @query
end

- (Object) source

Returns the value of attribute source



9
10
11
# File 'lib/dynamoid/criteria/chain.rb', line 9

def source
  @source
end

- (Object) start(start)

Returns the value of attribute start



9
10
11
12
# File 'lib/dynamoid/criteria/chain.rb', line 9

def start(start)
  @start = start
  self
end

- (Object) values

Returns the value of attribute values



9
10
11
# File 'lib/dynamoid/criteria/chain.rb', line 9

def values
  @values
end

Instance Method Details

- (Object) all

Returns all the records matching the criteria.

Since:

  • 0.2.0



45
46
47
# File 'lib/dynamoid/criteria/chain.rb', line 45

def all
  records
end

- (Object) consistent



37
38
39
40
# File 'lib/dynamoid/criteria/chain.rb', line 37

def consistent
  @consistent_read = true
  self
end

- (Object) consistent_opts



73
74
75
# File 'lib/dynamoid/criteria/chain.rb', line 73

def consistent_opts
  { :consistent_read => consistent_read }
end

- (Object) each(&block)

Allows you to use the results of a search as an enumerable over the results found.

Since:

  • 0.2.0



69
70
71
# File 'lib/dynamoid/criteria/chain.rb', line 69

def each(&block)
  records.each(&block)
end

- (Object) first

Returns the first record matching the criteria.

Since:

  • 0.2.0



52
53
54
# File 'lib/dynamoid/criteria/chain.rb', line 52

def first
  limit(1).first
end

- (Object) where(args)

The workhorse method of the criteria chain. Each key in the passed in hash will become another criteria that the ultimate query must match. A key can either be a symbol or a string, and should be an attribute name or an attribute name with a range operator.

Examples:

A simple criteria

where(:name => 'Josh')

A more complicated criteria

where(:name => 'Josh', 'created_at.gt' => DateTime.now - 1.day)

Since:

  • 0.2.0



32
33
34
35
# File 'lib/dynamoid/criteria/chain.rb', line 32

def where(args)
  args.each {|k, v| query[k] = v}
  self
end