Class: FrOData::EntitySet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/frodata/entity_set.rb

Overview

This class represents a set of entities within an FrOData service. It is instantiated whenever an FrOData::Service is asked for an EntitySet via the FrOData::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 = {}) ⇒ FrOData::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/frodata/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/frodata/entity_set.rb', line 22

def container
  @container
end

#nameObject (readonly)

The name of the EntitySet



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

def name
  @name
end

#namespaceObject (readonly)

The FrOData::Service’s namespace



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

def namespace
  @namespace
end

#service_nameObject (readonly)

The FrOData::Service’s identifiable name



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

def service_name
  @service_name
end

#typeObject (readonly)

The Entity type for the EntitySet



16
17
18
# File 'lib/frodata/entity_set.rb', line 16

def type
  @type
end

Instance Method Details

#<<(entity) ⇒ FrOData::Entity

Write supplied entity back to the service. TODO Test this more with CRM2011

Parameters:

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/frodata/entity_set.rb', line 90

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, options = {}) ⇒ FrOData::Entity?

Find the Entity with the supplied key value.

Parameters:

  • key (to_s)

    primary key to lookup

Returns:



76
77
78
79
80
81
82
83
84
# File 'lib/frodata/entity_set.rb', line 76

def [](key, options={})
  properties_to_expand = if options[:expand] == :all
    new_entity.navigation_property_names
  else
    [ options[:expand] ].compact.flatten
  end

  query.expand(*properties_to_expand).find(key)
end

#countInteger

Returns the number of entities within the set. Not supported in Microsoft CRM2011

Returns:

  • (Integer)


55
56
57
# File 'lib/frodata/entity_set.rb', line 55

def count
  query.count
end

#each(&block) ⇒ FrOData::Entity

Provided for Enumerable functionality

Parameters:

  • block (block)

    a block to evaluate

Returns:



40
41
42
# File 'lib/frodata/entity_set.rb', line 40

def each(&block)
  query.execute.each(&block)
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 FrOData::Entity for this set.

Returns:

  • (Hash)


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

def entity_options
  {
    service_name: service_name,
    type:         type,
    entity_set:   self
  }
end

#first(count = 1) ⇒ FrOData::EntitySet

Return the first ‘n` Entities for the set. If count is 1 it returns the single entity, otherwise its an array of entities

Returns:



47
48
49
50
# File 'lib/frodata/entity_set.rb', line 47

def first(count = 1)
  result = query.limit(count).execute
  count == 1 ? result.first : result.to_a
end

#new_entity(properties = {}) ⇒ FrOData::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:



62
63
64
# File 'lib/frodata/entity_set.rb', line 62

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

#query(options = {}) ⇒ FrOData::Query

Returns a query targetted at the current EntitySet.

Parameters:

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

    query options

Returns:



69
70
71
# File 'lib/frodata/entity_set.rb', line 69

def query(options = {})
  FrOData::Query.new(self, options)
end

#serviceFrOData::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 FrOData::Service this EntitySet is associated with.

Returns:



109
110
111
# File 'lib/frodata/entity_set.rb', line 109

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