Class: Exchanger::Field
- Inherits:
-
Object
- Object
- Exchanger::Field
- Defined in:
- lib/exchanger/field.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #field_uri ⇒ Object
- #field_uri_namespace ⇒ Object
-
#initialize(name, options = {}) ⇒ Field
constructor
A new instance of Field.
-
#sub_field ⇒ Object
Only for arrays.
- #tag_name ⇒ Object
-
#to_xml(value, options = {}) ⇒ Object
Convert Ruby value to XML.
-
#to_xml_field_uri(value) ⇒ Object
FieldURI or IndexedFieldURI <t:FieldURI FieldURI=“item:Sensitivity”/> <t:IndexedFieldURI FieldURI=“contacts:EmailAddress” FieldIndex=“EmailAddress1”/>.
-
#to_xml_updates(value) ⇒ Object
See Element#to_xml_updates.
- #type ⇒ Object
-
#value_from_xml(node) ⇒ Object
Convert XML to Ruby value.
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, = {}) @name = name @options = end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/exchanger/field.rb', line 3 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/exchanger/field.rb', line 3 def @options end |
Instance Method Details
#field_uri ⇒ Object
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_namespace ⇒ Object
28 29 30 |
# File 'lib/exchanger/field.rb', line 28 def field_uri_namespace [:field_uri_namespace].to_s end |
#sub_field ⇒ Object
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_name ⇒ Object
14 15 16 |
# File 'lib/exchanger/field.rb', line 14 def tag_name ([: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, = {}) if value.is_a?(Exchanger::Element) value.tag_name = tag_name value.to_xml() 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, ) 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 [: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 |
#type ⇒ Object
10 11 12 |
# File 'lib/exchanger/field.rb', line 10 def type [: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 |