Module: Wakame::Util
- Defined in:
- lib/wakame/util.rb
Class Method Summary collapse
- .build_const(name) ⇒ Object
- .exec(command, &capture) ⇒ Object
- .gen_id(str = nil) ⇒ Object
- .new_(class_or_str, *args) ⇒ Object
-
.snake_case(const) ⇒ String
Copied from github.com/ezmobius/nanite/.
- .spawn(command, &capture) ⇒ Object
- .ssh_known_hosts_hash(hostname, key = nil) ⇒ Object
-
.to_const_path(const) ⇒ String
Copied from github.com/ezmobius/nanite/.
Class Method Details
.build_const(name) ⇒ Object
25 26 27 |
# File 'lib/wakame/util.rb', line 25 def build_const(name) name.to_s.split(/::/).inject(Object) {|c,name| c.const_get(name) } end |
.exec(command, &capture) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/wakame/util.rb', line 77 def exec(command, &capture) outputs = [] Wakame.log.debug("#{self}.exec(#{command})") cmdstat = ::Open4.popen4(command) { |pid, stdin, stdout, stderr| stdout.each { |l| capture.call(l, :stdout) if capture outputs << l } stderr.each { |l| capture.call(l, :stderr) if capture outputs << l } } Wakame.log.debug(outputs.join('')) raise "Command Failed (exit=#{cmdstat.exitstatus}): #{command}" unless cmdstat.exitstatus == 0 end |
.gen_id(str = nil) ⇒ Object
19 20 21 |
# File 'lib/wakame/util.rb', line 19 def gen_id(str=nil) Digest::SHA1.hexdigest( (str.nil? ? rand.to_s : str) ) end |
.new_(class_or_str, *args) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/wakame/util.rb', line 31 def new_(class_or_str, *args) if class_or_str.is_a? Class class_or_str.new(*args) else c = build_const(class_or_str.to_s) c.new(*args) end end |
.snake_case(const) ⇒ String
Copied from github.com/ezmobius/nanite/
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
53 54 55 56 57 58 |
# File 'lib/wakame/util.rb', line 53 def snake_case(const) const = const.to_s return const.downcase if const =~ /^[A-Z\d]+$/ const.gsub(/\B([A-Z\d]+)|([A-Z]+)(?=[A-Z][a-z]?)/, '_\&') =~ /_*(.*)/ return $+.downcase.gsub(/[_]+/, '_') end |
.spawn(command, &capture) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wakame/util.rb', line 95 def spawn(command, &capture) outputs = [] Wakame.log.debug("#{self}.spawn(#{command})") cmdstat = ::Open4.popen4(command) { |pid, stdin, stdout, stderr| stdout.each { |l| capture.call(l, :stdout) if capture outputs << l } stderr.each { |l| capture.call(l, :stderr) if capture outputs << l } } Wakame.log.debug(outputs.join('')) cmdstat end |
.ssh_known_hosts_hash(hostname, key = nil) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/wakame/util.rb', line 10 def ssh_known_hosts_hash(hostname, key=nil) # Generate 20bytes random value key = Array.new(20).collect{rand(0xFF).to_i}.pack('c*') if key.nil? "|1|#{[key].pack('m').chop}|#{[HMAC::SHA1.digest(key, hostname)].pack('m').chop}" end |
.to_const_path(const) ⇒ String
Copied from github.com/ezmobius/nanite/
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
71 72 73 |
# File 'lib/wakame/util.rb', line 71 def to_const_path(const) snake_case(const).gsub(/::/, "/") end |