Module: RubySync::Utilities
- Included in:
- Connectors::BaseConnector, Event, Operation, Pipelines::BasePipeline
- Defined in:
- lib/ruby_sync/util/utilities.rb
Constant Summary collapse
- @@base_path =
nil
Instance Method Summary collapse
-
#as_array(o) ⇒ Object
If not already an array, slip into one.
- #call_if_exists(method, event, hint = "") ⇒ Object
- #connector_called(name) ⇒ Object
-
#effective_operations(operations, record = {}) ⇒ Object
Filter operations to eliminate those that would have no effect on the record.
-
#ensure_dir_exists(paths) ⇒ Object
Ensure that a given path exists as a directory.
- #get_preference(name, file_name = nil) ⇒ Object
- #get_preference_file_path(name) ⇒ Object
-
#include_in_search_path(path) ⇒ Object
Ensure that path is in the search path prepends it if it’s not.
- #log_progress(last_action, event, hint = "") ⇒ Object
-
#perform_operations(operations, record = {}, options = {}) ⇒ Object
Performs the given operations on the given record.
- #pipeline_called(name) ⇒ Object
- #set_preference(name) ⇒ Object
-
#something_called(name, extension) ⇒ Object
Locates and returns an instance of a class for the given name.
-
#with_rescue(text) ⇒ Object
Perform an action and rescue any exceptions thrown, display the exception with the specified text.
Instance Method Details
#as_array(o) ⇒ Object
If not already an array, slip into one
50 51 52 |
# File 'lib/ruby_sync/util/utilities.rb', line 50 def as_array o (o.instance_of? Array)? o : [o] end |
#call_if_exists(method, event, hint = "") ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/ruby_sync/util/utilities.rb', line 64 def call_if_exists(method, event, hint="") result = nil if respond_to? method with_rescue("#{method} #{hint}") {result = send method, event} else log.debug "No #{method}(event) method, continuing #{hint}" end return result end |
#connector_called(name) ⇒ Object
99 100 101 |
# File 'lib/ruby_sync/util/utilities.rb', line 99 def connector_called name something_called name, "connector" end |
#effective_operations(operations, record = {}) ⇒ Object
Filter operations to eliminate those that would have no effect on the record. Returns the resulting array of operations.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/ruby_sync/util/utilities.rb', line 197 def effective_operations operations, record={} effective = [] operations.each do |op| existing = as_array(record[op.subject] || []) case op.type when :add if existing.empty? effective << op else next if existing == op.values # already same so ignore effective << Operation.replace(op.subject, op.values) end when :replace if existing.empty? effective << Operation.add(op.subject, op.values) else next if existing == op.values effective << op end when :delete if [nil, "", []].include?(op.values) effective << op if record[op.subject] else targets = op.values & existing targets.empty? or effective << Operation.delete(op.subject, targets) end else raise Exception.new("Unknown operation '#{op.type}'") end end effective end |
#ensure_dir_exists(paths) ⇒ Object
Ensure that a given path exists as a directory
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby_sync/util/utilities.rb', line 80 def ensure_dir_exists paths as_array(paths).each do |path| raise Exception.new("Can't create nil directory") unless path if File.exist? path unless File.directory? path raise Exception.new("'#{path}' exists but is not a directory") end else log.info "Creating directory '#{path}'" FileUtils.mkpath path end end end |
#get_preference(name, file_name = nil) ⇒ Object
141 142 143 |
# File 'lib/ruby_sync/util/utilities.rb', line 141 def get_preference(name, file_name=nil) class_name ||= get_preference_file end |
#get_preference_file_path(name) ⇒ Object
149 150 151 152 153 |
# File 'lib/ruby_sync/util/utilities.rb', line 149 def get_preference_file_path name dir = "#{ENV[HOME]}/.rubysync" Dir.mkdir(dir) "#{dir}#{file}" end |
#include_in_search_path(path) ⇒ Object
Ensure that path is in the search path prepends it if it’s not
112 113 114 115 |
# File 'lib/ruby_sync/util/utilities.rb', line 112 def include_in_search_path path path = File.(path) $:.unshift path unless $:.include?(path) end |
#log_progress(last_action, event, hint = "") ⇒ Object
74 75 76 |
# File 'lib/ruby_sync/util/utilities.rb', line 74 def log_progress last_action, event, hint="" log.info "Result of #{last_action}: #{hint}\n" + YAML.dump(event) end |
#perform_operations(operations, record = {}, options = {}) ⇒ Object
Performs the given operations on the given record. The record is a Hash in which each key is a field name and each value is an array of values for that field. Operations is an Array of RubySync::Operation objects to be performed on the record.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ruby_sync/util/utilities.rb', line 159 def perform_operations operations, record={}, ={} subjects = [:subjects] operations.each do |op| unless op.instance_of? RubySync::Operation log.warn "!!!!!!!!!! PROBLEM, DUMP FOLLOWS: !!!!!!!!!!!!!!" p op end next if subjects and !subjects.include?(op.subject) case op.type when :add if record[op.subject] existing = as_array(record[op.subject]) next if existing == op.values # already same so ignore (existing & op.values).empty? or raise "Attempt to add duplicate elements to #{name}" record[op.subject] = existing + op.values else record[op.subject] = op.values end when :replace record[op.subject] = op.values when :delete if value == nil || value == "" || value == [] record.delete(op.subject) else record[op.subject] -= values end else raise Exception.new("Unknown operation '#{op.type}'") end end return record end |
#pipeline_called(name) ⇒ Object
94 95 96 |
# File 'lib/ruby_sync/util/utilities.rb', line 94 def pipeline_called name something_called name, "pipeline" end |
#set_preference(name) ⇒ Object
145 146 147 |
# File 'lib/ruby_sync/util/utilities.rb', line 145 def set_preference(name) end |
#something_called(name, extension) ⇒ Object
Locates and returns an instance of a class for the given name.
105 106 107 108 |
# File 'lib/ruby_sync/util/utilities.rb', line 105 def something_called name, extension filename = "#{name.to_s}_#{extension}" eval(filename.camelize).new end |
#with_rescue(text) ⇒ Object
Perform an action and rescue any exceptions thrown, display the exception with the specified text
55 56 57 58 59 60 61 62 |
# File 'lib/ruby_sync/util/utilities.rb', line 55 def with_rescue text begin yield rescue Exception => exception log.warn "#{text}: #{exception.}" log.debug exception.backtrace.join("\n") end end |