Class: DynamicsCRM::FetchXml::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamics_crm/fetch_xml/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Builder

Returns a new instance of Builder.



32
33
34
35
36
37
38
39
40
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 32

def initialize(options={})
  @entities = []
  @link_entities = []

  @version = options[:version] || '1.0'
  @output_format = options[:output_format] || 'xml-platform'
  @mapping = options[:mapping] || 'logical'
  @distinct = options[:distinct] || false
end

Instance Attribute Details

#distinctObject

Returns the value of attribute distinct.



30
31
32
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 30

def distinct
  @distinct
end

#mappingObject

Returns the value of attribute mapping.



30
31
32
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 30

def mapping
  @mapping
end

#output_formatObject

Returns the value of attribute output_format.



30
31
32
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 30

def output_format
  @output_format
end

#versionObject

Returns the value of attribute version.



30
31
32
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 30

def version
  @version
end

Instance Method Details

#entity(logical_name) ⇒ Object



42
43
44
45
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 42

def entity(logical_name)
  @entities << Entity.new(logical_name)
  @entities.last
end

#to_xmlObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dynamics_crm/fetch_xml/builder.rb', line 47

def to_xml
  @builder = ::Builder::XmlMarkup.new(:indent=>2)

  # <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  @builder.fetch(version: @version, :"output-format" => @output_format, mapping: @mapping, distinct: @distinct) {
    @entities.each do |e|
      # <entity name="opportunityproduct">
      @builder.entity(name: e.logical_name) {
        e.attributes.each do |field|
          # <attribute name="productid" />
          @builder.attribute(name: field)
        end

        if e.order_field
          @builder.order(attribute: e.order_field, descending: e.order_desc)
        end

        add_link_entities(e)

      if e.has_conditions?
        # <filter type="and">
        @builder.filter(type: 'and') {
          e.conditions.each do |c|
            # <condition attribute="opportunityid" operator="eq" value="02dd7344-d04a-e411-a9d3-9cb654950300" />
            @builder.condition(attribute: c[:attribute], operator: c[:operator], value: c[:value])
          end
        }
      end

      # </entity>
      }
    end
  }
  @builder.target!
end