Class: Puppet_X::Binford2k::Itemize::Cli
- Inherits:
-
Object
- Object
- Puppet_X::Binford2k::Itemize::Cli
- Defined in:
- lib/puppet_x/binford2k/itemize/cli.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Cli
constructor
A new instance of Cli.
- #render! ⇒ Object
- #run! ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #to_yaml ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Cli
Returns a new instance of Cli.
2 3 4 |
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 2 def initialize( = {}) @options = end |
Instance Method Details
#render! ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 12 def render! run! case @options[:render] when :human puts to_s when :json puts to_json when :yaml puts to_yaml else raise "Invalid render type (#{@options[:render]})." end end |
#run! ⇒ Object
6 7 8 9 10 |
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 6 def run! runner = Puppet_X::Binford2k::Itemize::Runner.new(@options).run! @results = runner.results self end |
#to_json ⇒ Object
51 52 53 54 |
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 51 def to_json require 'json' JSON.pretty_generate(@results) end |
#to_s ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 27 def to_s name_width = 2 count_width = 2 @results.each do |kind, counts| counts.each do |name, count| name_width = [name_width, name.size].max count_width = [count_width, count.to_s.size].max end end output = "Resource usage analysis:\n" output << '=' * (name_width + count_width + 8) output << "\n" @results.each do |kind, counts| output << ">> #{kind}:\n" counts.each do |name, count| output << sprintf(" %#{name_width}s | %#{count_width}s\n", name, count) end output << "\n" end output end |
#to_yaml ⇒ Object
56 57 58 59 |
# File 'lib/puppet_x/binford2k/itemize/cli.rb', line 56 def to_yaml require 'yaml' @results.to_yaml end |