Class: MudisQL::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/mudis-ql/scope.rb

Overview

Scope provides a chainable DSL for querying mudis cache data

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#storeObject (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

#allArray<Hash>

Execute the query and return all matching records

Returns:

  • (Array<Hash>)

    array of 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

#cloneObject

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

#countInteger

Count matching records

Returns:

  • (Integer)

    number of 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

Returns:

  • (Boolean)

    true if at least one record matches



107
108
109
# File 'lib/mudis-ql/scope.rb', line 107

def exists?
  count > 0
end

#firstHash?

Execute query and return first result

Returns:

  • (Hash, nil)

    first matching record or nil



86
87
88
# File 'lib/mudis-ql/scope.rb', line 86

def first
  limit(1).all.first
end

#lastHash?

Execute query and return last result

Returns:

  • (Hash, nil)

    last matching record or nil



93
94
95
# File 'lib/mudis-ql/scope.rb', line 93

def last
  all.last
end

#limit(value) ⇒ Scope

Limit the number of results

Parameters:

  • value (Integer)

    maximum number of results

Returns:

  • (Scope)

    self for chaining



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

Parameters:

  • value (Integer)

    number of results to skip

Returns:

  • (Scope)

    self for chaining



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

Parameters:

  • field (Symbol, String)

    the field to order by

  • direction (Symbol) (defaults to: :asc)

    :asc or :desc

Returns:

  • (Scope)

    self for chaining



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

Parameters:

  • fields (Array<Symbol, String>)

    fields to extract

Returns:

  • (Array)

    array of field values or arrays of values



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

Examples:

query.where(status: "active")
query.where(age: ->(v) { v > 18 })
query.where(name: /^A/)

Parameters:

  • conditions (Hash)

    hash of field => value or field => proc

Returns:

  • (Scope)

    self for chaining



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