Class: Motoko::Formatter

Inherits:
Object
  • Object
show all
Includes:
Utils::SnakeToCamel
Defined in:
lib/motoko/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::SnakeToCamel

#snake_to_camel_case

Constructor Details

#initializeFormatter

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

#columnsObject

Returns the value of attribute columns.



11
12
13
# File 'lib/motoko/formatter.rb', line 11

def columns
  @columns
end

#columns_specObject (readonly)

Returns the value of attribute columns_spec.



10
11
12
# File 'lib/motoko/formatter.rb', line 10

def columns_spec
  @columns_spec
end

#countObject

Returns the value of attribute count.



11
12
13
# File 'lib/motoko/formatter.rb', line 11

def count
  @count
end

#monoObject

Returns the value of attribute mono.



11
12
13
# File 'lib/motoko/formatter.rb', line 11

def mono
  @mono
end

#nodesObject

Returns the value of attribute nodes.



11
12
13
# File 'lib/motoko/formatter.rb', line 11

def nodes
  @nodes
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/motoko/formatter.rb', line 10

def options
  @options
end

#shortcutsObject (readonly)

Returns the value of attribute shortcuts.



10
11
12
# File 'lib/motoko/formatter.rb', line 10

def shortcuts
  @shortcuts
end

#sort_byObject

Returns the value of attribute sort_by.



11
12
13
# File 'lib/motoko/formatter.rb', line 11

def sort_by
  @sort_by
end

#wideObject

Returns the value of attribute wide.



11
12
13
# File 'lib/motoko/formatter.rb', line 11

def wide
  @wide
end

Instance Method Details

#column_resolversObject



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

#dataObject



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

#headingsObject



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_nodesObject



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_sObject



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