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 by a Query or Scan.

Constant Summary collapse

TYPES_TO_DUMP_FOR_QUERY =

TODO: Should we transform any other types of query values?

[:string, :integer, :boolean, :serialized]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Chain

Create a new criteria chain.

Parameters:

  • source (Class)

    the class upon which the ultimate query will be performed.



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

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

Instance Attribute Details

#consistent_readObject

Returns the value of attribute consistent_read.



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

def consistent_read
  @consistent_read
end

#hash_keyObject (readonly)

Returns the value of attribute hash_key.



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

def hash_key
  @hash_key
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



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

def index_name
  @index_name
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#range_keyObject (readonly)

Returns the value of attribute range_key.



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

def range_key
  @range_key
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#allObject

Returns all the records matching the criteria.

Since:

  • 0.2.0



54
55
56
# File 'lib/dynamoid/criteria/chain.rb', line 54

def all
  records
end

#batch(batch_size) ⇒ Object



102
103
104
105
# File 'lib/dynamoid/criteria/chain.rb', line 102

def batch(batch_size)
  @batch_size = batch_size
  self
end

#consistentObject



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

def consistent
  @consistent_read = true
  self
end

#consistent_optsObject



124
125
126
# File 'lib/dynamoid/criteria/chain.rb', line 124

def consistent_opts
  { :consistent_read => consistent_read }
end

#destroy_allObject

Destroys all the records matching the criteria.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dynamoid/criteria/chain.rb', line 66

def destroy_all
  ids = []

  if key_present?
    ranges = []
    Dynamoid.adapter.query(source.table_name, range_query).collect do |hash|
      ids << hash[source.hash_key.to_sym]
      ranges << hash[source.range_key.to_sym]
    end

    Dynamoid.adapter.delete(source.table_name, ids,{:range_key => ranges})
  else
    Dynamoid.adapter.scan(source.table_name, query, scan_opts).collect do |hash|
      ids << hash[source.hash_key.to_sym]
    end

    Dynamoid.adapter.delete(source.table_name, ids)
  end
end

#each(&block) ⇒ Object

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

Since:

  • 0.2.0



120
121
122
# File 'lib/dynamoid/criteria/chain.rb', line 120

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

#lastObject

Returns the last fetched record matched the criteria



60
61
62
# File 'lib/dynamoid/criteria/chain.rb', line 60

def last
  all.last
end

#record_limit(limit) ⇒ Object

The record limit is the limit of evaluated records returned by the query or scan.



88
89
90
91
# File 'lib/dynamoid/criteria/chain.rb', line 88

def record_limit(limit)
  @record_limit = limit
  self
end

#scan_index_forward(scan_index_forward) ⇒ Object



112
113
114
115
# File 'lib/dynamoid/criteria/chain.rb', line 112

def scan_index_forward(scan_index_forward)
  @scan_index_forward = scan_index_forward
  self
end

#scan_limit(limit) ⇒ Object

The scan limit which is the limit of records that DynamoDB will internally query or scan. This is different from the record limit as with filtering DynamoDB may look at N scanned records but return 0 records if none pass the filter.



97
98
99
100
# File 'lib/dynamoid/criteria/chain.rb', line 97

def scan_limit(limit)
  @scan_limit = limit
  self
end

#start(start) ⇒ Object



107
108
109
110
# File 'lib/dynamoid/criteria/chain.rb', line 107

def start(start)
  @start = start
  self
end

#where(args) ⇒ Object

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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dynamoid/criteria/chain.rb', line 34

def where(args)
  args.each do |k, v|
    sym = k.to_sym
    query[sym] = if (field_options = source.attributes[sym]) && (type = field_options[:type]) && TYPES_TO_DUMP_FOR_QUERY.include?(type)
                   source.dump_field(v, field_options)
                 else
                   v
                 end
  end
  self
end