Class: LitCLI::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/config.rb

Overview

Default config that can be overridden from the command line or application.

@usage:

LitCLI.configure do |config|
  config.<property> = <value>
end

@precedence:

3. Defaults - initialize()
2. Application - LitCLI.configure()
1. Command line flags - cli_configure()

Constant Summary collapse

@@errors =

Track errors and only show them once.

Set.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/config.rb', line 33

def initialize()

  @statuses = {
    :info => { icon: "", color: :blue, styles: [:upcase] },
    :pass => { icon: "", color: :green, styles: [:upcase] },
    :warn => { icon: "", color: :yellow, styles: [:upcase] },
    :fail => { icon: "", color: :red, styles: [:upcase] },
    :error => { icon: "!", color: :red, styles: [:upcase] },
    :debug => { icon: "?", color: :purple, styles: [:upcase] },
  }

  @types = nil

  # Lit is disabled by default, then enabled via the `lit` command.
  # Or it can be permanently enabled, without the use of the `lit` command.
  @enabled = false

  ##
  # FLAGS.
  #
  # Flag defaults when not supplied via command line.
  ##

  # Array of statuses to filter by, for example: [:warn, :error, :fail]
  @status = nil

  # Array of types to filter by, for example: [:cat, :dog, :tree]
  @type = nil

  # Integer or float representing amount of seconds to delay each lit() by.
  @delay = 0

  ##
  # PRIVATE.
  ##

  # Boolean on whether or not to step through each lit() breakpoint.
  # Setting to true here wont enable Pry, best to enable via command line.
  @step = false

  cli_configure()
end

Instance Attribute Details

#delayObject

Returns the value of attribute delay


31
32
33
# File 'lib/config.rb', line 31

def delay
  @delay
end

#enabledObject

Returns the value of attribute enabled


24
25
26
# File 'lib/config.rb', line 24

def enabled
  @enabled
end

#statusObject

Returns the value of attribute status


29
30
31
# File 'lib/config.rb', line 29

def status
  @status
end

#statusesObject

Returns the value of attribute statuses


25
26
27
# File 'lib/config.rb', line 25

def statuses
  @statuses
end

#stepObject

Flags.


28
29
30
# File 'lib/config.rb', line 28

def step
  @step
end

#typeObject

Returns the value of attribute type


30
31
32
# File 'lib/config.rb', line 30

def type
  @type
end

#typesObject

Returns the value of attribute types


26
27
28
# File 'lib/config.rb', line 26

def types
  @types
end

Instance Method Details

#cli_configureObject

Override config from command line.


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/config.rb', line 77

def cli_configure()

  # Enable lit via the command line.
  @enabled = true if ENV['LIT_ENABLED'] && ENV['LIT_ENABLED'].to_i >= (Time.now.to_i() - 1)
  return unless @enabled

  # Convert flag string to hash.
  flags = {}
  if ENV['LIT_FLAGS'] && !ENV['LIT_FLAGS'].empty?
    ENV['LIT_FLAGS'].split().each do |flag|
      values = flag.split('=')

      key = values.shift.to_sym

      # No arguments.
      if values.empty?
        flags[key] = nil
      else
        args = values.pop.split(',')
        # Single argument.
        if args.count == 1
          flags[key] = args.first
        # Multiple arguments.
        else
          flags[key] = args
        end
      end
    end
  end

  @step = true if flags.has_key? :step
  @status = Array(flags[:status]).map(&:to_sym) if valid? flags, :status
  @type = Array(flags[:type]).map(&:to_sym) if valid? flags, :type
  @delay = flags[:delay].to_f if valid? flags, :delay
end

#valid?(flags, flag) ⇒ Boolean

Returns:

  • (Boolean)

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/config.rb', line 113

def valid? flags, flag
  # Has flag even been entered on the command line?
  unless flags.has_key? flag
    return false
  end

  if flags[flag].nil?
    error = "🔥 ERROR: Invalid argument for @#{flag}."
    unless @@errors.include? error
      @@errors.add error
      puts error
    end
    return false
  end

  true
end