Class: DynamicsCRM::XML::Criteria

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

Constant Summary collapse

SUPPORTED_OPERATORS =
%w(And Or)

Instance Method Summary collapse

Constructor Details

#initialize(tuples = [], filter_operator: nil) ⇒ Criteria

Returns a new instance of Criteria.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dynamics_crm/xml/criteria.rb', line 6

def initialize(tuples = [], filter_operator: nil)
  filter_operator ||= 'And'
  raise "Supported operators: #{SUPPORTED_OPERATORS.join(',')}" if !SUPPORTED_OPERATORS.include?(filter_operator)

  super(tuples)
  @filter_operator = filter_operator

  # Convert to ConditionExpression
  @expressions = self.map do |tuple|
    attr_name, operator, value, data_type = *tuple
    ConditionExpression.new(attr_name, operator, value, type: data_type)
  end
end

Instance Method Details

#add_condition(attr_name, operator, value, type: nil) ⇒ Object



20
21
22
# File 'lib/dynamics_crm/xml/criteria.rb', line 20

def add_condition(attr_name, operator, value, type: nil)
  @expressions << ConditionExpression.new(attr_name, operator, value, type: type)
end

#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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dynamics_crm/xml/criteria.rb', line 29

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

  xml_expression = @expressions.map do |conditional|
    conditional.to_xml(options)
  end.join('')

  %(<#{ns}:Criteria>
      <#{ns}:Conditions>
        #{xml_expression}
      </#{ns}:Conditions>
      <#{ns}:FilterOperator>#{@filter_operator}</#{ns}:FilterOperator>
  </#{ns}:Criteria>)
end