Class: WCC::API::BaseQuery
- Inherits:
-
Object
- Object
- WCC::API::BaseQuery
- Defined in:
- lib/wcc/api/base_query.rb
Constant Summary collapse
- MAX_LIMIT =
50
Instance Attribute Summary collapse
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#paging ⇒ Object
readonly
Returns the value of attribute paging.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #call(scope = self.scope) ⇒ Object
- #default ⇒ Object
- #default_scope ⇒ Object
- #filtered(scope = self.scope) ⇒ Object
-
#initialize(params, scope: default_scope, paging: true) ⇒ BaseQuery
constructor
A new instance of BaseQuery.
- #ordered(scope = self.scope) ⇒ Object
- #paged(scope = self.scope) ⇒ Object
- #permitted_keys ⇒ Object
- #set_defaults ⇒ Object
- #size ⇒ Object
- #total ⇒ Object
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
#filter ⇒ Object
Returns the value of attribute filter.
6 7 8 |
# File 'lib/wcc/api/base_query.rb', line 6 def filter @filter end |
#limit ⇒ Object
Returns the value of attribute limit.
5 6 7 |
# File 'lib/wcc/api/base_query.rb', line 5 def limit @limit end |
#offset ⇒ Object
Returns the value of attribute offset.
5 6 7 |
# File 'lib/wcc/api/base_query.rb', line 5 def offset @offset end |
#paging ⇒ Object (readonly)
Returns the value of attribute paging.
5 6 7 |
# File 'lib/wcc/api/base_query.rb', line 5 def paging @paging end |
#scope ⇒ Object (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 |
#default ⇒ Object
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_scope ⇒ Object
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_keys ⇒ Object
18 19 20 |
# File 'lib/wcc/api/base_query.rb', line 18 def permitted_keys %i[limit offset filter] end |
#set_defaults ⇒ Object
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 |
#size ⇒ Object
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 |
#total ⇒ Object
76 77 78 |
# File 'lib/wcc/api/base_query.rb', line 76 def total @total ||= filtered.count end |