Module: Ssync::Helpers
Instance Method Summary collapse
- #action_eq?(action) ⇒ Boolean
- #aquire_lock! ⇒ Object
- #ask(config_item, question) ⇒ Object
- #config_exists?(path = config_path) ⇒ Boolean
- #config_path ⇒ Object
- #create_homedir! ⇒ Object
- #default_config_path ⇒ Object
- #display(message) ⇒ Object
- #display_error(message) ⇒ Object (also: #e)
- #display_help! ⇒ Object
- #exit_with_error!(message) ⇒ Object (also: #e!)
- #lock_path ⇒ Object
- #options_set?(*options) ⇒ Boolean
- #read_config ⇒ Object
- #read_default_config ⇒ Object
- #ssync_filename ⇒ Object
- #ssync_homedir ⇒ Object
- #write_config!(config) ⇒ Object
- #write_default_config!(config) ⇒ Object
Instance Method Details
#action_eq?(action) ⇒ Boolean
29 30 31 |
# File 'lib/ssync/helpers.rb', line 29 def action_eq?(action) Ssync::Command.action == action.to_sym end |
#aquire_lock! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ssync/helpers.rb', line 57 def aquire_lock! # better way is to write out the pid ($$) and read it back in, to make sure it's the same e! "Found a lock at #{lock_path}, is another instance of Ssync running?" if File.exist?(lock_path) begin system "touch #{lock_path}" yield ensure system "rm #{lock_path}" end end |
#ask(config_item, question) ⇒ Object
23 24 25 26 27 |
# File 'lib/ssync/helpers.rb', line 23 def ask(config_item, question) print(question + " [#{config_item}]: ") a = $stdin.readline.chomp a.empty? ? config_item : a end |
#config_exists?(path = config_path) ⇒ Boolean
33 34 35 |
# File 'lib/ssync/helpers.rb', line 33 def config_exists?(path = config_path) File.exist?(path) end |
#config_path ⇒ Object
49 50 51 |
# File 'lib/ssync/helpers.rb', line 49 def config_path "#{ssync_homedir}/#{ssync_filename}.yml" end |
#create_homedir! ⇒ Object
94 95 96 |
# File 'lib/ssync/helpers.rb', line 94 def create_homedir! `mkdir #{ssync_homedir}` unless File.exists?(ssync_homedir) end |
#default_config_path ⇒ Object
45 46 47 |
# File 'lib/ssync/helpers.rb', line 45 def default_config_path "#{ssync_homedir}/defaults.yml" end |
#display(message) ⇒ Object
7 8 9 |
# File 'lib/ssync/helpers.rb', line 7 def display() puts("[#{Time.now}] #{}") end |
#display_error(message) ⇒ Object Also known as: e
11 12 13 |
# File 'lib/ssync/helpers.rb', line 11 def display_error() display("Error! " + ) end |
#display_help! ⇒ Object
3 4 5 |
# File 'lib/ssync/helpers.rb', line 3 def display_help! display("Not implemented yet.") end |
#exit_with_error!(message) ⇒ Object Also known as: e!
15 16 17 18 |
# File 'lib/ssync/helpers.rb', line 15 def exit_with_error!() display_error() exit end |
#lock_path ⇒ Object
53 54 55 |
# File 'lib/ssync/helpers.rb', line 53 def lock_path "#{ssync_homedir}/#{ssync_filename}.lock" end |
#options_set?(*options) ⇒ Boolean
98 99 100 101 102 103 |
# File 'lib/ssync/helpers.rb', line 98 def (*) false [].flatten.each do |option| return true if Command.args.include?("#{option.to_s}") end end |
#read_config ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/ssync/helpers.rb', line 69 def read_config begin open(config_path, "r") { |f| YAML::load(f) } rescue {} end end |
#read_default_config ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/ssync/helpers.rb', line 77 def read_default_config begin open(default_config_path, "r") { |f| YAML::load(f) } rescue {} end end |
#ssync_filename ⇒ Object
41 42 43 |
# File 'lib/ssync/helpers.rb', line 41 def ssync_filename "#{read_default_config[:last_used_bucket]}" end |
#ssync_homedir ⇒ Object
37 38 39 |
# File 'lib/ssync/helpers.rb', line 37 def ssync_homedir "#{ENV['HOME']}/.ssync" end |
#write_config!(config) ⇒ Object
85 86 87 |
# File 'lib/ssync/helpers.rb', line 85 def write_config!(config) open(config_path, "w") { |f| YAML::dump(config, f) } end |
#write_default_config!(config) ⇒ Object
89 90 91 92 |
# File 'lib/ssync/helpers.rb', line 89 def write_default_config!(config) create_homedir! open(default_config_path, "w") { |f| YAML::dump(config, f) } end |