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.
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
-
#consistent_read ⇒ Object
Returns the value of attribute consistent_read.
-
#hash_key ⇒ Object
readonly
Returns the value of attribute hash_key.
-
#index_name ⇒ Object
readonly
Returns the value of attribute index_name.
-
#query ⇒ Object
Returns the value of attribute query.
-
#range_key ⇒ Object
readonly
Returns the value of attribute range_key.
-
#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
-
#delete_all ⇒ Object
(also: #destroy_all)
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.
-
#initialize(source) ⇒ Chain
constructor
Create a new criteria chain.
-
#last ⇒ Object
Returns the last fetched record matched the criteria Enumerable doesn’t implement ‘last`, only `first` So we have to implement it ourselves.
-
#record_limit(limit) ⇒ Object
The record limit is the limit of evaluated records returned by the query or scan.
- #scan_index_forward(scan_index_forward) ⇒ Object
-
#scan_limit(limit) ⇒ Object
The scan limit which is the limit of records that DynamoDB will internally query or scan.
- #start(start) ⇒ Object
-
#where(args) ⇒ Object
The workhorse method of the criteria chain.
Constructor Details
#initialize(source) ⇒ Chain
Create a new criteria chain.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dynamoid/criteria/chain.rb', line 16 def initialize(source) @query = {} @source = source @consistent_read = false @scan_index_forward = true # Honor STI and :type field if it presents if @source.attributes.key?(:type) @query[:'type.in'] = @source.deep_subclasses.map(&:name) << @source.name end end |
Instance Attribute Details
#consistent_read ⇒ Object
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_key ⇒ Object (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_name ⇒ Object (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 |
#query ⇒ Object
Returns the value of attribute query.
10 11 12 |
# File 'lib/dynamoid/criteria/chain.rb', line 10 def query @query end |
#range_key ⇒ Object (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 |
#source ⇒ Object
Returns the value of attribute source.
10 11 12 |
# File 'lib/dynamoid/criteria/chain.rb', line 10 def source @source end |
#values ⇒ Object
Returns the value of attribute values.
10 11 12 |
# File 'lib/dynamoid/criteria/chain.rb', line 10 def values @values end |
Instance Method Details
#all ⇒ Object
Returns all the records matching the criteria.
59 60 61 |
# File 'lib/dynamoid/criteria/chain.rb', line 59 def all records end |
#batch(batch_size) ⇒ Object
111 112 113 114 |
# File 'lib/dynamoid/criteria/chain.rb', line 111 def batch(batch_size) @batch_size = batch_size self end |
#consistent ⇒ Object
51 52 53 54 |
# File 'lib/dynamoid/criteria/chain.rb', line 51 def consistent @consistent_read = true self end |
#delete_all ⇒ Object Also known as: destroy_all
Destroys all the records matching the criteria.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dynamoid/criteria/chain.rb', line 73 def delete_all ids = [] ranges = [] if key_present? 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] if source.range_key end Dynamoid.adapter.delete(source.table_name, ids, range_key: ranges.presence) else Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).collect do |hash| ids << hash[source.hash_key.to_sym] ranges << hash[source.range_key.to_sym] if source.range_key end Dynamoid.adapter.delete(source.table_name, ids, range_key: ranges.presence) end end |
#each(&block) ⇒ Object
Allows you to use the results of a search as an enumerable over the results found.
129 130 131 |
# File 'lib/dynamoid/criteria/chain.rb', line 129 def each(&block) records.each(&block) end |
#last ⇒ Object
Returns the last fetched record matched the criteria Enumerable doesn’t implement ‘last`, only `first` So we have to implement it ourselves
67 68 69 |
# File 'lib/dynamoid/criteria/chain.rb', line 67 def last all.to_a.last end |
#record_limit(limit) ⇒ Object
The record limit is the limit of evaluated records returned by the query or scan.
97 98 99 100 |
# File 'lib/dynamoid/criteria/chain.rb', line 97 def record_limit(limit) @record_limit = limit self end |
#scan_index_forward(scan_index_forward) ⇒ Object
121 122 123 124 |
# File 'lib/dynamoid/criteria/chain.rb', line 121 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.
106 107 108 109 |
# File 'lib/dynamoid/criteria/chain.rb', line 106 def scan_limit(limit) @scan_limit = limit self end |
#start(start) ⇒ Object
116 117 118 119 |
# File 'lib/dynamoid/criteria/chain.rb', line 116 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.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dynamoid/criteria/chain.rb', line 39 def where(args) args.each do |k, v| sym = k.to_sym query[sym] = if ( = source.attributes[sym]) && (type = [:type]) && TYPES_TO_DUMP_FOR_QUERY.include?(type) source.dump_field(v, ) else v end end self end |