Module: Brat::CLI::Helpers

Extended by:
Helpers
Included in:
Brat::CLI, Helpers, Help, Shell
Defined in:
lib/brat/cli_helpers.rb

Overview

Defines methods related to CLI output and formatting.

Instance Method Summary collapse

Instance Method Details

#actions_tableString

Table with available commands.

Returns:

  • (String)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/brat/cli_helpers.rb', line 54

def actions_table
  client = Brat::Client.new(endpoint: '')
  actions = Brat.actions
  methods = []

  actions.each do |action|
    methods << {
      name: action,
      owner: client.method(action).owner.to_s.gsub('Brat::Client::', '')
    }
  end

  owners = methods.map {|m| m[:owner]}.uniq.sort
  methods_c = methods.group_by {|m| m[:owner]}
  methods_c = methods_c.map {|_, v| [_, v.sort_by {|hv| hv[:name]}] }
  methods_c = Hash[methods_c.sort_by(&:first).map {|k, v| [k, v]}]
  max_column_length = methods_c.values.max_by(&:size).size

  rows = max_column_length.times.map do |i|
    methods_c.keys.map do |key|
      methods_c[key][i] ? methods_c[key][i][:name] : ''
    end
  end

  table do |t|
    t.title = "Available commands (#{actions.size} total)"
    t.headings = owners

    rows.each do |row|
      t.add_row row
    end
  end
end

#brat_helper(cmd, args = []) ⇒ Object

Helper function to call Brat commands with args.



164
165
166
167
168
169
170
171
172
173
# File 'lib/brat/cli_helpers.rb', line 164

def brat_helper(cmd, args=[])
  begin
    data = args.any? ? Brat.send(cmd, *args) : Brat.send(cmd)
  rescue => e
    puts e.message
    yield if block_given?
  end

  data
end

#confirm_command(cmd) ⇒ String

Confirms command with a desctructive action.

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brat/cli_helpers.rb', line 39

def confirm_command(cmd)
  if cmd.start_with?('remove_') || cmd.start_with?('delete_')
    puts "Are you sure? (y/n)"
    if %w(y yes).include?($stdin.gets.to_s.strip.downcase)
      puts 'Proceeding..'
    else
      puts 'Command aborted.'
      exit(1)
    end
  end
end

#excluded_fields(args) ⇒ Array

Returns filtered excluded fields.

Returns:

  • (Array)


20
21
22
23
24
25
26
# File 'lib/brat/cli_helpers.rb', line 20

def excluded_fields(args)
  if args.any? && args.last.start_with?('--except=')
    args.last.gsub('--except=', '').split(',')
  else
    []
  end
end

#multiple_record_table(data, cmd, args) ⇒ String

Table for multiple records.

Returns:

  • (String)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/brat/cli_helpers.rb', line 131

def multiple_record_table(data, cmd, args)
  return 'No data' if data.empty?

  arr = data.map(&:to_h)
  keys = arr.first.keys.sort {|x, y| x.to_s <=> y.to_s }
  keys = keys & required_fields(args) if required_fields(args).any?
  keys = keys - excluded_fields(args)

  table do |t|
    t.title = "Brat.#{cmd} #{args.join(', ')}"
    t.headings = keys

    arr.each_with_index do |hash, index|
      values = []

      keys.each do |key|
        case value = hash[key]
        when Hash
          value = 'Hash'
        when nil
          value = 'null'
        end

        values << value
      end

      t.add_row values
      t.add_separator unless arr.size - 1 == index
    end
  end
end

#output_table(cmd, args, data) ⇒ String

Decides which table to use.

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
100
# File 'lib/brat/cli_helpers.rb', line 91

def output_table(cmd, args, data)
  case data
  when Brat::ObjectifiedHash
    puts single_record_table(data, cmd, args)
  when Array
    puts multiple_record_table(data, cmd, args)
  else
    puts data.inspect
  end
end

#required_fields(args) ⇒ Array

Returns filtered required fields.

Returns:

  • (Array)


9
10
11
12
13
14
15
# File 'lib/brat/cli_helpers.rb', line 9

def required_fields(args)
  if args.any? && args.last.start_with?('--only=')
    args.last.gsub('--only=', '').split(',')
  else
    []
  end
end

#single_record_table(data, cmd, args) ⇒ String

Table for a single record.

Returns:

  • (String)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/brat/cli_helpers.rb', line 105

def single_record_table(data, cmd, args)
  hash = data.to_h
  keys = hash.keys.sort {|x, y| x.to_s <=> y.to_s }
  keys = keys & required_fields(args) if required_fields(args).any?
  keys = keys - excluded_fields(args)

  table do |t|
    t.title = "Brat.#{cmd} #{args.join(', ')}"

    keys.each_with_index do |key, index|
      case value = hash[key]
      when Hash
        value = 'Hash'
      when nil
        value = 'null'
      end

      t.add_row [key, value]
      t.add_separator unless keys.size - 1 == index
    end
  end
end

#valid_command?(cmd) ⇒ Boolean

Confirms command is valid.

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/brat/cli_helpers.rb', line 31

def valid_command?(cmd)
  command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym
  Brat.actions.include?(command)
end