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.
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
#filter ⇒ Object
Returns the value of attribute filter.
5 6 7 |
# File 'lib/wcc/api/base_query.rb', line 5 def filter @filter end |
#limit ⇒ Object
Returns the value of attribute limit.
4 5 6 |
# File 'lib/wcc/api/base_query.rb', line 4 def limit @limit end |
#offset ⇒ Object
Returns the value of attribute offset.
4 5 6 |
# File 'lib/wcc/api/base_query.rb', line 4 def offset @offset end |
#paging ⇒ Object (readonly)
Returns the value of attribute paging.
3 4 5 |
# File 'lib/wcc/api/base_query.rb', line 3 def paging @paging end |
#scope ⇒ Object (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 |
#default ⇒ Object
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_scope ⇒ Object
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_keys ⇒ Object
17 18 19 |
# File 'lib/wcc/api/base_query.rb', line 17 def permitted_keys %i(limit offset filter) end |
#set_defaults ⇒ Object
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 |
#size ⇒ Object
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 |
#total ⇒ Object
75 76 77 |
# File 'lib/wcc/api/base_query.rb', line 75 def total @total ||= filtered.count end |