Class: DynamicsCRM::XML::Criteria

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tuples = []) ⇒ Criteria

Returns a new instance of Criteria.



7
8
9
10
# File 'lib/dynamics_crm/xml/criteria.rb', line 7

def initialize(tuples=[])
  super
  @filter_operator = 'And'
end

Instance Attribute Details

#filter_operatorObject

Returns the value of attribute filter_operator.



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

def filter_operator
  @filter_operator
end

Instance Method Details

#to_xml(options = {}) ⇒ Object

ConditionExpression can be repeated multiple times Operator: can be lots of values such as: eq (Equals), neq (Not Equals), gt (Greater Than)

get the values from a fetch xml query

Values -> Value can be repeated multiple times FilterOperator: and OR or depending on the filter requirements



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
# File 'lib/dynamics_crm/xml/criteria.rb', line 17

def to_xml(options={})
  ns = options[:namespace] ? options[:namespace] : "a"

  expressions = ""
  self.each do |tuple|
    attr_name = tuple[0]
    operator = tuple[1]
    values = tuple[2].is_a?(Array) ? tuple[2] : [tuple[2]]
    # TODO: Improve type detection
    type = (tuple[3] || values.first.class).to_s.downcase
    type = "int" if type == "fixnum"
    type = "boolean" if ["trueclass", "falseclass"].include?(type)

    expressions << %Q{<#{ns}:ConditionExpression>
        <#{ns}:AttributeName>#{attr_name}</#{ns}:AttributeName>
        <#{ns}:Operator>#{operator}</#{ns}:Operator>
        <#{ns}:Values xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      }
        values.each do |v|
            expressions << %Q{<d:anyType i:type="s:#{type}" xmlns:s="http://www.w3.org/2001/XMLSchema">#{v}</d:anyType>}
        end
        
    expressions << %Q{
         </#{ns}:Values>
    </#{ns}:ConditionExpression>}
  end

  %Q{<#{ns}:Criteria>
      <#{ns}:Conditions>
        #{expressions}
      </#{ns}:Conditions>
      <#{ns}:FilterOperator>#{@filter_operator}</#{ns}:FilterOperator>
  </#{ns}:Criteria>}
end