Class: OData::Entity
- Inherits:
-
Object
- Object
- OData::Entity
- 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
-
#namespace ⇒ Object
readonly
The OData::Service namespace.
-
#type ⇒ Object
readonly
The Entity type name.
Class Method Summary collapse
-
.from_xml(xml_doc, options = {}) ⇒ OData::Entity
Create Entity from XML document with provided options.
-
.with_properties(new_properties = {}, options = {}) ⇒ Object
Create Entity with provided properties and options.
Instance Method Summary collapse
-
#[](property_name) ⇒ *
Get property value.
-
#[]=(property_name, value) ⇒ Object
Set property value.
-
#initialize(options = {}) ⇒ Entity
constructor
Initializes a bare Entity.
- #is_new? ⇒ Boolean
-
#name ⇒ String
Returns name of Entity from Service specified type.
-
#primary_key ⇒ String
Returns the primary key for the Entity.
-
#to_xml ⇒ String
Converts Entity to its XML representation.
Constructor Details
#initialize(options = {}) ⇒ Entity
Initializes a bare Entity
15 16 17 18 |
# File 'lib/odata/entity.rb', line 15 def initialize( = {}) @type = [:type] @namespace = [:namespace] end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
The OData::Service namespace
11 12 13 |
# File 'lib/odata/entity.rb', line 11 def namespace @namespace end |
#type ⇒ Object (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.
70 71 72 73 74 75 76 77 |
# File 'lib/odata/entity.rb', line 70 def self.from_xml(xml_doc, = {}) return nil if xml_doc.nil? entity = OData::Entity.new() process_properties(entity, xml_doc) process_feed_property(entity, xml_doc, 'title') process_feed_property(entity, xml_doc, 'summary') entity end |
.with_properties(new_properties = {}, options = {}) ⇒ Object
Create Entity with provided properties and options.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/odata/entity.rb', line 52 def self.with_properties(new_properties = {}, = {}) entity = OData::Entity.new() entity.instance_eval do service.properties_for(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
29 30 31 32 33 34 35 |
# File 'lib/odata/entity.rb', line 29 def [](property_name) begin properties[property_name.to_s].value rescue NoMethodError raise ArgumentError, "Unknown property: #{property_name}" end end |
#[]=(property_name, value) ⇒ Object
Set property value
40 41 42 43 44 45 46 |
# File 'lib/odata/entity.rb', line 40 def []=(property_name, value) begin properties[property_name.to_s].value = value rescue NoMethodError raise ArgumentError, "Unknown property: #{property_name}" end end |
#is_new? ⇒ Boolean
112 113 114 |
# File 'lib/odata/entity.rb', line 112 def is_new? self[primary_key].nil? end |
#name ⇒ String
Returns name of Entity from Service specified type.
22 23 24 |
# File 'lib/odata/entity.rb', line 22 def name @name ||= type.gsub(/#{namespace}\./, '') end |
#primary_key ⇒ String
Returns the primary key for the Entity.
108 109 110 |
# File 'lib/odata/entity.rb', line 108 def primary_key service.primary_key_for(name) end |
#to_xml ⇒ String
Converts Entity to its XML representation.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/odata/entity.rb', line 81 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. { 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 |