Class: Frodo::Entity
- Inherits:
-
Object
- Object
- Frodo::Entity
- Defined in:
- lib/frodo/entity.rb
Overview
An Frodo::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. Frodo::Entity instances should not be instantiated directly; instead, they should either be read or instantiated from their respective Frodo::EntitySet.
Constant Summary collapse
- PROPERTY_NOT_LOADED =
:not_loaded
- XML_NAMESPACES =
{ 'xmlns' => 'http://www.w3.org/2005/Atom', 'xmlns:data' => 'http://docs.oasis-open.org/odata/ns/data', 'xmlns:metadata' => 'http://docs.oasis-open.org/odata/ns/metadata', 'xmlns:georss' => 'http://www.georss.org/georss', 'xmlns:gml' => 'http://www.opengis.net/gml', }.freeze
Instance Attribute Summary collapse
-
#entity_set ⇒ Object
readonly
The entity set this entity belongs to.
-
#errors ⇒ Object
readonly
List of errors on entity.
-
#service_name ⇒ Object
readonly
The Frodo::Service’s identifying name.
-
#type ⇒ Object
readonly
The Entity type name.
Class Method Summary collapse
-
.from_json(json, options = {}) ⇒ Frodo::Entity
Create Entity from JSON document with provided options.
-
.from_xml(xml_doc, options = {}) ⇒ Frodo::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.
- #any_errors? ⇒ Boolean
-
#context ⇒ String
Returns context URL for this entity.
- #get_property(property_name) ⇒ Object
-
#id ⇒ String
Returns the canonical URL for this entity.
-
#initialize(options = {}) ⇒ Entity
constructor
Initializes a bare Entity.
- #is_new? ⇒ Boolean
-
#links ⇒ Hash
Links to other Frodo entitites.
-
#name ⇒ String
Returns name of Entity from Service specified type.
- #namespace ⇒ Object
- #navigation_properties ⇒ Object
- #navigation_property_names ⇒ Object
-
#parse_annotations_from_property_name(property_name) ⇒ Object
strip inline annotations from property names and return separately.
-
#primary_key ⇒ String
Returns the primary key for the Entity.
- #property_names ⇒ Object
- #schema ⇒ Object
- #service ⇒ Object
-
#to_hash ⇒ Hash
Converts Entity to a hash.
-
#to_json ⇒ String
Converts Entity to its JSON representation.
-
#to_xml ⇒ String
Converts Entity to its XML representation.
Constructor Details
#initialize(options = {}) ⇒ Entity
Initializes a bare Entity
29 30 31 32 33 34 35 36 37 |
# File 'lib/frodo/entity.rb', line 29 def initialize( = {}) @id = [:id] @type = [:type] @service_name = [:service_name] @entity_set = [:entity_set] @context = [:context] @links = [:links] @errors = [] end |
Instance Attribute Details
#entity_set ⇒ Object (readonly)
The entity set this entity belongs to
13 14 15 |
# File 'lib/frodo/entity.rb', line 13 def entity_set @entity_set end |
#errors ⇒ Object (readonly)
List of errors on entity
15 16 17 |
# File 'lib/frodo/entity.rb', line 15 def errors @errors end |
#service_name ⇒ Object (readonly)
The Frodo::Service’s identifying name
11 12 13 |
# File 'lib/frodo/entity.rb', line 11 def service_name @service_name end |
#type ⇒ Object (readonly)
The Entity type name
9 10 11 |
# File 'lib/frodo/entity.rb', line 9 def type @type end |
Class Method Details
.from_json(json, options = {}) ⇒ Frodo::Entity
Create Entity from JSON document with provided options.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/frodo/entity.rb', line 152 def self.from_json(json, = {}) return nil if json.nil? json = JSON.parse(json.to_s) unless json.is_a?(Hash) = (json) .merge!(context: ['@odata.context']) entity = with_properties(json, ) (entity, ) entity end |
.from_xml(xml_doc, options = {}) ⇒ Frodo::Entity
Create Entity from XML document with provided options.
167 168 169 170 171 172 173 |
# File 'lib/frodo/entity.rb', line 167 def self.from_xml(xml_doc, = {}) return nil if xml_doc.nil? entity = Frodo::Entity.new() process_properties(entity, xml_doc) process_links(entity, xml_doc) entity end |
.with_properties(new_properties = {}, options = {}) ⇒ Object
Create Entity with provided properties and options.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/frodo/entity.rb', line 132 def self.with_properties(new_properties = {}, = {}) entity = Frodo::Entity.new() entity.instance_eval do service.properties_for_entity(type).each do |property_name, instance| set_property(property_name, instance) end new_properties.each do |property_name, property_value| prop_name, annotation = parse_annotations_from_property_name(property_name) # TODO: Do something with the annotation? self[prop_name] = property_value end end entity end |
Instance Method Details
#[](property_name) ⇒ *
Get property value
58 59 60 61 62 63 64 |
# File 'lib/frodo/entity.rb', line 58 def [](property_name) if get_property(property_name).is_a?(::Frodo::Properties::Complex) get_property(property_name) else get_property(property_name).value end end |
#[]=(property_name, value) ⇒ Object
Set property value
69 70 71 |
# File 'lib/frodo/entity.rb', line 69 def []=(property_name, value) get_property(property_name).value = value end |
#any_errors? ⇒ Boolean
233 234 235 |
# File 'lib/frodo/entity.rb', line 233 def any_errors? !errors.empty? end |
#context ⇒ String
Returns context URL for this entity
51 52 53 |
# File 'lib/frodo/entity.rb', line 51 def context @context ||= context_url end |
#get_property(property_name) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/frodo/entity.rb', line 73 def get_property(property_name) prop_name = property_name.to_s # Property is lazy loaded if properties_xml_value.has_key?(prop_name) property = instantiate_property(prop_name, properties_xml_value[prop_name]) set_property(prop_name, property.dup) properties_xml_value.delete(prop_name) end if properties.has_key? prop_name properties[prop_name] elsif .has_key? prop_name [prop_name] else raise ArgumentError, "Unknown property: #{property_name}" end end |
#id ⇒ String
Returns the canonical URL for this entity
215 216 217 218 219 220 221 |
# File 'lib/frodo/entity.rb', line 215 def id @id ||= lambda { entity_set = self.entity_set.andand.name entity_set ||= context.split('#').last.split('/').first "#{entity_set}(#{self[primary_key]})" }.call end |
#is_new? ⇒ Boolean
229 230 231 |
# File 'lib/frodo/entity.rb', line 229 def is_new? self[primary_key].nil? end |
#links ⇒ Hash
Links to other Frodo entitites
119 120 121 122 123 124 125 126 |
# File 'lib/frodo/entity.rb', line 119 def links @links ||= schema.[name].map do |nav_name, details| [ nav_name, { type: details.nav_type, href: "#{id}/#{nav_name}" } ] end.to_h end |
#name ⇒ String
Returns name of Entity from Service specified type.
45 46 47 |
# File 'lib/frodo/entity.rb', line 45 def name @name ||= type.split('.').last end |
#namespace ⇒ Object
39 40 41 |
# File 'lib/frodo/entity.rb', line 39 def namespace @namespace ||= type.rpartition('.').first end |
#navigation_properties ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/frodo/entity.rb', line 108 def @navigation_properties ||= links.keys.map do |nav_name| [ nav_name, Frodo::NavigationProperty::Proxy.new(self, nav_name) ] end.to_h end |
#navigation_property_names ⇒ Object
104 105 106 |
# File 'lib/frodo/entity.rb', line 104 def .keys end |
#parse_annotations_from_property_name(property_name) ⇒ Object
strip inline annotations from property names and return separately
92 93 94 95 |
# File 'lib/frodo/entity.rb', line 92 def parse_annotations_from_property_name(property_name) prop_name, annotation = property_name.to_s.split('@', 2) return prop_name, annotation end |
#primary_key ⇒ String
Returns the primary key for the Entity.
225 226 227 |
# File 'lib/frodo/entity.rb', line 225 def primary_key schema.primary_key_for(name) end |
#property_names ⇒ Object
97 98 99 100 101 102 |
# File 'lib/frodo/entity.rb', line 97 def property_names [ @properties_xml_value.andand.keys, @properties.andand.keys ].compact.flatten end |
#schema ⇒ Object
241 242 243 |
# File 'lib/frodo/entity.rb', line 241 def schema @schema ||= service.schemas[namespace] end |
#service ⇒ Object
237 238 239 |
# File 'lib/frodo/entity.rb', line 237 def service @service ||= Frodo::ServiceRegistry[service_name] end |
#to_hash ⇒ Hash
Converts Entity to a hash.
207 208 209 210 211 |
# File 'lib/frodo/entity.rb', line 207 def to_hash property_names.map do |name| [name, get_property(name).json_value] end.to_h end |
#to_json ⇒ String
Converts Entity to its JSON representation.
200 201 202 203 |
# File 'lib/frodo/entity.rb', line 200 def to_json # TODO: add @odata.context to_hash.to_json end |
#to_xml ⇒ String
Converts Entity to its XML representation.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/frodo/entity.rb', line 177 def to_xml namespaces = XML_NAMESPACES.merge('xml:base' => service.service_url) builder = Nokogiri::XML::Builder.new do |xml| xml.entry(namespaces) do xml.category(term: type, scheme: 'http://docs.oasis-open.org/odata/ns/scheme') xml. { xml.name } xml.content(type: 'application/xml') do xml['metadata'].properties do property_names.each do |name| next if name == primary_key get_property(name).to_xml(xml) end end end end end builder.to_xml end |