Module: GreenHat::Kind

Included in:
Thing
Defined in:
lib/greenhat/thing/kind.rb

Overview

Overall Type Parsing

Instance Method Summary collapse

Instance Method Details

#check_oj_parse?(first_line) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/greenhat/thing/kind.rb', line 46

def check_oj_parse?(first_line)
  Oj.load(first_line)
  true
rescue StandardError
  false
end

#kind_collectObject

rubobop:disable *



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/greenhat/thing/kind.rb', line 5

def kind_collect
  # If Direct Match
  if types.key? name
    self.type = name

    return true
  end

  # Check Pattern Matches
  matches = types.select do |_k, v|
    v.pattern.any? { |x| x =~ name }
  end

  # If there is only one match
  if matches.keys.count == 1
    self.type = matches.keys.first

    true

  # TODO: Prompt for smaller selection
  elsif matches.keys.count > 1
    puts 'Multiple!'
  # History Match
  elsif ThingHistory.match? name
    self.type = ThingHistory.match(name)
  else
    prompt_for_kind
  end
end

#kind_pattern_match(name) ⇒ Object

Pattern Match / Look for ‘match_` patterns and then strip name for kind type



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/greenhat/thing/kind.rb', line 89

def kind_pattern_match(name)
  pattern_match = methods.grep(/^match_/).find do |pattern|
    send(pattern).any? { |x| name =~ x }
  end

  if pattern_match
    pattern_match.to_s.split('match_', 2).last.to_sym
  else
    false
  end
end

#kind_setupObject

File Identifier



36
37
38
39
# File 'lib/greenhat/thing/kind.rb', line 36

def kind_setup
  self.kind = types.dig(type, :format)
  self.log = types.dig(type, :log)
end

#promptObject



41
42
43
44
# File 'lib/greenhat/thing/kind.rb', line 41

def prompt
  # Quiet Exit
  @prompt ||= TTY::Prompt.new(interrupt: :exit)
end

#prompt_for_kindObject

rubocop:disable Style/SymbolProc



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
# File 'lib/greenhat/thing/kind.rb', line 54

def prompt_for_kind
  # rubocop:disable Lint/Debugger
  binding.pry if ENV['DEBUG']
  # rubocop:enable Lint/Debugger

  # Default to everything
  prompt_list = types.clone

  first_line = File.open(file) { |f| f.readline }
  json = check_oj_parse?(first_line)

  if json
    if Settings.assume_json?
      self.type = 'json'
      return true
    end

    prompt_list.select! do |_k, v|
      v.to_s.include? 'json'
    end
  end

  puts "Unable to determine file type for #{name.pastel(:yellow)}"
  puts "Use '#{'json'.pastel(:cyan)}' or '#{'raw'.pastel(:cyan)}' if there are no matches (see file_types.rb)"

  option = prompt.select('Wat is this?', prompt_list.keys.sort_by(&:length), filter: true)

  # Store for later
  ThingHistory.add(name, option)

  self.type = option
end