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.
-
#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) ⇒ 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) ⇒ BaseQuery
Returns a new instance of BaseQuery.
25 26 27 28 29 30 31 |
# File 'lib/wcc/api/base_query.rb', line 25 def initialize(params, scope: default_scope) @scope = scope 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 |
#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
33 34 35 36 37 38 39 |
# File 'lib/wcc/api/base_query.rb', line 33 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
51 52 53 |
# File 'lib/wcc/api/base_query.rb', line 51 def filtered(scope=self.scope) scope end |
#ordered(scope = self.scope) ⇒ Object
47 48 49 |
# File 'lib/wcc/api/base_query.rb', line 47 def ordered(scope=self.scope) scope end |
#paged(scope = self.scope) ⇒ Object
41 42 43 44 45 |
# File 'lib/wcc/api/base_query.rb', line 41 def paged(scope=self.scope) scope .limit(limit) .offset(offset) 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
55 56 57 58 59 |
# File 'lib/wcc/api/base_query.rb', line 55 def set_defaults default.each do |key, value| public_send("#{key}=", value) end end |
#size ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/wcc/api/base_query.rb', line 74 def size if total - offset < limit total - offset else limit end end |
#total ⇒ Object
70 71 72 |
# File 'lib/wcc/api/base_query.rb', line 70 def total @total ||= filtered.count end |