Class: DynamicsCRM::XML::Attributes

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

Direct Known Subclasses

FormattedValues, Parameters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Attributes

Returns a new instance of Attributes.



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

def initialize(attrs)
  super
  self.merge!(attrs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Allows method-like access to the hash (OpenStruct)



183
184
185
186
# File 'lib/dynamics_crm/xml/attributes.rb', line 183

def method_missing(method_name, *args, &block)
  # Return local hash entry if any.
  return self.key?(method_name.to_s) ? self[method_name.to_s] : nil
end

Class Method Details

.from_xml(xml_document) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/dynamics_crm/xml/attributes.rb', line 171

def self.from_xml(xml_document)
  hash = MessageParser.parse_key_value_pairs(xml_document)
  if xml_document.name == "FormattedValues"
    return FormattedValues.new(hash)
  elsif xml_document.name == "Parameters"
    return Parameters.new(hash)
  else
    return Attributes.new(hash)
  end
end

Instance Method Details

#build_xml(key, value, type) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dynamics_crm/xml/attributes.rb', line 92

def build_xml(key, value, type)

  xml = %Q{
    <a:KeyValuePairOfstringanyType>
      <c:key>#{key}</c:key>
    }

  # If we have an object that can convert itself, use it.
  if (value.respond_to?(:to_xml) && value.class.to_s.include?("DynamicsCRM"))
    xml << render_object_xml(type, value)
  else
    xml << render_value_xml(type, value)
  end

  xml << "\n</a:KeyValuePairOfstringanyType>"

  xml
end

#class_nameObject



167
168
169
# File 'lib/dynamics_crm/xml/attributes.rb', line 167

def class_name
  self.class.to_s.split("::").last
end

#get_type(key, value) ⇒ Object



11
12
13
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dynamics_crm/xml/attributes.rb', line 11

def get_type(key, value)
  type = "string"
  case value
    when ::Fixnum
      type = "int"
    when ::BigDecimal, ::Float
      type = "decimal"
    when ::TrueClass, ::FalseClass
      type = "boolean"
    when ::Time, ::DateTime
      type = "dateTime"
    when ::Hash, EntityReference
      type = "EntityReference"
    when Entity
      type = "Entity"
    when EntityCollection
      type = "EntityCollection"
    when Query
      type = "QueryExpression"
    when FetchExpression
      type = "FetchExpression"
    when Money
      type = "Money"
    when DynamicsCRM::Metadata::FilterExpression
      type = "FilterExpression"
    when DynamicsCRM::Metadata::PropertiesExpression
      type = "PropertiesExpression"
    when DynamicsCRM::Metadata::AttributeQueryExpression
      type = "AttributeQueryExpression"
    when DynamicsCRM::Metadata::EntityQueryExpression
      type = "EntityQueryExpression"
    else
      if key.to_s == "EntityFilters"
        type = "EntityFilters"
      elsif key.to_s == "RollupType"
        type = "RollupType"
      end
  end

  if type == "string" && value =~ /\w+{8}-\w+{4}-\w+{4}-\w+{4}-\w+{12}/
    type = "guid"
  end

  type
end

#render_object_xml(type, value) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/dynamics_crm/xml/attributes.rb', line 156

def render_object_xml(type, value)
    case type
    when "EntityQueryExpression"
      xml = %Q{<c:value i:type="d:#{type}" xmlns:d="http://schemas.microsoft.com/xrm/2011/Metadata/Query">} << value.to_xml({namespace: 'd'}) << "</c:value>"
    else
      xml = %Q{<c:value i:type="a:#{type}">} << value.to_xml({exclude_root: true, namespace: 'a'}) << "</c:value>"
    end

    return xml
end

#render_value_xml(type, value) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dynamics_crm/xml/attributes.rb', line 111

def render_value_xml(type, value)
  xml = ""
  case type
  when "EntityReference"
    xml << %Q{
      <c:value i:type="a:EntityReference">
          <a:Id>#{value[:id]}</a:Id>
          <a:LogicalName>#{value[:logical_name]}</a:LogicalName>
          <a:Name #{value[:name] ? '' : 'i:nil="true"'}>#{value[:name]}</a:Name>
      </c:value>
    }
  when "OptionSetValue", "Money"
    xml << %Q{
        <c:value i:type="a:#{type}">
          <a:Value>#{value}</a:Value>
        </c:value>
    }
  else
    s_namespace = "http://www.w3.org/2001/XMLSchema"
    if ["EntityFilters"].include?(type)
      s_namespace = "http://schemas.microsoft.com/xrm/2011/Metadata"
    end

    if type == "guid"
      xml << %Q{
        <c:value xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/" i:type="d:guid">#{value}</c:value>
      }
    elsif type == "RollupType"
      xml << %Q{
        <c:value i:type="a:RollupType">#{value}</c:value>
      }
    elsif type == "dateTime"
      xml << %Q{
        <c:value i:type="s:#{type}" xmlns:s="http://www.w3.org/2001/XMLSchema">#{value.utc.strftime('%Y-%m-%dT%H:%M:%SZ')}</c:value>
      }
    else
      xml << %Q{
        <c:value i:type="s:#{type}" xmlns:s="#{s_namespace}">#{value}</c:value>
      }
    end
  end

  xml
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/dynamics_crm/xml/attributes.rb', line 188

def respond_to_missing?(method_name, include_private = false)
  self.has_key?(method_name.to_s) || super
end

#to_hashObject

Removes Attributes class wrapper.



58
59
60
61
62
63
64
# File 'lib/dynamics_crm/xml/attributes.rb', line 58

def to_hash
  raw_hash = {}
  self.each do |key, value|
    raw_hash[key] = value
  end
  raw_hash
end

#to_sObject



88
89
90
# File 'lib/dynamics_crm/xml/attributes.rb', line 88

def to_s
  self.to_hash
end

#to_xmlObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dynamics_crm/xml/attributes.rb', line 66

def to_xml
  xml = %Q{<a:#{self.class_name} xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">}

  self.each do |key,value|

    # Temporary hack to handle types I cannot infer (OptionSetValue or Money).
    if value.is_a?(Hash) && !value[:type].nil?
      type = value[:type]
      value = value[:value]
    else
      type = get_type(key, value)
    end

    # escape strings to avoid xml parsing errors
    value = CGI.escapeHTML(value) if value.is_a?(String)

    xml << build_xml(key, value, type)
  end

  xml << %Q{\n</a:#{self.class_name}>}
end