Module: RubySync::Utilities

Included in:
Configuration, Connectors::BaseConnector, Pipelines::BasePipeline
Defined in:
lib/ruby_sync/util/utilities.rb

Constant Summary collapse

@@base_path =
nil

Instance Method Summary collapse

Instance Method Details

#base_pathObject

Return the base_path



101
102
103
104
# File 'lib/ruby_sync/util/utilities.rb', line 101

def base_path
  @@base_path = find_base_path unless @@base_path
  @@base_path
end

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



46
47
48
49
50
51
52
53
54
# File 'lib/ruby_sync/util/utilities.rb', line 46

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



81
82
83
# File 'lib/ruby_sync/util/utilities.rb', line 81

def connector_called name
  something_called name, "connector"
end

#ensure_dir_exists(paths) ⇒ Object

Ensure that a given path exists as a directory



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_sync/util/utilities.rb', line 62

def ensure_dir_exists paths
  paths.as_array.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_pathObject

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby_sync/util/utilities.rb', line 110

def find_base_path
  bp = File.expand_path(".")
  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.expand_path("#{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



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

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

#get_preference_file_path(name) ⇒ Object



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

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



95
96
97
98
# File 'lib/ruby_sync/util/utilities.rb', line 95

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

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



56
57
58
# File 'lib/ruby_sync/util/utilities.rb', line 56

def log_progress last_action, event, hint=""
  log.info "Result of #{last_action}: #{hint}\n" + YAML.dump(event)
end

#pipeline_called(name) ⇒ Object



76
77
78
# File 'lib/ruby_sync/util/utilities.rb', line 76

def pipeline_called name
  something_called name, "pipeline"
end

#set_preference(name) ⇒ Object



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

def set_preference(name)
  
end

#something_called(name, extension) ⇒ Object

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



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

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



37
38
39
40
41
42
43
44
# File 'lib/ruby_sync/util/utilities.rb', line 37

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