Module: BoshExtensions
- Included in:
- Object
- Defined in:
- lib/bosh/gen/core-ext.rb
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #err(message) ⇒ Object
- #err_nl ⇒ Object
- #format_time(time) ⇒ Object
- #header(message, filler = '-') ⇒ Object
- #load_yaml_file(path, expected_type = Hash) ⇒ Object
- #nl(count = 1) ⇒ Object
- #pluralize(number, singular, plural = nil) ⇒ Object
- #pretty_size(what, prec = 1) ⇒ Object
- #quit(message = nil) ⇒ Object
- #read_yaml_file(path) ⇒ Object
- #say(message, sep = "\n") ⇒ Object
- #terminal_width ⇒ Fixnum
- #warning(message) ⇒ Object
- #with_indent(indent) ⇒ Object
- #write_yaml(manifest, path) ⇒ Object
Instance Method Details
#blank? ⇒ Boolean
40 41 42 |
# File 'lib/bosh/gen/core-ext.rb', line 40 def blank? self.to_s.blank? end |
#err(message) ⇒ Object
31 32 33 |
# File 'lib/bosh/gen/core-ext.rb', line 31 def err() raise Bosh::Cli::CliError, end |
#err_nl ⇒ Object
27 28 29 |
# File 'lib/bosh/gen/core-ext.rb', line 27 def err_nl warn('') end |
#format_time(time) ⇒ Object
65 66 67 68 |
# File 'lib/bosh/gen/core-ext.rb', line 65 def format_time(time) ts = time.to_i sprintf("%02d:%02d:%02d", ts / 3600, (ts / 60) % 60, ts % 60); end |
#header(message, filler = '-') ⇒ Object
17 18 19 20 21 |
# File 'lib/bosh/gen/core-ext.rb', line 17 def header(, filler = '-') say("\n") say() say(filler.to_s * .size) end |
#load_yaml_file(path, expected_type = Hash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bosh/gen/core-ext.rb', line 70 def load_yaml_file(path, expected_type = Hash) yaml_str = read_yaml_file(path) yaml = Psych::load(yaml_str) if expected_type && !yaml.is_a?(expected_type) err("Incorrect YAML structure in '#{path}': expected #{expected_type} at the root".make_red) end yaml end |
#nl(count = 1) ⇒ Object
23 24 25 |
# File 'lib/bosh/gen/core-ext.rb', line 23 def nl(count = 1) say("\n" * count) end |
#pluralize(number, singular, plural = nil) ⇒ Object
60 61 62 63 |
# File 'lib/bosh/gen/core-ext.rb', line 60 def pluralize(number, singular, plural = nil) plural = plural || "#{singular}s" number == 1 ? "1 #{singular}" : "#{number} #{plural}" end |
#pretty_size(what, prec = 1) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/bosh/gen/core-ext.rb', line 44 def pretty_size(what, prec=1) if what.is_a?(String) && File.exists?(what) size = File.size(what) else size = what.to_i end return "NA" unless size return "#{size}B" if size < 1024 return sprintf("%.#{prec}fK", size/1024.0) if size < (1024*1024) if size < (1024*1024*1024) return sprintf("%.#{prec}fM", size/(1024.0*1024.0)) end sprintf("%.#{prec}fG", size/(1024.0*1024.0*1024.0)) end |
#quit(message = nil) ⇒ Object
35 36 37 38 |
# File 'lib/bosh/gen/core-ext.rb', line 35 def quit( = nil) say() raise Bosh::Cli::GracefulExit, end |
#read_yaml_file(path) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bosh/gen/core-ext.rb', line 81 def read_yaml_file(path) err("Cannot find file '#{path}'".make_red) unless File.exist?(path) begin yaml_str = ERB.new(File.read(path)).result rescue SystemCallError => e err("Cannot load YAML file at '#{path}': #{e}".make_red) end begin Bosh::Cli::YamlHelper.check_duplicate_keys(yaml_str) rescue Exception => e # on ruby 1.9.3 Psych::SyntaxError isn't a StandardError err("Incorrect YAML structure in '#{path}': #{e}".make_red) end yaml_str end |
#say(message, sep = "\n") ⇒ Object
3 4 5 6 7 8 |
# File 'lib/bosh/gen/core-ext.rb', line 3 def say(, sep = "\n") return unless Bosh::Cli::Config.output && = .dup.to_s sep = "" if [-1] == sep Bosh::Cli::Config.output.print("#{$indent}#{}#{sep}") end |
#terminal_width ⇒ Fixnum
105 106 107 |
# File 'lib/bosh/gen/core-ext.rb', line 105 def terminal_width STDIN.tty? ? [HighLine::SystemExtensions.terminal_size[0], 120].min : 80 end |
#warning(message) ⇒ Object
109 110 111 |
# File 'lib/bosh/gen/core-ext.rb', line 109 def warning() warn("[WARNING] #{}".make_yellow) end |
#with_indent(indent) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/bosh/gen/core-ext.rb', line 10 def with_indent(indent) old_indent, $indent = $indent, old_indent.to_s + indent.to_s yield ensure $indent = old_indent end |
#write_yaml(manifest, path) ⇒ Object
98 99 100 101 102 |
# File 'lib/bosh/gen/core-ext.rb', line 98 def write_yaml(manifest, path) File.open(path, "w+") do |f| f.write(manifest.to_yaml) end end |