Module: Rubber::Util
Instance Method Summary collapse
- #camelcase(str) ⇒ Object
-
#clean_indent(str) ⇒ Object
remove leading whitespace from "here" strings so they look good in code skips empty lines.
- #fatal(msg, code = 1) ⇒ Object
- #has_asset_pipeline? ⇒ Boolean
- #is_bundler? ⇒ Boolean
- #is_instance_id?(str) ⇒ Boolean
- #is_internet_gateway_id?(str) ⇒ Boolean
- #is_rails? ⇒ Boolean
- #parse_aliases(instance_aliases) ⇒ Object
- #prompt(name, desc, required = false, default = nil) ⇒ Object
-
#retry_on_failure(*exception_list) ⇒ Object
execute the given block, retrying only when one of the given exceptions is raised.
- #stringify(val) ⇒ Object
- #stringify_keys(map) ⇒ Object
-
#sudo_open(path, perms, &block) ⇒ Object
Opens the file for writing by root.
- #symbolize_keys(map) ⇒ Object
Instance Method Details
#camelcase(str) ⇒ Object
118 119 120 |
# File 'lib/rubber/util.rb', line 118 def camelcase(str) str.split('_').map{ |part| part.capitalize }.join end |
#clean_indent(str) ⇒ Object
remove leading whitespace from "here" strings so they look good in code skips empty lines
88 89 90 91 92 93 94 95 96 |
# File 'lib/rubber/util.rb', line 88 def clean_indent(str) str.lines.collect do |line| if line =~ /\S/ # line has at least one non-whitespace character line.lstrip else line end end.join() end |
#fatal(msg, code = 1) ⇒ Object
81 82 83 84 |
# File 'lib/rubber/util.rb', line 81 def fatal(msg, code=1) puts msg exit code end |
#has_asset_pipeline? ⇒ Boolean
63 64 65 |
# File 'lib/rubber/util.rb', line 63 def has_asset_pipeline? is_rails? && Dir["#{Rubber.root}/*/assets"].size > 0 end |
#is_bundler? ⇒ Boolean
59 60 61 |
# File 'lib/rubber/util.rb', line 59 def is_bundler? File.exist?(File.join(Rubber.root, 'Gemfile')) end |
#is_instance_id?(str) ⇒ Boolean
122 123 124 |
# File 'lib/rubber/util.rb', line 122 def is_instance_id?(str) str =~ /^i-[a-z0-9]+$/ end |
#is_internet_gateway_id?(str) ⇒ Boolean
126 127 128 |
# File 'lib/rubber/util.rb', line 126 def is_internet_gateway_id?(str) str =~ /^igw-[a-z0-9]+$/ end |
#is_rails? ⇒ Boolean
55 56 57 |
# File 'lib/rubber/util.rb', line 55 def is_rails? File.exist?(File.join(Rubber.root, 'config', 'boot.rb')) end |
#parse_aliases(instance_aliases) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rubber/util.rb', line 34 def parse_aliases(instance_aliases) aliases = [] alias_patterns = instance_aliases.to_s.strip.split(/\s*,\s*/) alias_patterns.each do |a| if a =~ /~/ range = a.split(/\s*~\s*/) range_items = (range.first..range.last).to_a raise "Invalid range, '#{a}', sequence generated no items" if range_items.size == 0 aliases.concat(range_items) else aliases << a end end return aliases end |
#prompt(name, desc, required = false, default = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rubber/util.rb', line 67 def prompt(name, desc, required=false, default=nil) value = ENV.delete(name) msg = "#{desc}" msg << " [#{default}]" if default msg << ": " unless value print msg value = gets end value = value.size == 0 ? default : value fatal "#{name} is required, pass using environment or enter at prompt" if required && ! value return value end |
#retry_on_failure(*exception_list) ⇒ Object
execute the given block, retrying only when one of the given exceptions is raised
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rubber/util.rb', line 99 def retry_on_failure(*exception_list) opts = exception_list.last.is_a?(Hash) ? exception_list.pop : {} opts = {:retry_count => 3}.merge(opts) retry_count = opts[:retry_count] begin yield rescue *exception_list => e if retry_count > 0 retry_count -= 1 Rubber.logger.info "Exception, trying again #{retry_count} more times" sleep opts[:retry_sleep].to_i if opts[:retry_sleep] retry else Rubber.logger.error "Too many exceptions...re-raising" raise end end end |
#stringify(val) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rubber/util.rb', line 20 def stringify(val) case val when String val when Hash val.inject({}) {|h, a| h[stringify(a[0])] = stringify(a[1]); h} when Enumerable val.collect {|v| stringify(v)} else val.to_s end end |
#stringify_keys(map) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/rubber/util.rb', line 13 def stringify_keys(map) map.inject({}) do |, (key, value)| [key.to_s || key] = value end end |
#sudo_open(path, perms, &block) ⇒ Object
Opens the file for writing by root
51 52 53 |
# File 'lib/rubber/util.rb', line 51 def sudo_open(path, perms, &block) open("|sudo tee #{path} > /dev/null", perms, &block) end |
#symbolize_keys(map) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/rubber/util.rb', line 6 def symbolize_keys(map) map.inject({}) do |, (key, value)| [key.to_sym || key] = value end end |