Class: RubySync::Connectors::XmlConnector

Inherits:
BaseConnector show all
Includes:
REXML
Defined in:
lib/ruby_sync/connectors/xml_connector.rb

Instance Attribute Summary

Attributes inherited from BaseConnector

#is_vault, #name, #once_only, #pipeline

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseConnector

#associate, #association_context, #association_for, #association_key_for, #association_to_path_dbm_filename, #associations_for, #can_act_as_vault?, class_for, class_name_for, #clean, #create_operations_for, #dbm_path, #digest, #each_change, #entry_for_own_association_key, fields, #find_associated, #has_entry_for_key?, #initialize, #is_delete_echo?, #is_echo?, #is_vault?, #mirror_dbm_filename, #own_association_key_for, #path_for_association, #path_for_own_association_key, #path_to_association_dbm_filename, #remove_association, #remove_associations, #remove_mirror, #start, #started, #stop, #stopped, #sync_started, #sync_stopped, #test_add, #test_delete, #test_modify

Methods included from Utilities

#as_array, #base_path, #call_if_exists, #connector_called, #effective_operations, #ensure_dir_exists, #find_base_path, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #pipeline_called, #set_preference, #something_called, #with_rescue

Constructor Details

This class inherits a constructor from RubySync::Connectors::BaseConnector

Class Method Details

.sample_configObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 133

def self.sample_config
  return %q(
#
# "filename" should be the full name of the file containing
# the xml representation of the synchronized content.
# You probably want to change this:
#
filename "/tmp/rubysync.xml"
  )
end

Instance Method Details

#[](id) ⇒ Object



81
82
83
84
85
86
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 81

def [](id)
  with_xml(:read_only=>true) do |xml|
    element = xml.entry_element_for(id)
    return (element)? to_entry(element) : nil
  end
end

#[]=(id, value) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 90

def []=(id, value)
  with_xml do |xml|
    new_child = to_xml(id, value)
    if old_child = xml.entry_element_for(id)   
      xml.root.replace_child(old_child, new_child)
    else
      xml.root << new_child
    end
  end
end

#add(id, operations) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 54

def add id, operations
  entry = nil
  with_xml do |xml|
    xml.entry_element_for(id) and raise "Element '#{id}' already exists."
    entry = perform_operations(operations)
  end
  self[id] = entry
  id
end

#delete(id) ⇒ Object



74
75
76
77
78
79
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 74

def delete id
  xpath = "//entry[@id='#{id}']"
  with_xml do |xml|
    xml.root.delete_element xpath
  end
end

#each_entryObject



45
46
47
48
49
50
51
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 45

def each_entry
  with_xml(:read_only=>true) do |xml|
    xml.root.each_element("entry") do |element|
      yield element.attribute('id').value, to_entry(element)
    end
  end
end

#modify(id, operations) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 64

def modify id, operations
  entry = nil
  with_xml do |xml|
    existing_entry = to_entry(xml.entry_element_for(id))
    entry = perform_operations(operations, existing_entry)
  end
  self[id] = entry
  id
end

#to_entry(entry_element) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 119

def to_entry entry_element
  entry = {}
  if entry_element
    entry_element.each_element("attr") do |child|
      entry[child.attribute('name').value] = values = []
      child.each_element("value") do |value_element|
        values << value_element.text
      end
    end
  end
  entry
end

#to_xml(key, entry) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 102

def to_xml key, entry
  el = Element.new("entry")
  el.add_attribute('id', key)
  entry.each do |key, values|
    el << attr = Element.new("attr")
    attr.add_attribute 'name', key
    as_array(values).each do |value|
      value_el = Element.new('value')
      attr << value_el.add_text(value)
    end
  end
  el
end

#with_xml(options = {}) ⇒ Object

Should be re-entrant within a single thread but isn’t thread-safe.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ruby_sync/connectors/xml_connector.rb', line 147

def with_xml options={}
  unless @with_xml_invoked
    begin
      @with_xml_invoked = true
      File.exist?(filename) or File.open(filename,'w') {|file| file.write('<entries/>')}
      File.open(filename, "r") do |file|
        file.flock(File::LOCK_EX)
        @xml = Document.new(file)
        begin
          yield @xml
        ensure
          File.open(filename, "w") do |out|
            @xml.write out
          end
        end
      end
    ensure
      @with_xml_invoked = false
    end
  else # this is a nested call so we don't need to read or write the file
    yield @xml
  end
end