Class: DynamicsCRM::XML::Attributes
- Inherits:
-
Hash
- Object
- Hash
- DynamicsCRM::XML::Attributes
show all
- Defined in:
- lib/dynamics_crm/xml/attributes.rb
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)
202
203
204
205
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 202
def method_missing(method_name, *args, &block)
return self.key?(method_name.to_s) ? self[method_name.to_s] : nil
end
|
Class Method Details
.from_xml(xml_document) ⇒ Object
Instance Method Details
#build_xml(key, value, type) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 96
def build_xml(key, value, type)
xml = %Q{
<a:KeyValuePairOfstringanyType>
<c:key>#{key}</c:key>
}
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_name ⇒ Object
186
187
188
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 186
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
56
57
58
59
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 11
def get_type(key, value)
type = "string"
case value
when ::Array
type = "ArrayOfEntity"
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::Double
type = "double"
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 =~ /\A\{?\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}?\z/
type = 'guid'
end
type
end
|
#render_object_xml(type, value) ⇒ Object
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 175
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
xml
end
|
#render_value_xml(type, value) ⇒ Object
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 115
def render_value_xml(type, value)
xml = ""
case type
when "ArrayOfEntity"
raise "We can only serialize Entities inside of ArrayOfEntity" unless value.all?{|a| a.is_a?(DynamicsCRM::XML::Entity)}
xml << %Q{
<c:value i:type="a:ArrayOfEntity">
#{value.map(&->(_) { _.to_xml({in_array: true}) }).join}
</c:value>
}
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 value.nil?
xml << %Q{
<c:value i:nil="true"></c:value>
}
elsif 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>
}
elsif type == "double"
xml << %Q{
<c:value i:type="s:#{type}" xmlns:s="#{s_namespace}">#{value.value}</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
207
208
209
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 207
def respond_to_missing?(method_name, include_private = false)
self.has_key?(method_name.to_s) || super
end
|
#to_hash ⇒ Object
Removes Attributes class wrapper.
62
63
64
65
66
67
68
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 62
def to_hash
raw_hash = {}
self.each do |key, value|
raw_hash[key] = value
end
raw_hash
end
|
#to_s ⇒ Object
92
93
94
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 92
def to_s
self.to_hash
end
|
#to_xml ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/dynamics_crm/xml/attributes.rb', line 70
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|
if value.is_a?(Hash) && !value[:type].nil?
type = value[:type]
value = value[:value]
else
type = get_type(key, value)
end
value = CGI.escapeHTML(value) if value.is_a?(String)
xml << build_xml(key, value, type)
end
xml << %Q{\n</a:#{self.class_name}>}
end
|