Class: OData::EntitySet
- Inherits:
-
Object
- Object
- OData::EntitySet
- Includes:
- Enumerable
- Defined in:
- lib/odata/entity_set.rb
Overview
This class represents a set of entities within an OData service. It is instantiated whenever an OData::Service is asked for an EntitySet via the OData::Service#[] method call. It also provides Enumerable behavior so that you can interact with the entities within a set in a very comfortable way.
This class also implements a query interface for finding certain entities based on query criteria or limiting the result set returned by the set. This functionality is implemented through transparent proxy objects.
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
The EntitySet’s container name.
-
#name ⇒ Object
readonly
The name of the EntitySet.
-
#namespace ⇒ Object
readonly
The OData::Service’s namespace.
-
#service_name ⇒ Object
readonly
The OData::Service’s identifiable name.
-
#type ⇒ Object
readonly
The Entity type for the EntitySet.
Instance Method Summary collapse
-
#<<(entity) ⇒ OData::Entity
Write supplied entity back to the service.
-
#[](key) ⇒ OData::Entity?
Find the Entity with the supplied key value.
-
#count ⇒ Integer
Returns the number of entities within the set.
-
#each(&block) ⇒ OData::Entity
Provided for Enumerable functionality.
-
#entity_options ⇒ Hash
private
Options used for instantiating a new OData::Entity for this set.
-
#first ⇒ OData::EntitySet
Return the first Entity for the set.
-
#initialize(options = {}) ⇒ OData::EntitySet
constructor
Sets up the EntitySet to permit querying for the resources in the set.
-
#new_entity(properties = {}) ⇒ OData::Entity
Create a new Entity for this set with the given properties.
-
#query ⇒ OData::Query
Returns a query targetted at the current EntitySet.
-
#service ⇒ OData::Service
private
The OData::Service this EntitySet is associated with.
Constructor Details
#initialize(options = {}) ⇒ OData::EntitySet
Sets up the EntitySet to permit querying for the resources in the set.
28 29 30 31 32 33 34 |
# File 'lib/odata/entity_set.rb', line 28 def initialize( = {}) @name = [:name] @type = [:type] @namespace = [:namespace] @service_name = [:service_name] @container = [:container] end |
Instance Attribute Details
#container ⇒ Object (readonly)
The EntitySet’s container name
22 23 24 |
# File 'lib/odata/entity_set.rb', line 22 def container @container end |
#name ⇒ Object (readonly)
The name of the EntitySet
14 15 16 |
# File 'lib/odata/entity_set.rb', line 14 def name @name end |
#namespace ⇒ Object (readonly)
The OData::Service’s namespace
18 19 20 |
# File 'lib/odata/entity_set.rb', line 18 def namespace @namespace end |
#service_name ⇒ Object (readonly)
The OData::Service’s identifiable name
20 21 22 |
# File 'lib/odata/entity_set.rb', line 20 def service_name @service_name end |
#type ⇒ Object (readonly)
The Entity type for the EntitySet
16 17 18 |
# File 'lib/odata/entity_set.rb', line 16 def type @type end |
Instance Method Details
#<<(entity) ⇒ OData::Entity
Write supplied entity back to the service.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/odata/entity_set.rb', line 104 def <<(entity) url_chunk, = setup_entity_post_request(entity) result = execute_entity_post_request(, url_chunk) if entity.is_new? doc = ::Nokogiri::XML(result.body).remove_namespaces! primary_key_node = doc.xpath("//content/properties/#{entity.primary_key}").first entity[entity.primary_key] = primary_key_node.content unless primary_key_node.nil? end unless result.code.to_s =~ /^2[0-9][0-9]$/ entity.errors << ['could not commit entity'] end entity end |
#[](key) ⇒ OData::Entity?
Find the Entity with the supplied key value.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/odata/entity_set.rb', line 90 def [](key) entity = new_entity key_property = entity.send(:properties)[entity.primary_key] key_property.value = key url_key = key_property.url_value result = service.execute("#{name}(#{url_key})") entities = service.find_entities(result) OData::Entity.from_xml(entities[0], ) end |
#count ⇒ Integer
Returns the number of entities within the set.
70 71 72 |
# File 'lib/odata/entity_set.rb', line 70 def count service.execute("#{name}/$count").body.to_i end |
#each(&block) ⇒ OData::Entity
Provided for Enumerable functionality
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/odata/entity_set.rb', line 40 def each(&block) per_page = 5; page = 0; page_position = 0; counter = 0 total, entities = get_paginated_entities(per_page, page) until counter == total if page_position >= per_page page += 1 _, entities = get_paginated_entities(per_page, page) page_position = 0 end entity = OData::Entity.from_xml(entities[page_position], ) block_given? ? block.call(entity) : yield(entity) counter += 1 page_position += 1 end end |
#entity_options ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Options used for instantiating a new OData::Entity for this set.
130 131 132 133 134 135 136 |
# File 'lib/odata/entity_set.rb', line 130 def { namespace: namespace, service_name: service_name, type: type } end |
#first ⇒ OData::EntitySet
Return the first Entity for the set.
61 62 63 64 65 66 |
# File 'lib/odata/entity_set.rb', line 61 def first query = OData::Query.new(self).limit(1) result = service.execute(query) entities = service.find_entities(result) OData::Entity.from_xml(entities[0], ) end |
#new_entity(properties = {}) ⇒ OData::Entity
Create a new Entity for this set with the given properties.
77 78 79 |
# File 'lib/odata/entity_set.rb', line 77 def new_entity(properties = {}) OData::Entity.with_properties(properties, ) end |
#query ⇒ OData::Query
Returns a query targetted at the current EntitySet.
83 84 85 |
# File 'lib/odata/entity_set.rb', line 83 def query OData::Query.new(self) end |
#service ⇒ OData::Service
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The OData::Service this EntitySet is associated with.
123 124 125 |
# File 'lib/odata/entity_set.rb', line 123 def service @service ||= OData::ServiceRegistry[service_name] end |