Module: RubySync::Utilities
- Included in:
- Configuration, 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.
-
#base_path ⇒ Object
Return the base_path.
- #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.
-
#find_base_path ⇒ Object
Locate a configuration directory by checking the current directory and all of it’s ancestors until it finds one that looks like a rubysync configuration directory.
-
#get_preference(name, file_name = nil) ⇒ Object
Make and instance method name that returns the value set by the class method name.
- #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 |
#base_path ⇒ Object
Return the base_path
119 120 121 122 |
# File 'lib/ruby_sync/util/utilities.rb', line 119 def base_path @@base_path = find_base_path unless @@base_path @@base_path 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.
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 229 230 231 232 233 234 235 |
# File 'lib/ruby_sync/util/utilities.rb', line 204 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 |
#find_base_path ⇒ Object
Locate a configuration directory by checking the current directory and all of it’s ancestors until it finds one that looks like a rubysync configuration directory. Returns false if no suitable directory was found
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ruby_sync/util/utilities.rb', line 128 def find_base_path bp = File.(".") last = nil # Keep going up until we start repeating ourselves while File.directory?(bp) && bp != last && bp != "/" return bp if File.directory?("#{bp}/pipelines") && File.directory?("#{bp}/connectors") last = bp bp = File.("#{bp}/..") end return false end |
#get_preference(name, file_name = nil) ⇒ Object
Make and instance method name that returns the value set by the class method name. def self.class_option name
self.class_eval "def #{name}() self.class.instance_variable_get :#{name}; end"
self.instance_eval "def #{name}(value) @#{name}=value; end"
end
148 149 150 |
# File 'lib/ruby_sync/util/utilities.rb', line 148 def get_preference(name, file_name=nil) class_name ||= get_preference_file end |
#get_preference_file_path(name) ⇒ Object
156 157 158 159 160 |
# File 'lib/ruby_sync/util/utilities.rb', line 156 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
113 114 115 116 |
# File 'lib/ruby_sync/util/utilities.rb', line 113 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.
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 192 193 194 195 196 197 198 |
# File 'lib/ruby_sync/util/utilities.rb', line 166 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
152 153 154 |
# File 'lib/ruby_sync/util/utilities.rb', line 152 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 109 |
# File 'lib/ruby_sync/util/utilities.rb', line 105 def something_called name, extension filename = "#{name.to_s}_#{extension}" $".include?(filename) or require filename or return nil 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 |