Class: OData::AbstractQuery::Segments::NavigationPropertySegment

Inherits:
EntityTypeAndKeyValuesSegment show all
Defined in:
lib/o_data/abstract_query/segments/navigation_property_segment.rb

Instance Attribute Summary collapse

Attributes inherited from EntityTypeAndKeyValuesSegment

#key_values

Attributes inherited from EntityTypeSegment

#entity_type

Attributes inherited from OData::AbstractQuery::Segment

#query

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityTypeAndKeyValuesSegment

extract_schema_object_name_and_key_values_and_keys, key?, #key?, #key_property_value, key_property_value_for, remove_quotes, sanitize_key_values_and_keys_for!

Methods inherited from EntityTypeSegment

#valid?

Methods included from Countable

included

Methods inherited from OData::AbstractQuery::Segment

#can_follow?, #inspect, segment_name, #valid?

Constructor Details

#initialize(query, from_entity_type, to_entity_type, navigation_property, key_values = {}) ⇒ NavigationPropertySegment

Returns a new instance of NavigationPropertySegment.



37
38
39
40
41
42
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 37

def initialize(query, from_entity_type, to_entity_type, navigation_property, key_values = {})
  @from_entity_type = from_entity_type
  @navigation_property = navigation_property

  super(query, to_entity_type, key_values)
end

Instance Attribute Details

Returns the value of attribute navigation_property.



35
36
37
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 35

def navigation_property
  @navigation_property
end

Class Method Details

.can_follow?(anOtherSegment) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 44

def self.can_follow?(anOtherSegment)
  if anOtherSegment.is_a?(Class)
    anOtherSegment == CollectionSegment || anOtherSegment == NavigationPropertySegment
  else
    (anOtherSegment.is_a?(CollectionSegment) || anOtherSegment.is_a?(NavigationPropertySegment)) && !anOtherSegment.countable?
  end
end

.parse!(query, str) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 5

def self.parse!(query, str)
  return nil if query.segments.empty?
  return nil unless query.segments.last.respond_to?(:entity_type)
  entity_type = query.segments.last.entity_type
  return nil if entity_type.blank?

  schema_object_name, key_values, keys = extract_schema_object_name_and_key_values_and_keys(str)
  return nil if schema_object_name.blank?

  navigation_property = entity_type.navigation_properties.find { |np| np.name == schema_object_name }
  return nil if navigation_property.blank?

  if navigation_property.to_end.polymorphic?
    raise OData::AbstractQuery::Errors::AbstractQueryKeyValueException.new(query, key_values.keys.first, key_values.values.first) unless key_values.empty?
    raise OData::AbstractQuery::Errors::AbstractQueryKeyValueException.new(query, '$polymorphic#Key', keys.first) unless keys.empty?

    query.Segment(self, entity_type, navigation_property.to_end.return_type, navigation_property, {})
  else
    sanitized_key_values = sanitize_key_values_and_keys_for!(query, navigation_property.to_end.entity_type, key_values, keys)
    
    unless sanitized_key_values.empty?
      raise OData::AbstractQuery::Errors::AbstractQueryKeyValueException.new(query, sanitized_key_values.keys.first, sanitized_key_values.values.first) unless navigation_property.to_end.multiple?
    end

    query.Segment(self, entity_type, navigation_property.to_end.entity_type, navigation_property, sanitized_key_values)
  end
end

Instance Method Details

#countable?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 52

def countable?
  multiple? && super
end

#execute!(acc) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 56

def execute!(acc)
  [acc].flatten.compact.collect { |one|
    if key?
      @navigation_property.find_one(one, key_property_value)
    else
      @navigation_property.find_all(one, @key_values)
    end
  }.first
end

#multiple?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 66

def multiple?
  @navigation_property.to_end.multiple?
end

#valueObject



70
71
72
73
74
75
76
77
78
# File 'lib/o_data/abstract_query/segments/navigation_property_segment.rb', line 70

def value
  if self.key_values.blank? || !multiple?
    @navigation_property.name
  elsif key?
    @navigation_property.name + '(' + key_property_value.to_s + ')'
  else
    @navigation_property.name + '(' + self.key_values.collect { |key, value| "#{key}=#{value}" }.join(',') + ')'
  end
end