Class: OData::AbstractQuery::Options::SelectOption

Inherits:
OData::AbstractQuery::Option show all
Defined in:
lib/o_data/abstract_query/options/select_option.rb

Instance Attribute Summary collapse

Attributes inherited from BasicOption

#key, #query

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OData::AbstractQuery::Option

#applies_to?, #option_name

Methods inherited from BasicOption

#inspect, #option_name

Constructor Details

#initialize(query, properties = []) ⇒ SelectOption

Returns a new instance of SelectOption.



11
12
13
14
15
# File 'lib/o_data/abstract_query/options/select_option.rb', line 11

def initialize(query, properties = [])
  @properties = properties
  
  super(query, self.class.option_name)
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/o_data/abstract_query/options/select_option.rb', line 9

def properties
  @properties
end

Class Method Details

.applies_to?(query) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/o_data/abstract_query/options/select_option.rb', line 17

def self.applies_to?(query)
  return false if query.segments.empty?
  (query.segments.last.is_a?(OData::AbstractQuery::Segments::CollectionSegment) || query.segments.last.is_a?(OData::AbstractQuery::Segments::NavigationPropertySegment))
end

.option_nameObject



5
6
7
# File 'lib/o_data/abstract_query/options/select_option.rb', line 5

def self.option_name
  '$select'
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
# File 'lib/o_data/abstract_query/options/select_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
  
  if query.segments.last.respond_to?(:entity_type)
    entity_type = query.segments.last.entity_type
    
    properties = value.to_s.strip == "*" ? entity_type.properties : value.to_s.split(/\s*,\s*/).collect { |path|
      if md = path.match(/^([A-Za-z_]+)$/)
        property_name = md[1]
        
        property = entity_type.properties.find { |p| p.name == property_name }
        raise OData::AbstractQuery::Errors::PropertyNotFound.new(query, property_name) if property.blank?

        property
      else
        raise OData::AbstractQuery::Errors::PropertyNotFound.new(query, path)
      end
    }
    
    query.Option(self, properties)
  else
    raise OData::AbstractQuery::Errors::InvalidOptionContext.new(query, self.option_name) unless value.blank?
  end
end

Instance Method Details

#entity_typeObject



53
54
55
56
57
# File 'lib/o_data/abstract_query/options/select_option.rb', line 53

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

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/o_data/abstract_query/options/select_option.rb', line 59

def valid?
  entity_type = self.entity_type
  return false if entity_type.blank?
  
  @properties.is_a?(Array) && @properties.all? { |property|
    property.is_a?(OData::AbstractSchema::Property) && !!entity_type.properties.find { |p| p == property }
  }
end

#valueObject



68
69
70
# File 'lib/o_data/abstract_query/options/select_option.rb', line 68

def value
  "'" + @properties.collect(&:name).join(',') + "'"
end