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 either on an index or by a full table scan.
Instance Attribute Summary collapse
-
#consistent_read ⇒ Object
Returns the value of attribute consistent_read.
-
#index ⇒ Object
writeonly
Sets the attribute index.
-
#limit(limit) ⇒ Object
Returns the value of attribute limit.
-
#query ⇒ Object
Returns the value of attribute query.
-
#source ⇒ Object
Returns the value of attribute source.
-
#start(start) ⇒ Object
Returns the value of attribute start.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
-
#all(opts = {}) ⇒ 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.
-
#first ⇒ Object
Returns the first record matching the criteria.
-
#initialize(source) ⇒ Chain
constructor
Create a new criteria chain.
- #scan_index_forward(scan_index_forward) ⇒ Object
-
#where(args) ⇒ Object
The workhorse method of the criteria chain.
Constructor Details
#initialize(source) ⇒ Chain
Create a new criteria chain.
15 16 17 18 19 20 |
# File 'lib/dynamoid/criteria/chain.rb', line 15 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.
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def consistent_read @consistent_read end |
#index=(value) ⇒ Object
Sets the attribute index
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def index=(value) @index = value end |
#limit(limit) ⇒ Object
Returns the value of attribute limit.
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def limit @limit end |
#query ⇒ Object
Returns the value of attribute query.
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def query @query end |
#source ⇒ Object
Returns the value of attribute source.
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def source @source end |
#start(start) ⇒ Object
Returns the value of attribute start.
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def start @start end |
#values ⇒ Object
Returns the value of attribute values.
9 10 11 |
# File 'lib/dynamoid/criteria/chain.rb', line 9 def values @values end |
Instance Method Details
#all(opts = {}) ⇒ Object
Returns all the records matching the criteria.
46 47 48 49 |
# File 'lib/dynamoid/criteria/chain.rb', line 46 def all(opts = {}) batch opts[:batch_size] if opts.has_key? :batch_size records end |
#batch(batch_size) ⇒ Object
120 121 122 123 124 |
# File 'lib/dynamoid/criteria/chain.rb', line 120 def batch(batch_size) raise 'Cannot batch calls when using partitioning' if Dynamoid::Config.partitioning? @batch_size = batch_size self end |
#consistent ⇒ Object
38 39 40 41 |
# File 'lib/dynamoid/criteria/chain.rb', line 38 def consistent @consistent_read = true self end |
#consistent_opts ⇒ Object
143 144 145 |
# File 'lib/dynamoid/criteria/chain.rb', line 143 def consistent_opts { :consistent_read => consistent_read } end |
#destroy_all ⇒ Object
Destroys all the records matching the criteria.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/dynamoid/criteria/chain.rb', line 53 def destroy_all ids = [] if range? 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}) elsif index #TODO: test this throughly and find a way to delete all index table records for one source record if index.range_key? results = Dynamoid::Adapter.query(index.table_name, index_query.merge(consistent_opts)) else results = Dynamoid::Adapter.read(index.table_name, index_query[:hash_value], consistent_opts) end results.collect do |hash| ids << hash[source.hash_key.to_sym] index_ranges << hash[source.range_key.to_sym] end unless ids.nil? || ids.empty? ids = ids.to_a if @start ids = ids.drop_while { |id| id != @start.hash_key }.drop(1) index_ranges = index_ranges.drop_while { |range| range != @start.hash_key }.drop(1) unless index_ranges.nil? end if @limit ids = ids.take(@limit) index_ranges = index_ranges.take(@limit) end Dynamoid::Adapter.delete(source.table_name, ids) if index.range_key? Dynamoid::Adapter.delete(index.table_name, ids,{:range_key => index_ranges}) else Dynamoid::Adapter.delete(index.table_name, ids) end end 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.
139 140 141 |
# File 'lib/dynamoid/criteria/chain.rb', line 139 def each(&block) records.each(&block) end |
#first ⇒ Object
Returns the first record matching the criteria.
111 112 113 |
# File 'lib/dynamoid/criteria/chain.rb', line 111 def first limit(1).first end |
#scan_index_forward(scan_index_forward) ⇒ Object
131 132 133 134 |
# File 'lib/dynamoid/criteria/chain.rb', line 131 def scan_index_forward(scan_index_forward) @scan_index_forward = scan_index_forward 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.
33 34 35 36 |
# File 'lib/dynamoid/criteria/chain.rb', line 33 def where(args) args.each {|k, v| query[k.to_sym] = v} self end |