Class: MudisQL::Scope
- Inherits:
-
Object
- Object
- MudisQL::Scope
- Defined in:
- lib/mudis-ql/scope.rb
Overview
Scope provides a chainable DSL for querying mudis cache data
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#all ⇒ Array<Hash>
Execute the query and return all matching records.
-
#clone ⇒ Object
Create a new independent scope instance.
-
#count ⇒ Integer
Count matching records.
-
#exists? ⇒ Boolean
Check if any records match.
-
#first ⇒ Hash?
Execute query and return first result.
-
#initialize(store) ⇒ Scope
constructor
A new instance of Scope.
-
#last ⇒ Hash?
Execute query and return last result.
-
#limit(value) ⇒ Scope
Limit the number of results.
-
#offset(value) ⇒ Scope
Skip the first N results.
-
#order(field, direction = :asc) ⇒ Scope
Order results by a field.
-
#pluck(*fields) ⇒ Array
Pluck specific fields from results.
-
#where(conditions) ⇒ Scope
Filter records by conditions.
Constructor Details
#initialize(store) ⇒ Scope
Returns a new instance of Scope.
8 9 10 11 12 13 14 15 |
# File 'lib/mudis-ql/scope.rb', line 8 def initialize(store) @store = store @conditions = [] @order_by = nil @order_direction = :asc @limit_value = nil @offset_value = 0 end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
6 7 8 |
# File 'lib/mudis-ql/scope.rb', line 6 def store @store end |
Instance Method Details
#all ⇒ Array<Hash>
Execute the query and return all matching records
75 76 77 78 79 80 81 |
# File 'lib/mudis-ql/scope.rb', line 75 def all results = store.all results = apply_conditions(results) results = apply_order(results) results = apply_pagination(results) results end |
#clone ⇒ Object
Create a new independent scope instance
18 19 20 21 22 23 24 25 26 |
# File 'lib/mudis-ql/scope.rb', line 18 def clone cloned = self.class.new(@store) cloned.instance_variable_set(:@conditions, @conditions.dup) cloned.instance_variable_set(:@order_by, @order_by) cloned.instance_variable_set(:@order_direction, @order_direction) cloned.instance_variable_set(:@limit_value, @limit_value) cloned.instance_variable_set(:@offset_value, @offset_value) cloned end |
#count ⇒ Integer
Count matching records
100 101 102 |
# File 'lib/mudis-ql/scope.rb', line 100 def count apply_conditions(store.all).size end |
#exists? ⇒ Boolean
Check if any records match
107 108 109 |
# File 'lib/mudis-ql/scope.rb', line 107 def exists? count > 0 end |
#first ⇒ Hash?
Execute query and return first result
86 87 88 |
# File 'lib/mudis-ql/scope.rb', line 86 def first limit(1).all.first end |
#last ⇒ Hash?
Execute query and return last result
93 94 95 |
# File 'lib/mudis-ql/scope.rb', line 93 def last all.last end |
#limit(value) ⇒ Scope
Limit the number of results
58 59 60 61 |
# File 'lib/mudis-ql/scope.rb', line 58 def limit(value) @limit_value = value self end |
#offset(value) ⇒ Scope
Skip the first N results
67 68 69 70 |
# File 'lib/mudis-ql/scope.rb', line 67 def offset(value) @offset_value = value self end |
#order(field, direction = :asc) ⇒ Scope
Order results by a field
48 49 50 51 52 |
# File 'lib/mudis-ql/scope.rb', line 48 def order(field, direction = :asc) @order_by = field.to_s @order_direction = direction self end |
#pluck(*fields) ⇒ Array
Pluck specific fields from results
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mudis-ql/scope.rb', line 115 def pluck(*fields) fields = fields.map(&:to_s) results = all if fields.size == 1 results.map { |record| record[fields.first] } else results.map { |record| fields.map { |f| record[f] } } end end |
#where(conditions) ⇒ Scope
Filter records by conditions
37 38 39 40 41 |
# File 'lib/mudis-ql/scope.rb', line 37 def where(conditions) @conditions = @conditions.dup unless @conditions.frozen? @conditions << conditions self end |