Class: OData::EntitySet

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OData::EntitySet

Sets up the EntitySet to permit querying for the resources in the set.

Parameters:

  • options (Hash) (defaults to: {})

    the options to setup the EntitySet



26
27
28
29
30
31
32
# File 'lib/odata/entity_set.rb', line 26

def initialize(options = {})
  @name = options[:name]
  @type = options[:type]
  @namespace = options[:namespace]
  @container = options[:container]
  self
end

Instance Attribute Details

#containerObject (readonly)

The EntitySet’s container name



20
21
22
# File 'lib/odata/entity_set.rb', line 20

def container
  @container
end

#nameObject (readonly)

The name of the EntitySet



14
15
16
# File 'lib/odata/entity_set.rb', line 14

def name
  @name
end

#namespaceObject (readonly)

The OData::Service namespace



18
19
20
# File 'lib/odata/entity_set.rb', line 18

def namespace
  @namespace
end

#typeObject (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.

Parameters:

  • entity (OData::Entity)

    entity to save or update in the service

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/odata/entity_set.rb', line 97

def <<(entity)
  url_chunk, options = setup_entity_post_request(entity)
  result = execute_entity_post_request(options, 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.

Parameters:

  • key (to_s)

    primary key to lookup

Returns:



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], entity_options)
end

#countInteger

Returns the number of entities within the set.

Returns:

  • (Integer)


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

Parameters:

  • block (block)

    a block to evaluate

Returns:



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], entity_options)
    block_given? ? block.call(entity) : yield(entity)

    counter += 1
    page_position += 1
  end
end

#entity_optionsHash

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.

Returns:

  • (Hash)


117
118
119
120
121
122
# File 'lib/odata/entity_set.rb', line 117

def entity_options
  {
      namespace:  namespace,
      type:       type
  }
end

#firstOData::EntitySet

Return the first Entity for the set.

Returns:



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], entity_options)
end

#new_entity(properties = {}) ⇒ OData::Entity

Create a new Entity for this set with the given properties.

Parameters:

  • properties (Hash) (defaults to: {})

    property name as key and it’s initial value

Returns:



75
76
77
# File 'lib/odata/entity_set.rb', line 75

def new_entity(properties = {})
  OData::Entity.with_properties(properties, entity_options)
end

#queryOData::Query

Returns a query targetted at the current EntitySet.

Returns:



81
82
83
# File 'lib/odata/entity_set.rb', line 81

def query
  OData::Query.new(self)
end

#serviceOData::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.

Returns:



110
111
112
# File 'lib/odata/entity_set.rb', line 110

def service
  @service ||= OData::ServiceRegistry[namespace]
end