Module: GreenHat::FieldHelper

Defined in:
lib/greenhat/shell/field_helper.rb

Overview

Common Helpers

Class Method Summary collapse

Class Method Details

.field_auto_complete(word, files, flags = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/greenhat/shell/field_helper.rb', line 53

def self.field_auto_complete(word, files, flags = {})
  # Prevent weird dupes
  return nil if word[-1] == ','

  # Command Manipulation
  cmd, fields = word.split('=', 2)
  complete = fields.split(',')[0..-2]
  auto = fields.split(',').last

  # Field Finder
  matches = fields_find(files, auto, flags)

  if matches.count == 1
    "--#{cmd}=#{(complete + matches).join(',')}"
  elsif matches.count > 1
    puts "#{'Field Options:'.pastel(:bright_blue)} #{matches.join(' ').pastel(:bright_green)}"

    list = [Cli.common_substr(matches.map(&:to_s))]
    "--#{cmd}=#{(complete + list).join(',')}"
  end
end

.field_auto_complete?(word) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/greenhat/shell/field_helper.rb', line 47

def self.field_auto_complete?(word)
  return false if word.blank?

  filter_auto_completes.include? word.split('=', 2).first
end

.fields_find(files, word, flags = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/greenhat/shell/field_helper.rb', line 4

def self.fields_find(files, word, flags = {})
  fields = ShellHelper.find_things(files, flags).map(&:fields).flatten.uniq

  if word.blank?
    puts 'Possible Fields:'.pastel(:bright_blue)
    puts ShellHelper.field_table(fields)

    return [] # Empty Result
  end

  list_select(fields, word)
end

.filter_auto_completesObject



41
42
43
44
45
# File 'lib/greenhat/shell/field_helper.rb', line 41

def self.filter_auto_completes
  %w[
    except exists pluck slice sort stats uniq
  ]
end

.filter_flags(word) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/greenhat/shell/field_helper.rb', line 21

def self.filter_flags(word)
  if word.blank?
    puts 'Filter Options:'.pastel(:bright_blue)
    puts ShellHelper.field_table(filter_opts, 6)
    puts

    return []
  end

  list_select(filter_opts, word)
end

.filter_optsObject



33
34
35
36
37
38
39
# File 'lib/greenhat/shell/field_helper.rb', line 33

def self.filter_opts
  %w[
    archive case combine end exact except exists json limit or page pluck
    raw reverse round slice sort start stats table_style text time_zone
    total truncate uniq
  ]
end

.list_select(list, word) ⇒ Object



17
18
19
# File 'lib/greenhat/shell/field_helper.rb', line 17

def self.list_select(list, word)
  list.select! { |x| x[/^#{Regexp.escape(word)}/] }
end