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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Criteria.



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

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

  @filters = []
end

Instance Attribute Details

#filter_operatorObject

Returns the value of attribute filter_operator.



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

def filter_operator
  @filter_operator
end

Instance Method Details

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



23
24
25
# File 'lib/dynamics_crm/xml/criteria.rb', line 23

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

#add_filter(filter) ⇒ Object



27
28
29
# File 'lib/dynamics_crm/xml/criteria.rb', line 27

def add_filter(filter)
  @filters << filter
end

#conditions_xml(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dynamics_crm/xml/criteria.rb', line 47

def conditions_xml(options)
  ns = options[:namespace] ? options[:namespace] : 'a'

  if @expressions.empty?
    "<#{ns}:Conditions />"
  else
    xml_expression = @expressions.map do |conditional|
      conditional.to_xml(options)
    end.join('')

    %(<#{ns}:Conditions>
      #{xml_expression}
    </#{ns}:Conditions>)
  end
end

#filters_xml(options) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/dynamics_crm/xml/criteria.rb', line 63

def filters_xml(options)
  ns = options[:namespace] ? options[:namespace] : 'a'
  if @filters.empty?
    "<#{ns}:Filters />"
  else
    fx = @filters.map { |f| f.to_xml(options) }.join('')
    %(<#{ns}:Filters>#{fx}</#{ns}:Filters>)
  end
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 Filters: can contain FilterExpressions to support complex logical statements.



37
38
39
40
41
42
43
44
45
# File 'lib/dynamics_crm/xml/criteria.rb', line 37

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

  %(<#{ns}:Criteria>
      #{conditions_xml(options)}
      <#{ns}:FilterOperator>#{@filter_operator}</#{ns}:FilterOperator>
      #{filters_xml(options)}
  </#{ns}:Criteria>)
end