Class: OData::AbstractQuery::Segments::EntityTypeAndKeyValuesSegment

Inherits:
EntityTypeSegment show all
Defined in:
lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb

Direct Known Subclasses

CollectionSegment, NavigationPropertySegment

Instance Attribute Summary collapse

Attributes inherited from EntityTypeSegment

#entity_type

Attributes inherited from OData::AbstractQuery::Segment

#query

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityTypeSegment

can_follow?, #valid?

Methods included from Countable

included

Methods inherited from OData::AbstractQuery::Segment

#can_follow?, can_follow?, #inspect, parse!, segment_name, #valid?

Constructor Details

#initialize(query, entity_type, key_values = {}) ⇒ EntityTypeAndKeyValuesSegment

Returns a new instance of EntityTypeAndKeyValuesSegment.



58
59
60
61
62
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 58

def initialize(query, entity_type, key_values = {})
  @key_values = key_values

  super(query, entity_type)
end

Instance Attribute Details

#key_valuesObject (readonly)

Returns the value of attribute key_values.



56
57
58
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 56

def key_values
  @key_values
end

Class Method Details

.extract_schema_object_name_and_key_values_and_keys(str) ⇒ Object



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/entity_type_and_key_values_segment.rb', line 9

def self.extract_schema_object_name_and_key_values_and_keys(str)
  if md1 = str.to_s.match(/^([^\(]+)(?:\(([^\)]+)\)|\(\s*\))?$/)
    schema_object_name = md1[1]
    key_values = {}
    keys = []

    if key_values_string = md1[2]
      key_values_string.split(/\s*,\s*/).each do |key_value_pair|
        key, value = key_value_pair.split('=', 2)

        if value
          key_values[key.to_sym] = remove_quotes(value)
        else
          keys << remove_quotes(key)
        end
      end
    end

    [schema_object_name, key_values, keys]
  else
    nil
  end
end

.key?(entity_type, key_values = {}) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 98

def self.key?(entity_type, key_values = {})
  !!key_property_value_for(entity_type, key_values)
end

.key_property_value_for(entity_type, key_values = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 102

def self.key_property_value_for(entity_type, key_values = {})
  return nil if entity_type.blank?

  return nil if key_values.blank?
  return nil unless key_values.size == 1

  key_property = entity_type.key_property
  return nil if key_property.blank?

  key_values[key_property.name.to_sym]
end

.remove_quotes(str) ⇒ Object



5
6
7
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 5

def self.remove_quotes(str)
  str.to_s.sub(/^\'(.*?)\'$/, '\1')
end

.sanitize_key_values_and_keys_for!(query, entity_type, key_values = {}, keys = []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 33

def self.sanitize_key_values_and_keys_for!(query, entity_type, key_values = {}, keys = [])
  key_property_name = entity_type.key_property.name

  sanitized_key_values = key_values.inject({}) { |acc, key_value_pair|
    key, value = key_value_pair

    property = entity_type.properties.find { |p| p.name == key.to_s }
    raise OData::AbstractQuery::Errors::PropertyNotFound.new(query, key) if property.blank?
    
    raise OData::AbstractQuery::Errors::AbstractQueryKeyValueException.new(query, key, value) unless acc[key.to_sym].blank?

    acc[property.name.to_sym] = value
    acc
  }

  keys.inject(sanitized_key_values) { |acc, key_value| 
    raise OData::AbstractQuery::Errors::AbstractQueryKeyValueException.new(query, key_property_name, key_value) unless acc[key_property_name.to_sym].blank?
    
    acc[key_property_name.to_sym] = key_value
    acc
  }
end

Instance Method Details

#countable?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 64

def countable?
  !key?
end

#execute!(acc) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 68

def execute!(acc)
  return [] if self.entity_type.blank?

  if key?
    self.entity_type.find_one(key_property_value)
  else
    self.entity_type.find_all(@key_values)
  end
end

#key?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 78

def key?
  !self.entity_type.blank? && self.class.key?(self.entity_type, @key_values)
end

#key_property_valueObject



82
83
84
85
86
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 82

def key_property_value
  return nil if self.entity_type.blank?

  self.class.key_property_value_for(self.entity_type, @key_values)
end

#valueObject



88
89
90
91
92
93
94
95
96
# File 'lib/o_data/abstract_query/segments/entity_type_and_key_values_segment.rb', line 88

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