Module: Gritano::CLI::Renderer

Included in:
Console::Base
Defined in:
lib/gritano/renderer.rb

Instance Method Summary collapse

Instance Method Details

#render_table(elements, *attributes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gritano/renderer.rb', line 14

def render_table(elements, *attributes)
  if elements.count > 0
    attributes_hash = {}
    attributes.each do |a|
      if a.respond_to?(:keys)
        attributes_hash = attributes_hash.merge(a)
      else
        attributes_hash[a] = nil
      end
    end
    table = Terminal::Table.new do |t|
      t << attributes_hash.map { |key, value| key }
      t << :separator
      elements.each do |element|
        row = []
        attributes_hash.each do |attribute, params|
          if params
            row << element.send(attribute, params)
          else
            row << element.send(attribute)
          end
        end
        t.add_row row
      end
    end
    render_text table.to_s
  else
    render_text "there aren't #{elements.model.name.split(':')[-1].pluralize.downcase}", :warning
  end
end

#render_text(text, level = :success) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/gritano/renderer.rb', line 4

def render_text(text, level = :success)
  if level == :success
    puts text.color(:green)
  elsif level == :warning
    puts text.color(:yellow)
  else
    puts text.color(:red)
  end
end