Class: WCC::API::BaseQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/api/base_query.rb

Constant Summary collapse

MAX_LIMIT =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, scope: default_scope, paging: true) ⇒ BaseQuery

Returns a new instance of BaseQuery.


26
27
28
29
30
31
32
33
# File 'lib/wcc/api/base_query.rb', line 26

def initialize(params, scope: default_scope, paging: true)
  @scope = scope
  @paging = paging
  set_defaults
  permitted_keys.each do |key|
    public_send("#{key}=", params[key]) if params.key?(key)
  end
end

Instance Attribute Details

#filterObject

Returns the value of attribute filter


6
7
8
# File 'lib/wcc/api/base_query.rb', line 6

def filter
  @filter
end

#limitObject

Returns the value of attribute limit


5
6
7
# File 'lib/wcc/api/base_query.rb', line 5

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset


5
6
7
# File 'lib/wcc/api/base_query.rb', line 5

def offset
  @offset
end

#pagingObject (readonly)

Returns the value of attribute paging


5
6
7
# File 'lib/wcc/api/base_query.rb', line 5

def paging
  @paging
end

#scopeObject (readonly)

Returns the value of attribute scope


5
6
7
# File 'lib/wcc/api/base_query.rb', line 5

def scope
  @scope
end

Instance Method Details

#call(scope = self.scope) ⇒ Object


35
36
37
38
39
40
41
# File 'lib/wcc/api/base_query.rb', line 35

def call(scope = self.scope)
  scope = scope.dup
  scope = paged(scope)
  scope = ordered(scope)
  scope = filtered(scope)
  scope
end

#defaultObject


10
11
12
13
14
15
16
# File 'lib/wcc/api/base_query.rb', line 10

def default
  {
    limit: 20,
    offset: 0,
    filter: {}
  }
end

#default_scopeObject

Raises:

  • (NotImplementedError)

22
23
24
# File 'lib/wcc/api/base_query.rb', line 22

def default_scope
  raise NotImplementedError
end

#filtered(scope = self.scope) ⇒ Object


57
58
59
# File 'lib/wcc/api/base_query.rb', line 57

def filtered(scope = self.scope)
  scope
end

#ordered(scope = self.scope) ⇒ Object


53
54
55
# File 'lib/wcc/api/base_query.rb', line 53

def ordered(scope = self.scope)
  scope
end

#paged(scope = self.scope) ⇒ Object


43
44
45
46
47
48
49
50
51
# File 'lib/wcc/api/base_query.rb', line 43

def paged(scope = self.scope)
  if paging
    scope
      .limit(limit)
      .offset(offset)
  else
    scope
  end
end

#permitted_keysObject


18
19
20
# File 'lib/wcc/api/base_query.rb', line 18

def permitted_keys
  %i[limit offset filter]
end

#set_defaultsObject


61
62
63
64
65
# File 'lib/wcc/api/base_query.rb', line 61

def set_defaults
  default.each do |key, value|
    public_send("#{key}=", value)
  end
end

#sizeObject


80
81
82
83
84
85
86
# File 'lib/wcc/api/base_query.rb', line 80

def size
  if total - offset < limit
    total - offset
  else
    limit
  end
end

#totalObject


76
77
78
# File 'lib/wcc/api/base_query.rb', line 76

def total
  @total ||= filtered.count
end