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



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

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

Instance Attribute Details

#containerObject (readonly)

The EntitySet’s container name



22
23
24
# File 'lib/odata/entity_set.rb', line 22

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’s namespace



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

def namespace
  @namespace
end

#service_nameObject (readonly)

The OData::Service’s identifiable name



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

def service_name
  @service_name
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:



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

Parameters:

  • key (to_s)

    primary key to lookup

Returns:



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

#countInteger

Returns the number of entities within the set.

Returns:

  • (Integer)


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

Parameters:

  • block (block)

    a block to evaluate

Returns:



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


130
131
132
133
134
135
136
# File 'lib/odata/entity_set.rb', line 130

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

#firstOData::EntitySet

Return the first Entity for the set.

Returns:



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], 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:



77
78
79
# File 'lib/odata/entity_set.rb', line 77

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

#queryOData::Query

Returns a query targetted at the current EntitySet.

Returns:



83
84
85
# File 'lib/odata/entity_set.rb', line 83

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:



123
124
125
# File 'lib/odata/entity_set.rb', line 123

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