Class: Motoko::Formatter
- Inherits:
-
Object
- Object
- Motoko::Formatter
- Includes:
- Utils::SnakeToCamel
- Defined in:
- lib/motoko/formatter.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#columns_spec ⇒ Object
readonly
Returns the value of attribute columns_spec.
-
#count ⇒ Object
Returns the value of attribute count.
-
#mono ⇒ Object
Returns the value of attribute mono.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#shortcuts ⇒ Object
readonly
Returns the value of attribute shortcuts.
-
#sort_by ⇒ Object
Returns the value of attribute sort_by.
-
#wide ⇒ Object
Returns the value of attribute wide.
Instance Method Summary collapse
- #column_resolvers ⇒ Object
- #data ⇒ Object
- #headings ⇒ Object
-
#initialize ⇒ Formatter
constructor
A new instance of Formatter.
- #sorted_nodes ⇒ Object
- #to_s ⇒ Object
Methods included from Utils::SnakeToCamel
Constructor Details
#initialize ⇒ Formatter
Returns a new instance of Formatter.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/motoko/formatter.rb', line 15 def initialize config = Config.new @nodes = [] @columns = config.columns @mono = false @wide = false @count = false @sort_by = config.sort_by @columns_spec = config.columns_spec @shortcuts = config.shortcuts end |
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
11 12 13 |
# File 'lib/motoko/formatter.rb', line 11 def columns @columns end |
#columns_spec ⇒ Object (readonly)
Returns the value of attribute columns_spec.
10 11 12 |
# File 'lib/motoko/formatter.rb', line 10 def columns_spec @columns_spec end |
#count ⇒ Object
Returns the value of attribute count.
11 12 13 |
# File 'lib/motoko/formatter.rb', line 11 def count @count end |
#mono ⇒ Object
Returns the value of attribute mono.
11 12 13 |
# File 'lib/motoko/formatter.rb', line 11 def mono @mono end |
#nodes ⇒ Object
Returns the value of attribute nodes.
11 12 13 |
# File 'lib/motoko/formatter.rb', line 11 def nodes @nodes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/motoko/formatter.rb', line 10 def end |
#shortcuts ⇒ Object (readonly)
Returns the value of attribute shortcuts.
10 11 12 |
# File 'lib/motoko/formatter.rb', line 10 def shortcuts @shortcuts end |
#sort_by ⇒ Object
Returns the value of attribute sort_by.
11 12 13 |
# File 'lib/motoko/formatter.rb', line 11 def sort_by @sort_by end |
#wide ⇒ Object
Returns the value of attribute wide.
11 12 13 |
# File 'lib/motoko/formatter.rb', line 11 def wide @wide end |
Instance Method Details
#column_resolvers ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/motoko/formatter.rb', line 42 def column_resolvers @column_resolvers ||= columns.map do |column| klass = columns_spec[column].delete('resolver') || 'Fact' Object.const_get("Motoko::Resolvers::#{snake_to_camel_case(klass)}").new(column, columns_spec[column]) end end |
#data ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/motoko/formatter.rb', line 50 def data return @data if @data @data = sorted_nodes.map! do |node| column_resolvers.map do |column| column.value(node) end end @data.skittlize!(split: "\n", join: ', ') unless mono @data end |
#headings ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/motoko/formatter.rb', line 64 def headings column_resolvers.each_with_index.map do |column, idx| name = column.human_name if count different_values = data.map { |line| line[idx] }.uniq.compact.count name += " (#{different_values})" if different_values > 1 end { value: mono ? name : "\e[1m#{name}\e[0m", alignment: :center, } end end |
#sorted_nodes ⇒ Object
78 79 80 |
# File 'lib/motoko/formatter.rb', line 78 def sorted_nodes nodes.sort_by { |a| sort_by.map { |c| a.fact(c) || '' } + [a.identity] } end |
#to_s ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/motoko/formatter.rb', line 27 def to_s return '' if nodes.empty? @rows = nil @column_resolvers = nil columns.uniq! table = ::Terminal::Table.new headings: headings, rows: data column_resolvers.each_with_index do |column, idx| table.align_column(idx, column.align) if column.align end table.to_s end |