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
Returns the value of attribute container.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
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.
-
#filter(filter) ⇒ Array<OData::Entity>
Find Entities with the supplied filter applied.
-
#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.
Constructor Details
#initialize(options = {}) ⇒ OData::EntitySet
Sets up the EntitySet to permit querying for the resources in the set.
19 20 21 22 23 24 25 |
# File 'lib/odata/entity_set.rb', line 19 def initialize( = {}) @name = [:name] @type = [:type] @namespace = [:namespace] @container = [:container] self end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
13 14 15 |
# File 'lib/odata/entity_set.rb', line 13 def container @container end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/odata/entity_set.rb', line 13 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
13 14 15 |
# File 'lib/odata/entity_set.rb', line 13 def namespace @namespace end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/odata/entity_set.rb', line 13 def type @type end |
Instance Method Details
#<<(entity) ⇒ OData::Entity
Write supplied entity back to the service.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/odata/entity_set.rb', line 100 def <<(entity) new_entity = entity[entity.primary_key].nil? url_chunk = name url_chunk += "(#{entity[entity.primary_key]})" unless new_entity = { method: :post, body: entity.to_xml.gsub(/\n\s+/, ''), headers: { Accept: 'application/atom+xml', 'Content-Type' => 'application/atom+xml' } } result = service.execute(url_chunk, ) if result.code.to_s =~ /^2[0-9][0-9]$/ if new_entity doc = ::Nokogiri::XML(result.body).remove_namespaces! entity[entity.primary_key] = doc.xpath("//content/properties/#{entity.primary_key}").first.content end else raise StandardError, 'Something went wrong committing your entity' end entity end |
#[](key) ⇒ OData::Entity?
Find the Entity with the supplied key value.
91 92 93 94 95 |
# File 'lib/odata/entity_set.rb', line 91 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.
63 64 65 |
# File 'lib/odata/entity_set.rb', line 63 def count service.execute("#{name}/$count").body.to_i end |
#each(&block) ⇒ OData::Entity
Provided for Enumerable functionality
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/odata/entity_set.rb', line 31 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 |
#filter(filter) ⇒ Array<OData::Entity>
Find Entities with the supplied filter applied.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/odata/entity_set.rb', line 77 def filter(filter) entities = [] result = service.execute("#{name}?$filter=#{filter}") service.find_entities(result).each do |entity_xml| entities << OData::Entity.from_xml(entity_xml, ) end entities end |
#first ⇒ OData::EntitySet
Return the first Entity for the set.
52 53 54 55 56 57 58 59 |
# File 'lib/odata/entity_set.rb', line 52 def first query = OData::Query.new(name) query << OData::Query::Criteria.new(operation: 'skip', argument: 0) query << OData::Query::Criteria.new(operation: 'top', argument: 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.
70 71 72 |
# File 'lib/odata/entity_set.rb', line 70 def new_entity(properties = {}) OData::Entity.with_properties(properties, ) end |