Class: Exchanger::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/exchanger/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Field

Returns a new instance of Field.



5
6
7
8
# File 'lib/exchanger/field.rb', line 5

def initialize(name, options = {})
  @name = name
  @options = options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/exchanger/field.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/exchanger/field.rb', line 3

def options
  @options
end

Instance Method Details

#field_uriObject



32
33
34
35
36
37
38
# File 'lib/exchanger/field.rb', line 32

def field_uri
  if name == :text
    field_uri_namespace
  else
    "#{field_uri_namespace}:#{tag_name}"
  end
end

#field_uri_namespaceObject



28
29
30
# File 'lib/exchanger/field.rb', line 28

def field_uri_namespace
  options[:field_uri_namespace].to_s
end

#sub_fieldObject

Only for arrays



19
20
21
22
23
24
25
26
# File 'lib/exchanger/field.rb', line 19

def sub_field
  if type.is_a?(Array)
    Field.new(name, {
      :type => type[0],
      :field_uri_namespace => field_uri
    })
  end
end

#tag_nameObject



14
15
16
# File 'lib/exchanger/field.rb', line 14

def tag_name
  (options[:name] || name).to_s.camelize
end

#to_xml(value, options = {}) ⇒ Object

Convert Ruby value to XML



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/exchanger/field.rb', line 97

def to_xml(value, options = {})
  if value.is_a?(Exchanger::Element)
    value.tag_name = tag_name
    value.to_xml(options)
  else
    doc = Nokogiri::XML::Document.new
    root = doc.create_element(tag_name)
    case value
    when Array
      value.each do |sub_value|
        root << sub_field.to_xml(sub_value, options)
      end
    when Boolean
      root << doc.create_text_node(value == true)
    when Time
      root << doc.create_text_node(value.xmlschema)
    else # String, Integer, etc
      root << doc.create_text_node(value.to_s)
    end
    root
  end
end

#to_xml_field_uri(value) ⇒ Object

FieldURI or IndexedFieldURI <t:FieldURI FieldURI=“item:Sensitivity”/> <t:IndexedFieldURI FieldURI=“contacts:EmailAddress” FieldIndex=“EmailAddress1”/>

msdn.microsoft.com/en-us/library/aa494315.aspx msdn.microsoft.com/en-us/library/aa581079.aspx



46
47
48
49
50
51
52
53
# File 'lib/exchanger/field.rb', line 46

def to_xml_field_uri(value)
  doc = Nokogiri::XML::Document.new
  if value.is_a?(Entry)
    doc.create_element("IndexedFieldURI", "FieldURI" => field_uri, "FieldIndex" => value.key)
  else
    doc.create_element("FieldURI", "FieldURI" => field_uri)
  end
end

#to_xml_updates(value) ⇒ Object

See Element#to_xml_updates. Yields blocks with FieldURI and Item/Folder/etc changes.



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
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/exchanger/field.rb', line 57

def to_xml_updates(value)
  return if options[:readonly]
  doc = Nokogiri::XML::Document.new
  if value.is_a?(Array)
    value.each do |sub_value|
      sub_field.to_xml_updates(sub_value) do |field_uri_xml, element_xml|
        element_wrapper = doc.create_element(sub_field.tag_name)
        element_wrapper << element_xml
        yield [
          field_uri_xml,
          element_wrapper
        ]
      end
    end
  elsif value.is_a?(Exchanger::Element)
    value.tag_name = tag_name
    if value.class.elements.keys.include?(:text)
      field = value.class.elements[:text]
      yield [
        field.to_xml_field_uri(value),
        field.to_xml(value)
      ]
    else
      # PhysicalAddress ?
      value.class.elements.each do |name, field|
        yield [
          field.to_xml_field_uri(value),
          field.to_xml(value, :only => [name])
        ]
      end
    end
  else # String, Integer, Boolean, ...
    yield [
      self.to_xml_field_uri(value),
      self.to_xml(value)
    ]
  end
end

#typeObject



10
11
12
# File 'lib/exchanger/field.rb', line 10

def type
  options[:type] || String
end

#value_from_xml(node) ⇒ Object

Convert XML to Ruby value



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/exchanger/field.rb', line 121

def value_from_xml(node)
  if type.respond_to?(:new_from_xml)
    type.new_from_xml(node)
  elsif type.is_a?(Array)
    node.children.map do |sub_node|
      sub_field.value_from_xml(sub_node)
    end
  elsif type == Boolean
    node.text == "true"
  elsif type == Integer
    node.text.to_i unless node.text.empty?
  elsif type == Time
    Time.xmlschema(node.text) unless node.text.empty?
  else
    node.text
  end
end