Class: OData::Model::QueryProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/odata/model/query_proxy.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ QueryProxy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiates a new QueryProxy for the supplied OData::Model class.

Parameters:

  • model_class (Class)


13
14
15
16
17
18
# 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

Instance Attribute Details

#last_criteriaObject (readonly)

Last filter criteria set on the query.



8
9
10
# File 'lib/odata/model/query_proxy.rb', line 8

def last_criteria
  @last_criteria
end

Instance Method Details

#countInteger

Provides a faster implementation of Enumerable#count

Returns:

  • (Integer)


87
88
89
# File 'lib/odata/model/query_proxy.rb', line 87

def count
  query.count
end

#each(&block) ⇒ Object

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



93
94
95
96
97
98
99
# File 'lib/odata/model/query_proxy.rb', line 93

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

#is(arguments) ⇒ self

Sets up last criteria with supplied argument sets.

Parameters:

  • arguments (Hash)

Returns:

  • (self)


43
44
45
46
47
48
49
50
51
# 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 = query[property.name].send(operator.to_sym, value)
    query.where(last_criteria)
  end
  self
end

#limit(value) ⇒ self

Specifies the limit for the query.

Parameters:

  • value (to_i)

Returns:

  • (self)


56
57
58
59
# File 'lib/odata/model/query_proxy.rb', line 56

def limit(value)
  query.limit(value.to_i)
  self
end

#order_by(property_name) ⇒ self

Specified the ordering for the query.

Parameters:

  • property_name (to_sym)

Returns:

  • (self)


72
73
74
75
# File 'lib/odata/model/query_proxy.rb', line 72

def order_by(property_name)
  query.order_by(target.property_map[property_name.to_sym])
  self
end

#select(property_name) ⇒ self

Selects specific properties to return with query.

Parameters:

  • property_name (to_sym)

Returns:

  • (self)


80
81
82
83
# File 'lib/odata/model/query_proxy.rb', line 80

def select(property_name)
  query.select(target.property_map[property_name.to_sym])
  self
end

#set_default_selectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

By default we only select the properties defined on the Model.



103
104
105
106
107
108
# File 'lib/odata/model/query_proxy.rb', line 103

def set_default_select
  if target.odata_config[:limit_default_selection]
    query.select(target.property_map.values)
  end
  self
end

#skip(value) ⇒ self

Specifies the number of entities to skip for the query.

Parameters:

  • value (to_i)

Returns:

  • (self)


64
65
66
67
# File 'lib/odata/model/query_proxy.rb', line 64

def skip(value)
  query.skip(value.to_i)
  self
end

#where(argument) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets up a new criteria for filters for the given property name.

Parameters:

  • argument (Hash, to_sym)

Returns:

  • (self)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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