Class: OData::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/odata/entity.rb

Overview

An OData::Entity represents a single record returned by the service. All Entities have a type and belong to a specific namespace. They are written back to the service via the EntitySet they came from. OData::Entity instances should not be instantiated directly; instead, they should either be read or instantiated from their respective OData::EntitySet.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Entity

Initializes a bare Entity



21
22
23
24
25
26
27
# File 'lib/odata/entity.rb', line 21

def initialize(options = {})
  @type = options[:type]
  @namespace = options[:namespace]
  @service_name = options[:service_name]
  @links = options[:links] || {}
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

List of errors on entity



17
18
19
# File 'lib/odata/entity.rb', line 17

def errors
  @errors
end

Links to other OData entitites



15
16
17
# File 'lib/odata/entity.rb', line 15

def links
  @links
end

#namespaceObject (readonly)

The OData::Service’s namespace



11
12
13
# File 'lib/odata/entity.rb', line 11

def namespace
  @namespace
end

#service_nameObject (readonly)

The OData::Service’s identifying name



13
14
15
# File 'lib/odata/entity.rb', line 13

def service_name
  @service_name
end

#typeObject (readonly)

The Entity type name



9
10
11
# File 'lib/odata/entity.rb', line 9

def type
  @type
end

Class Method Details

.from_xml(xml_doc, options = {}) ⇒ OData::Entity

Create Entity from XML document with provided options.



83
84
85
86
87
88
89
90
91
# File 'lib/odata/entity.rb', line 83

def self.from_xml(xml_doc, options = {})
  return nil if xml_doc.nil?
  entity = OData::Entity.new(options)
  process_properties(entity, xml_doc)
  process_feed_property(entity, xml_doc, 'title')
  process_feed_property(entity, xml_doc, 'summary')
  process_links(entity, xml_doc)
  entity
end

.with_properties(new_properties = {}, options = {}) ⇒ Object

Create Entity with provided properties and options.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/odata/entity.rb', line 65

def self.with_properties(new_properties = {}, options = {})
  entity = OData::Entity.new(options)
  entity.instance_eval do
    service.properties_for_entity(name).each do |name, instance|
      set_property(name, instance)
    end

    new_properties.each do |property_name, property_value|
      self[property_name.to_s] = property_value
    end
  end
  entity
end

Instance Method Details

#[](property_name) ⇒ *

Get property value



38
39
40
41
42
43
44
45
46
# File 'lib/odata/entity.rb', line 38

def [](property_name)
  if properties[property_name.to_s].is_a?(::OData::ComplexType)
    properties[property_name.to_s]
  else
    properties[property_name.to_s].value
  end
rescue NoMethodError
  raise ArgumentError, "Unknown property: #{property_name}"
end

#[]=(property_name, value) ⇒ Object

Set property value



51
52
53
54
55
# File 'lib/odata/entity.rb', line 51

def []=(property_name, value)
  properties[property_name.to_s].value = value
rescue NoMethodError
  raise ArgumentError, "Unknown property: #{property_name}"
end

#any_errors?Boolean



130
131
132
# File 'lib/odata/entity.rb', line 130

def any_errors?
  !errors.empty?
end

#associationsObject



57
58
59
# File 'lib/odata/entity.rb', line 57

def associations
  @associations ||= OData::Association::Proxy.new(self)
end

#is_new?Boolean



126
127
128
# File 'lib/odata/entity.rb', line 126

def is_new?
  self[primary_key].nil?
end

#nameString

Returns name of Entity from Service specified type.



31
32
33
# File 'lib/odata/entity.rb', line 31

def name
  @name ||= type.gsub(/#{namespace}\./, '')
end

#primary_keyString

Returns the primary key for the Entity.



122
123
124
# File 'lib/odata/entity.rb', line 122

def primary_key
  service.primary_key_for(name)
end

#to_xmlString

Converts Entity to its XML representation.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/odata/entity.rb', line 95

def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.entry('xmlns'           => 'http://www.w3.org/2005/Atom',
              'xmlns:data'      => 'http://schemas.microsoft.com/ado/2007/08/dataservices',
              'xmlns:metadata'  => 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
              'xmlns:georss'    => 'http://www.georss.org/georss',
              'xmlns:gml'       => 'http://www.opengis.net/gml',
              'xml:base'        => 'http://services.odata.org/OData/OData.svc/') do
      xml.category(term: "#{namespace}.#{type}",
                   scheme: 'http://schemas.microsoft.com/ado/2007/08/dataservices/scheme')
      xml.author { xml.name }

      xml.content(type: 'application/xml') do
        xml['metadata'].properties do
          properties.each do |name, property|
            next if name == primary_key
            property.to_xml(xml)
          end
        end
      end
    end
  end
  builder.to_xml
end