Class: OData::QueryBuilder
- Inherits:
-
Object
- Object
- OData::QueryBuilder
- Defined in:
- lib/ruby_odata/query_builder.rb
Overview
The Categories method would return a QueryBuilder
Instance Attribute Summary collapse
-
#additional_params ⇒ Object
Returns the value of attribute additional_params.
Instance Method Summary collapse
-
#count ⇒ Object
Used to return a count of objects instead of the objects themselves.
-
#expand(path) ⇒ Object
Used to eagerly-load data for nested objects, for example, obtaining a Category for a Product within one call to the server.
-
#filter(filter) ⇒ Object
Used to filter data being returned.
-
#initialize(root, additional_params = {}) ⇒ QueryBuilder
constructor
Creates a new instance of the QueryBuilder class.
-
#links(navigation_property) ⇒ Object
Used to return links instead of actual objects.
-
#navigate(navigation_property) ⇒ Object
Used to navigate to a child collection, typically used to filter or perform a similar function against the children.
-
#order_by(order_by) ⇒ Object
Used to order the data being returned.
-
#query ⇒ Object
Builds the query URI (path, not including root) incorporating expands, filters, etc.
-
#select(*fields) ⇒ Object
Used to customize the properties that are returned for "ad-hoc" queries.
-
#skip(num) ⇒ Object
Used to skip a number of records This is typically used for paging, where it would be used along with the
topmethod. -
#top(num) ⇒ Object
Used to take only the top X records This is typically used for paging, where it would be used along with the
skipmethod.
Constructor Details
#initialize(root, additional_params = {}) ⇒ QueryBuilder
Creates a new instance of the QueryBuilder class
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby_odata/query_builder.rb', line 15 def initialize(root, additional_params = {}) @root = Helpers.uri_escape(root.to_s) @expands = [] @filters = [] @order_bys = [] @navigation_paths = [] @select = [] @skip = nil @top = nil @count = nil @links_navigation_property = nil @additional_params = additional_params end |
Instance Attribute Details
#additional_params ⇒ Object
Returns the value of attribute additional_params.
9 10 11 |
# File 'lib/ruby_odata/query_builder.rb', line 9 def additional_params @additional_params end |
Instance Method Details
#count ⇒ Object
Used to return a count of objects instead of the objects themselves
119 120 121 122 123 124 125 |
# File 'lib/ruby_odata/query_builder.rb', line 119 def count raise OData::NotSupportedError.new("You cannot call both the `links` method and the `count` method in the same query.") if @links_navigation_property raise OData::NotSupportedError.new("You cannot call both the `select` method and the `count` method in the same query.") unless @select.empty? @count = true self end |
#expand(path) ⇒ Object
Used to eagerly-load data for nested objects, for example, obtaining a Category for a Product within one call to the server
40 41 42 43 |
# File 'lib/ruby_odata/query_builder.rb', line 40 def (path) @expands << path self end |
#filter(filter) ⇒ Object
Used to filter data being returned
52 53 54 55 |
# File 'lib/ruby_odata/query_builder.rb', line 52 def filter(filter) @filters << CGI.escape(filter) self end |
#links(navigation_property) ⇒ Object
Used to return links instead of actual objects
104 105 106 107 108 109 |
# File 'lib/ruby_odata/query_builder.rb', line 104 def links() raise OData::NotSupportedError.new("You cannot call both the `links` method and the `count` method in the same query.") if @count raise OData::NotSupportedError.new("You cannot call both the `links` method and the `select` method in the same query.") unless @select.empty? @links_navigation_property = self end |
#navigate(navigation_property) ⇒ Object
Used to navigate to a child collection, typically used to filter or perform a similar function against the children
133 134 135 136 |
# File 'lib/ruby_odata/query_builder.rb', line 133 def navigate() @navigation_paths << Helpers.uri_escape() self end |
#order_by(order_by) ⇒ Object
Used to order the data being returned
64 65 66 67 |
# File 'lib/ruby_odata/query_builder.rb', line 64 def order_by(order_by) @order_bys << CGI.escape(order_by) self end |
#query ⇒ Object
Builds the query URI (path, not including root) incorporating expands, filters, etc. This is used internally when the execute method is called on the service
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ruby_odata/query_builder.rb', line 162 def query q = @root.clone # Navigation paths come first in the query q << "/" + @navigation_paths.join("/") unless @navigation_paths.empty? # Handle links queries, this isn't just a standard query option if @links_navigation_property q << "/$links/#{@links_navigation_property}" end # Handle count queries, this isn't just a standard query option if @count q << "/$count" end = [] << "$select=#{@select.join(',')}" unless @select.empty? << "$expand=#{@expands.join(',')}" unless @expands.empty? << "$filter=#{@filters.join('+and+')}" unless @filters.empty? << "$orderby=#{@order_bys.join(',')}" unless @order_bys.empty? << "$skip=#{@skip}" unless @skip.nil? << "$top=#{@top}" unless @top.nil? << @additional_params.to_query unless @additional_params.empty? if !.empty? q << "?" q << .join('&') end return q end |
#select(*fields) ⇒ Object
Used to customize the properties that are returned for "ad-hoc" queries
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ruby_odata/query_builder.rb', line 144 def select(*fields) raise OData::NotSupportedError.new("You cannot call both the `links` method and the `select` method in the same query.") if @links_navigation_property raise OData::NotSupportedError.new("You cannot call both the `count` method and the `select` method in the same query.") if @count @select |= fields = fields.find_all { |f| /\// =~ f } .each do |e| parts = e.split '/' @expands |= [parts[0...-1].join('/')] end self end |
#skip(num) ⇒ Object
Used to skip a number of records
This is typically used for paging, where it would be used along with the top method.
77 78 79 80 |
# File 'lib/ruby_odata/query_builder.rb', line 77 def skip(num) @skip = num self end |
#top(num) ⇒ Object
Used to take only the top X records
This is typically used for paging, where it would be used along with the skip method.
90 91 92 93 |
# File 'lib/ruby_odata/query_builder.rb', line 90 def top(num) @top = num self end |