Class: OData::Entity
- Inherits:
-
Object
- Object
- OData::Entity
- Defined in:
- lib/odata/entity.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .from_xml(xml_doc, options = {}) ⇒ Object
- .with_properties(new_properties = {}, options = {}) ⇒ Object
Instance Method Summary collapse
- #[](property_name) ⇒ Object
- #[]=(property_name, value) ⇒ Object
-
#initialize(options = {}) ⇒ Entity
constructor
A new instance of Entity.
- #name ⇒ Object
- #primary_key ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Entity
Returns a new instance of Entity.
5 6 7 8 |
# File 'lib/odata/entity.rb', line 5 def initialize( = {}) @type = [:type] @namespace = [:namespace] end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
3 4 5 |
# File 'lib/odata/entity.rb', line 3 def namespace @namespace end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/odata/entity.rb', line 3 def type @type end |
Class Method Details
.from_xml(xml_doc, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/odata/entity.rb', line 44 def self.from_xml(xml_doc, = {}) return nil if xml_doc.nil? entity = OData::Entity.new() entity.instance_eval do xml_doc.xpath('//content/properties/*').each do |property_xml| property_name = property_xml.name if property_xml.attributes['null'] && property_xml.attributes['null'].value == 'true' value = nil value_type = service.get_property_type(name, property_name) else value_type = property_xml.attributes['type'].value value = property_xml.content end klass_name = value_type.gsub(/^Edm\./, '') property = Kernel.const_get("OData::Properties::#{klass_name}").new(property_name, value) set_property(property_name, property) end begin title_value = xml_doc.xpath('//title').first.content property_name = service.get_title_property_name(name) value_type = service.get_property_type(name, property_name) klass_name = value_type.gsub(/^Edm\./, '') property = Kernel.const_get("OData::Properties::#{klass_name}").new(property_name, title_value) set_property(property_name, property) end begin summary_value = xml_doc.xpath('//summary').first.content property_name = service.get_summary_property_name(name) value_type = service.get_property_type(name, property_name) klass_name = value_type.gsub(/^Edm\./, '') property = Kernel.const_get("OData::Properties::#{klass_name}").new(property_name, summary_value) set_property(property_name, property) end end entity end |
.with_properties(new_properties = {}, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/odata/entity.rb', line 30 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] = property_value end end entity end |
Instance Method Details
#[](property_name) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/odata/entity.rb', line 14 def [](property_name) begin properties[property_name.to_s].value rescue NoMethodError raise ArgumentError, "Unknown property: #{property_name}" end end |
#[]=(property_name, value) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/odata/entity.rb', line 22 def []=(property_name, value) begin properties[property_name.to_s].value = value rescue NoMethodError raise ArgumentError, "Unknown property: #{property_name}" end end |
#name ⇒ Object
10 11 12 |
# File 'lib/odata/entity.rb', line 10 def name @name ||= type.gsub(/#{namespace}\./, '') end |
#primary_key ⇒ Object
118 119 120 |
# File 'lib/odata/entity.rb', line 118 def primary_key service.primary_key_for(name) end |
#to_xml ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/odata/entity.rb', line 84 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 attributes = { 'metadata:type' => property.type, } if property.value.nil? attributes['metadata:null'] = 'true' xml['data'].send(name.to_sym, attributes) else xml['data'].send(name.to_sym, attributes, property.xml_value) end end end end end end builder.to_xml end |