Class: OData::AbstractQuery::Options::OrderbyOption
Instance Attribute Summary collapse
Attributes inherited from BasicOption
#key, #query
Class Method Summary
collapse
Instance Method Summary
collapse
#applies_to?, #option_name
Methods inherited from BasicOption
#inspect, #option_name
Constructor Details
#initialize(query, pairs = []) ⇒ OrderbyOption
Returns a new instance of OrderbyOption.
11
12
13
14
15
|
# File 'lib/o_data/abstract_query/options/orderby_option.rb', line 11
def initialize(query, pairs = [])
@pairs = pairs
super(query, self.class.option_name)
end
|
Instance Attribute Details
#pairs ⇒ Object
Returns the value of attribute pairs.
9
10
11
|
# File 'lib/o_data/abstract_query/options/orderby_option.rb', line 9
def pairs
@pairs
end
|
Class Method Details
.applies_to?(query) ⇒ Boolean
.option_name ⇒ Object
5
6
7
|
# File 'lib/o_data/abstract_query/options/orderby_option.rb', line 5
def self.option_name
'$orderby'
end
|
.parse!(query, key, value = nil) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
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/options/orderby_option.rb', line 22
def self.parse!(query, key, value = nil)
return nil unless key == self.option_name
if query.segments.last.respond_to?(:navigation_property)
navigation_property = query.segments.last.navigation_property
raise OData::AbstractQuery::Errors::InvalidOptionValue.new(query, self.option_name) if navigation_property.to_end.polymorphic?
end
raise OData::AbstractQuery::Errors::InvalidOptionContext.new(query, self) unless query.segments.last.respond_to?(:countable?) && query.segments.last.countable?
if query.segments.last.respond_to?(:entity_type)
entity_type = query.segments.last.entity_type
pairs = value.to_s.split(/\s*,\s*/).collect { |path|
if md = path.match(/^([A-Za-z_]+(?:\/[A-Za-z_]+)*)(?:\s+(asc|desc))?$/)
property_name = md[1]
order = md[2].blank? ? :asc : md[2].to_sym
property = entity_type.properties.find { |p| p.name == property_name }
raise OData::AbstractQuery::Errors::PropertyNotFound.new(query, property_name) if property.blank?
[property, order]
else
raise OData::AbstractQuery::Errors::PropertyNotFound.new(query, path)
end
}
query.Option(self, pairs.empty? ? [[entity_type.key_property, :asc]] : pairs)
else
raise OData::AbstractQuery::Errors::InvalidOptionContext.new(query, self.option_name) unless value.blank?
end
end
|
Instance Method Details
#entity_type ⇒ Object
56
57
58
59
60
|
# File 'lib/o_data/abstract_query/options/orderby_option.rb', line 56
def entity_type
return nil if self.query.segments.empty?
return nil unless self.query.segments.last.respond_to?(:entity_type)
@entity_type ||= self.query.segments.last.entity_type
end
|
#valid? ⇒ Boolean
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/o_data/abstract_query/options/orderby_option.rb', line 62
def valid?
entity_type = self.entity_type
return false if entity_type.blank?
@pairs.is_a?(Array) && @pairs.all? { |pair|
property, value = pair
property.is_a?(OData::AbstractSchema::Property) && !!entity_type.properties.find { |p| p.name == property.name }
}
end
|
#value ⇒ Object
73
74
75
|
# File 'lib/o_data/abstract_query/options/orderby_option.rb', line 73
def value
"'" + @pairs.collect { |p| "#{p.first.name} #{p.last}" }.join(',') + "'"
end
|