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)


91
92
93
# File 'lib/odata/model/query_proxy.rb', line 91

def count
  query.count
end

#each(&block) ⇒ Object

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



103
104
105
106
107
108
109
# 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?Boolean

Indicates whether the query will return any results

Returns:

  • (Boolean)


97
98
99
# File 'lib/odata/model/query_proxy.rb', line 97

def empty?
  query.count > 0
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
52
53
54
55
# 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) ⇒ self

Specifies the limit for the query.

Parameters:

  • value (to_i)

Returns:

  • (self)


60
61
62
63
# File 'lib/odata/model/query_proxy.rb', line 60

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)


76
77
78
79
# 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) ⇒ self

Selects specific properties to return with query.

Parameters:

  • property_name (to_sym)

Returns:

  • (self)


84
85
86
87
# 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_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.



113
114
115
116
117
118
# 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) ⇒ self

Specifies the number of entities to skip for the query.

Parameters:

  • value (to_i)

Returns:

  • (self)


68
69
70
71
# File 'lib/odata/model/query_proxy.rb', line 68

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