class OData::Model::QueryProxy

Provides the proxy between OData::Query and OData::Model.

Attributes

last_criteria[R]

Last filter criteria set on the query.

query[R]
target[R]

Public Class Methods

new(model_class) click to toggle source

Instantiates a new QueryProxy for the supplied OData::Model class. @param model_class [Class] @api private

# File lib/odata/model/query_proxy.rb, line 13
def initialize(model_class)
  @target = model_class
  @query = target.odata_entity_set.query
  @last_criteria = nil
  set_default_select
end

Public Instance Methods

count() click to toggle source

Provides a faster implementation of Enumerable#count @return [Integer]

# File lib/odata/model/query_proxy.rb, line 91
def count
  query.count
end
each() { |model| ... } click to toggle source

Executes the query and returns each instance of the target model in turn.

# File lib/odata/model/query_proxy.rb, line 103
def each(&block)
  query.execute.each do |entity|
    model = target.new
    model.instance_variable_set(:@odata_entity, entity)
    block_given? ? block.call(model) : yield(model)
  end
end
empty?() click to toggle source

Indicates whether the query will return any results @return [Boolean]

# File lib/odata/model/query_proxy.rb, line 97
def empty?
  query.count > 0
end
is(arguments) click to toggle source

Sets up last criteria with supplied argument sets. @param arguments [Hash] @return [self]

# File lib/odata/model/query_proxy.rb, line 43
def is(arguments)
  raise ArgumentError 'can only accept Hash argument' unless arguments.is_a?(Hash)
  property = last_criteria.property
  arguments.each do |operator, value|
    @last_criteria = if property.respond_to?(:name)
      query[property.name].send(operator.to_sym, value)
    else
      query[property].send(operator.to_sym, value)
    end
    query.where(last_criteria)
  end
  self
end
limit(value) click to toggle source

Specifies the limit for the query. @param value [to_i] @return [self]

# File lib/odata/model/query_proxy.rb, line 60
def limit(value)
  query.limit(value.to_i)
  self
end
order_by(property_name) click to toggle source

Specified the ordering for the query. @param property_name [to_sym] @return [self]

# File lib/odata/model/query_proxy.rb, line 76
def order_by(property_name)
  query.order_by(target.property_map[property_name.to_sym])
  self
end
select(property_name) click to toggle source

Selects specific properties to return with query. @param property_name [to_sym] @return [self]

# File lib/odata/model/query_proxy.rb, line 84
def select(property_name)
  query.select(target.property_map[property_name.to_sym])
  self
end
set_default_select() click to toggle source

By default we only select the properties defined on the Model. @api private

# File lib/odata/model/query_proxy.rb, line 113
def set_default_select
  if target.odata_config[:limit_default_selection]
    query.select(target.property_map.values)
  end
  self
end
skip(value) click to toggle source

Specifies the number of entities to skip for the query. @param value [to_i] @return [self]

# File lib/odata/model/query_proxy.rb, line 68
def skip(value)
  query.skip(value.to_i)
  self
end
where(argument) click to toggle source

Sets up a new criteria for filters for the given property name. @param argument [Hash,to_sym] @return [self] @api private

# File lib/odata/model/query_proxy.rb, line 24
def where(argument)
  if argument.is_a?(Hash)
    argument.each do |property_name, value|
      self.where(property_name).is(eq: value)
    end
  else
    if argument.is_a?(String)
      @last_criteria = query[argument]
    else
      @last_criteria = query[target.property_map[argument.to_sym]]
    end
  end

  self
end