Class: DynamicsCRM::XML::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamics_crm/xml/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logical_name, id = nil) ⇒ Entity

Returns a new instance of Entity.



8
9
10
11
# File 'lib/dynamics_crm/xml/entity.rb', line 8

def initialize(logical_name, id=nil)
  @logical_name = logical_name
  @id = id || "00000000-0000-0000-0000-000000000000"
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/dynamics_crm/xml/entity.rb', line 6

def attributes
  @attributes
end

#entity_stateObject

Returns the value of attribute entity_state.



6
7
8
# File 'lib/dynamics_crm/xml/entity.rb', line 6

def entity_state
  @entity_state
end

#formatted_valuesObject

Returns the value of attribute formatted_values.



6
7
8
# File 'lib/dynamics_crm/xml/entity.rb', line 6

def formatted_values
  @formatted_values
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/dynamics_crm/xml/entity.rb', line 6

def id
  @id
end

#logical_nameObject

Returns the value of attribute logical_name.



6
7
8
# File 'lib/dynamics_crm/xml/entity.rb', line 6

def logical_name
  @logical_name
end

Returns the value of attribute related_entities.



6
7
8
# File 'lib/dynamics_crm/xml/entity.rb', line 6

def related_entities
  @related_entities
end

Class Method Details

.from_xml(xml_document) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dynamics_crm/xml/entity.rb', line 53

def self.from_xml(xml_document)
  entity = Entity.new('')

  if xml_document
    xml_document.elements.each do |node|

      attr_name = DynamicsCRM::StringUtil.underscore(node.name).to_sym
      if entity.respond_to?(attr_name)
        if node.name == "Attributes"
          entity.attributes = XML::Attributes.from_xml(node)
        elsif node.name == "FormattedValues"
          entity.formatted_values = XML::FormattedValues.from_xml(node)
          # Reset to nil if no values were found.
          entity.formatted_values = nil if entity.formatted_values.empty?
        else
          entity.send("#{attr_name}=", node.text ? node.text.strip : nil)
        end
      end
    end
  end

  return entity
end

Instance Method Details

#to_hashObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/dynamics_crm/xml/entity.rb', line 42

def to_hash
  {
    :attributes => @attributes.to_hash,
    :entity_state => entity_state,
    :formatted_values => (@formatted_values ? @formatted_values.to_hash : nil),
    :id => @id,
    :logical_name => @logical_name,
    :related_entities => related_entities
  }
end

#to_xml(options = {}) ⇒ Object

Using Entity vs entity causes the error: Value cannot be null.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dynamics_crm/xml/entity.rb', line 14

def to_xml(options={})

  inner_xml = %Q{
    #{@attributes.is_a?(XML::Attributes) ? @attributes.to_xml : @attributes}
    <a:EntityState i:nil="true" />
    <a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <a:Id>#{@id}</a:Id>
    <a:LogicalName>#{@logical_name}</a:LogicalName>
    <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  }

  return inner_xml if options[:exclude_root]

  if options[:in_array]
    %Q{
    <a:Entity>
      #{inner_xml}
    </a:Entity>
    }
  else
    %Q{
    <entity xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
      #{inner_xml}
    </entity>
    }
  end
end