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

Instance Method Details

#as_array(o) ⇒ Object

If not already an array, slip into one



51
52
53
# File 'lib/ruby_sync/util/utilities.rb', line 51

def as_array o
  (o.instance_of? Array)? o : [o]
end

#call_if_exists(method, event, hint = "") ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/ruby_sync/util/utilities.rb', line 78

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

#class_called(name, extension, message = nil) ⇒ Object



134
135
136
# File 'lib/ruby_sync/util/utilities.rb', line 134

def class_called name, extension, message=nil
  class_for_name(class_name_for(name, extension), message)
end

#class_for_name(name, message = nil) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/ruby_sync/util/utilities.rb', line 138

def class_for_name(name, message=nil)
  eval(name)
rescue
  message ||= "Unable to find class called '#{name}'"
  log.error message
  nil
end

#class_name_for(name, extension) ⇒ Object



146
147
148
# File 'lib/ruby_sync/util/utilities.rb', line 146

def class_name_for name, extension
  "#{name.to_s}_#{extension}".camelize
end

#connector_called(name, message = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/ruby_sync/util/utilities.rb', line 118

def connector_called name, message=nil
  begin
	something_called name, "connector"
  rescue
	message ||= "Connector named '#{name}' not found."
    log.error message
	nil
  end
end

#dump_afterObject



69
# File 'lib/ruby_sync/util/utilities.rb', line 69

def dump_after() []; end

#dump_beforeObject



65
66
67
# File 'lib/ruby_sync/util/utilities.rb', line 65

def dump_before
  []
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.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/ruby_sync/util/utilities.rb', line 240

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
      unless op.value
        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



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby_sync/util/utilities.rb', line 94

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



181
182
183
# File 'lib/ruby_sync/util/utilities.rb', line 181

def get_preference(name, file_name=nil)
  class_name ||= get_preference_file
end

#get_preference_file_path(name) ⇒ Object



189
190
191
192
193
# File 'lib/ruby_sync/util/utilities.rb', line 189

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



152
153
154
155
# File 'lib/ruby_sync/util/utilities.rb', line 152

def include_in_search_path path
  path = File.expand_path(path)
  $:.unshift path unless $:.include?(path)
end

#log_progress(last_action, event, hint = "") ⇒ Object



88
89
90
# File 'lib/ruby_sync/util/utilities.rb', line 88

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.



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
229
230
231
232
233
234
# File 'lib/ruby_sync/util/utilities.rb', line 199

def perform_operations operations, record={}, options={}
  subjects = options[:subjects]
  operations.each do |op|
    unless op.instance_of? RubySync::Operation
      log.warn "!!!!!!!!!!  PROBLEM, DUMP FOLLOWS: !!!!!!!!!!!!!!"
      p op
    end
	key = op.subject
    next if subjects and !subjects.include?(key)
    case op.type
    when :add
      if record[key]
        existing = as_array(record[key])
        next if existing == op.values # already same so ignore
        (existing & op.values).empty? or
   raise "Attempt to add duplicate elements to #{name}"
        record[key] =  existing + op.values
      else
        record[key] = op.values
      end
    when :replace
      record[key] = op.values
    when :delete
      if record[key]
        unless op.value
          record.delete(op.subject)
        else
          record[key] -= op.values
        end
      end
    else
      raise Exception.new("Unknown operation '#{op.type}'")
    end
  end
  return record
end

#perform_transform(name, event, hint = "") ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ruby_sync/util/utilities.rb', line 70

def perform_transform name, event, hint=""
  log.info event.to_yaml if dump_before.include?(name.to_sym)
  log.info "performing #{name}"
  call_if_exists name, event, hint
  event.commit_changes
  log.info event.to_yaml if dump_after.include?(name.to_sym)
end

#pipeline_called(name) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/ruby_sync/util/utilities.rb', line 108

def pipeline_called name
  begin
	something_called name, "pipeline"
  rescue
	log.error "Pipeline named '#{name}' not found."
	nil
  end
end

#set_preference(name) ⇒ Object



185
186
187
# File 'lib/ruby_sync/util/utilities.rb', line 185

def set_preference(name)
  
end

#something_called(name, extension, message = nil) ⇒ Object

Locates and returns an instance of a class for the given name.



130
131
132
# File 'lib/ruby_sync/util/utilities.rb', line 130

def something_called name, extension, message=nil
  klass = class_called(name, extension, message) and klass.new()
end

#with_rescue(text) ⇒ Object

Perform an action and rescue any exceptions thrown, display the exception with the specified text



56
57
58
59
60
61
62
63
# File 'lib/ruby_sync/util/utilities.rb', line 56

def with_rescue text
  begin
    yield
  rescue Exception => exception
    log.warn "#{text}: #{exception.message}"
    log.debug exception.backtrace.join("\n")
  end
end