Class: Munson::QueryBuilder
- Inherits:
-
Object
- Object
- Munson::QueryBuilder
- Defined in:
- lib/munson/query_builder.rb
Defined Under Namespace
Classes: AgentNotSet, PaginatorNotSet, UnsupportedSortDirectionError
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#paginator ⇒ Object
readonly
Returns the value of attribute paginator.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
Instance Method Summary collapse
-
#fetch ⇒ Array
Fetches resources using Agent.
-
#fields(*args) ⇒ Object
Hash resouce_name: [array of attribs].
- #filter(*args) ⇒ Object
-
#includes(*args) ⇒ Munson::QueryBuilder
Chainably include related resources.
-
#initialize(paginator: nil, agent: nil) ⇒ Type
constructor
Description of method.
-
#page(opts = {}) ⇒ Class?
Paginator proxy.
- #paging? ⇒ Boolean
-
#sort(*args) ⇒ Munson::QueryBuilder
Chainably sort results.
- #to_params ⇒ Object
-
#to_query_string ⇒ String
Query as a query string.
- #to_s ⇒ Object
Constructor Details
#initialize(paginator: nil, agent: nil) ⇒ Type
Description of method
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/munson/query_builder.rb', line 15 def initialize(paginator: nil, agent: nil) @paginator = paginator @agent = agent @query = { include: [], fields: [], filter: [], sort: [] } end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
5 6 7 |
# File 'lib/munson/query_builder.rb', line 5 def agent @agent end |
#paginator ⇒ Object (readonly)
Returns the value of attribute paginator.
4 5 6 |
# File 'lib/munson/query_builder.rb', line 4 def paginator @paginator end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/munson/query_builder.rb', line 3 def query @query end |
Class Method Details
.fields(*args) ⇒ Object
141 142 143 |
# File 'lib/munson/query_builder.rb', line 141 def self.fields(*args) new.fields(*args) end |
.filter(*args) ⇒ Object
145 146 147 |
# File 'lib/munson/query_builder.rb', line 145 def self.filter(*args) new.filter(*args) end |
.includes(*args) ⇒ Object
133 134 135 |
# File 'lib/munson/query_builder.rb', line 133 def self.includes(*args) new.includes(*args) end |
.sort(*args) ⇒ Object
137 138 139 |
# File 'lib/munson/query_builder.rb', line 137 def self.sort(*args) new.sort(*args) end |
Instance Method Details
#fetch ⇒ Array
Fetches resources using Agent
51 52 53 54 55 56 57 58 59 |
# File 'lib/munson/query_builder.rb', line 51 def fetch if @agent response = @agent.get(params: to_params) resources = ResponseMapper.new(response).resources Collection.new(resources) else raise AgentNotSet, "Agent was not set. QueryBuilder#new(agent:)" end end |
#fields(*args) ⇒ Object
Hash resouce_name: [array of attribs]
123 124 125 126 |
# File 'lib/munson/query_builder.rb', line 123 def fields(*args) @query[:fields] += args self end |
#filter(*args) ⇒ Object
128 129 130 131 |
# File 'lib/munson/query_builder.rb', line 128 def filter(*args) @query[:filter] += args self end |
#includes(*args) ⇒ Munson::QueryBuilder
Chainably include related resources.
92 93 94 95 |
# File 'lib/munson/query_builder.rb', line 92 def includes(*args) @query[:include] += args self end |
#page(opts = {}) ⇒ Class?
Paginator proxy
68 69 70 71 72 73 74 75 |
# File 'lib/munson/query_builder.rb', line 68 def page(opts={}) if paging? paginator.set(opts) self else raise PaginatorNotSet, "Paginator was not set. QueryBuilder#new(paginator:)" end end |
#paging? ⇒ Boolean
61 62 63 |
# File 'lib/munson/query_builder.rb', line 61 def paging? !!paginator end |
#sort(*args) ⇒ Munson::QueryBuilder
Note:
Default order is ascending
Chainably sort results
116 117 118 119 120 |
# File 'lib/munson/query_builder.rb', line 116 def sort(*args) validate_sort_args(args.select{|arg| arg.is_a?(Hash)}) @query[:sort] += args self end |
#to_params ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/munson/query_builder.rb', line 36 def to_params str = {} str[:filter] = filter_to_query_value unless @query[:filter].empty? str[:fields] = fields_to_query_value unless @query[:fields].empty? str[:include] = includes_to_query_value unless @query[:include].empty? str[:sort] = sort_to_query_value unless @query[:sort].empty? str.merge!(paginator.to_params) if paginator str end |
#to_query_string ⇒ String
Returns query as a query string.
28 29 30 |
# File 'lib/munson/query_builder.rb', line 28 def to_query_string Faraday::Utils.build_nested_query(to_params) end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/munson/query_builder.rb', line 32 def to_s to_query_string end |