Class: Dynamoid::Criteria::Chain
- Inherits:
-
Object
- Object
- Dynamoid::Criteria::Chain
- 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.
Instance Attribute Summary collapse
-
#consistent_read ⇒ Object
Returns the value of attribute consistent_read.
-
#query ⇒ Object
Returns the value of attribute query.
-
#source ⇒ Object
Returns the value of attribute source.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
-
#all ⇒ Object
Returns all the records matching the criteria.
- #batch(batch_size) ⇒ Object
- #consistent ⇒ Object
- #consistent_opts ⇒ Object
-
#destroy_all ⇒ Object
Destroys all the records matching the criteria.
-
#each(&block) ⇒ Object
Allows you to use the results of a search as an enumerable over the results found.
- #eval_limit(limit) ⇒ Object
-
#initialize(source) ⇒ Chain
constructor
Create a new criteria chain.
- #scan_index_forward(scan_index_forward) ⇒ Object
- #start(start) ⇒ Object
-
#where(args) ⇒ Object
The workhorse method of the criteria chain.
Constructor Details
#initialize(source) ⇒ Chain
Create a new criteria chain.
14 15 16 17 18 19 |
# File 'lib/dynamoid/criteria/chain.rb', line 14 def initialize(source) @query = {} @source = source @consistent_read = false @scan_index_forward = true end |
Instance Attribute Details
#consistent_read ⇒ Object
Returns the value of attribute consistent_read.
8 9 10 |
# File 'lib/dynamoid/criteria/chain.rb', line 8 def consistent_read @consistent_read end |
#query ⇒ Object
Returns the value of attribute query.
8 9 10 |
# File 'lib/dynamoid/criteria/chain.rb', line 8 def query @query end |
#source ⇒ Object
Returns the value of attribute source.
8 9 10 |
# File 'lib/dynamoid/criteria/chain.rb', line 8 def source @source end |
#values ⇒ Object
Returns the value of attribute values.
8 9 10 |
# File 'lib/dynamoid/criteria/chain.rb', line 8 def values @values end |
Instance Method Details
#all ⇒ Object
Returns all the records matching the criteria.
45 46 47 |
# File 'lib/dynamoid/criteria/chain.rb', line 45 def all records end |
#batch(batch_size) ⇒ Object
76 77 78 79 |
# File 'lib/dynamoid/criteria/chain.rb', line 76 def batch(batch_size) @batch_size = batch_size self end |
#consistent ⇒ Object
37 38 39 40 |
# File 'lib/dynamoid/criteria/chain.rb', line 37 def consistent @consistent_read = true self end |
#consistent_opts ⇒ Object
98 99 100 |
# File 'lib/dynamoid/criteria/chain.rb', line 98 def consistent_opts { :consistent_read => consistent_read } end |
#destroy_all ⇒ Object
Destroys all the records matching the criteria.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dynamoid/criteria/chain.rb', line 51 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.
94 95 96 |
# File 'lib/dynamoid/criteria/chain.rb', line 94 def each(&block) records.each(&block) end |
#eval_limit(limit) ⇒ Object
71 72 73 74 |
# File 'lib/dynamoid/criteria/chain.rb', line 71 def eval_limit(limit) @eval_limit = limit self end |
#scan_index_forward(scan_index_forward) ⇒ Object
86 87 88 89 |
# File 'lib/dynamoid/criteria/chain.rb', line 86 def scan_index_forward(scan_index_forward) @scan_index_forward = scan_index_forward self end |
#start(start) ⇒ Object
81 82 83 84 |
# File 'lib/dynamoid/criteria/chain.rb', line 81 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.
32 33 34 35 |
# File 'lib/dynamoid/criteria/chain.rb', line 32 def where(args) args.each {|k, v| query[k.to_sym] = v} self end |