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 namespace.
-
#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.
26 27 28 29 30 31 32 |
# File 'lib/odata/entity_set.rb', line 26 def initialize( = {}) @name = [:name] @type = [:type] @namespace = [:namespace] @container = [:container] self end |
Instance Attribute Details
#container ⇒ Object (readonly)
The EntitySet’s container name
20 21 22 |
# File 'lib/odata/entity_set.rb', line 20 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 namespace
18 19 20 |
# File 'lib/odata/entity_set.rb', line 18 def namespace @namespace 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.
97 98 99 100 101 102 103 104 105 |
# File 'lib/odata/entity_set.rb', line 97 def <<(entity) url_chunk, = setup_entity_post_request(entity) result = execute_entity_post_request(, url_chunk) if entity.is_new? doc = ::Nokogiri::XML(result).remove_namespaces! entity[entity.primary_key] = doc.xpath("//content/properties/#{entity.primary_key}").first.content end entity end |
#[](key) ⇒ OData::Entity?
Find the Entity with the supplied key value.
88 89 90 91 92 |
# File 'lib/odata/entity_set.rb', line 88 def [](key) result = service.execute("#{name}(#{key})") entities = service.find_entities(result) OData::Entity.from_xml(entities[0], ) end |
#count ⇒ Integer
Returns the number of entities within the set.
68 69 70 |
# File 'lib/odata/entity_set.rb', line 68 def count service.execute("#{name}/$count").body.to_i end |
#each(&block) ⇒ OData::Entity
Provided for Enumerable functionality
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/odata/entity_set.rb', line 38 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.
117 118 119 120 121 122 |
# File 'lib/odata/entity_set.rb', line 117 def { namespace: namespace, type: type } end |
#first ⇒ OData::EntitySet
Return the first Entity for the set.
59 60 61 62 63 64 |
# File 'lib/odata/entity_set.rb', line 59 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.
75 76 77 |
# File 'lib/odata/entity_set.rb', line 75 def new_entity(properties = {}) OData::Entity.with_properties(properties, ) end |
#query ⇒ OData::Query
Returns a query targetted at the current EntitySet.
81 82 83 |
# File 'lib/odata/entity_set.rb', line 81 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.
110 111 112 |
# File 'lib/odata/entity_set.rb', line 110 def service @service ||= OData::ServiceRegistry[namespace] end |