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.



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

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

Instance Attribute Details

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#pagingObject (readonly)

Returns the value of attribute paging.



3
4
5
# File 'lib/wcc/api/base_query.rb', line 3

def paging
  @paging
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/wcc/api/base_query.rb', line 3

def scope
  @scope
end

Instance Method Details

#call(scope = self.scope) ⇒ Object



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

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

#defaultObject



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

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

#default_scopeObject

Raises:

  • (NotImplementedError)


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

def default_scope
  raise NotImplementedError
end

#filtered(scope = self.scope) ⇒ Object



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

def filtered(scope=self.scope)
  scope
end

#ordered(scope = self.scope) ⇒ Object



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

def ordered(scope=self.scope)
  scope
end

#paged(scope = self.scope) ⇒ Object



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

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

#permitted_keysObject



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

def permitted_keys
  %i(limit offset filter)
end

#set_defaultsObject



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

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

#sizeObject



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

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

#totalObject



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

def total
  @total ||= filtered.count
end