Class: Ballantine::Config

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

Constant Summary collapse

FILE_BALLANTINE_CONFIG =
".ballantine.json"
ENV_LOCAL =
"local"
ENV_GLOBAL =
"global"
TYPE_TERMINAL =
"terminal"
TYPE_SLACK =
"slack"
AVAILABLE_ENVIRONMENTS =
[
  ENV_LOCAL,
  ENV_GLOBAL,
].freeze
KEY_SLACK_WEBHOOK =
"slack_webhook"
AVAILABLE_KEYS =
[
  KEY_SLACK_WEBHOOK,
].freeze
AVAILABLE_PRINT_INSTANCE_VARIABLES =
[:@name, :@from, :@to].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



33
34
35
36
37
38
39
# File 'lib/ballantine/config.rb', line 33

def initialize
  @env = ENV_LOCAL
  @data = {}
  @loaded = false
  @print_type = TYPE_TERMINAL
  @verbose = false
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/ballantine/config.rb', line 20

def data
  @data
end

#envObject

Returns the value of attribute env.



21
22
23
# File 'lib/ballantine/config.rb', line 21

def env
  @env
end

#loadedObject (readonly)

Returns the value of attribute loaded.



20
21
22
# File 'lib/ballantine/config.rb', line 20

def loaded
  @loaded
end

Returns the value of attribute print_type.



21
22
23
# File 'lib/ballantine/config.rb', line 21

def print_type
  @print_type
end

#verboseObject

Returns the value of attribute verbose.



21
22
23
# File 'lib/ballantine/config.rb', line 21

def verbose
  @verbose
end

Class Method Details

.instanceConfig

Note:

singleton method

Returns:



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

def instance(...)
  return @_instance if defined?(@_instance)

  @_instance = new(...)
end

Instance Method Details

#get_data(key, **options) ⇒ Stirng

Returns value.

Parameters:

Returns:

  • (Stirng)

    value



99
100
101
102
# File 'lib/ballantine/config.rb', line 99

def get_data(key, **options)
  load_file unless @loaded
  @data[key]
end

#init_file(**options) ⇒ Boolean

Returns result.

Parameters:

  • options (Hash)

Returns:

  • (Boolean)

    result

Raises:



43
44
45
46
47
48
# File 'lib/ballantine/config.rb', line 43

def init_file(**options)
  raise NotAllowed, "#{FILE_BALLANTINE_CONFIG} already exists." if Dir[file_path].any? && !options[:force]

  File.write(file_path, {})
  @loaded = false
end

#load_file(**options) ⇒ Boolean

Returns result.

Parameters:

  • options (Hash)

Returns:

  • (Boolean)

    result

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ballantine/config.rb', line 52

def load_file(**options)
  return false if @loaded
  raise NotAllowed, "Can't find #{FILE_BALLANTINE_CONFIG}" if Dir[file_path].empty?

  JSON.parse(File.read(file_path)).each do |key, value|
    next unless AVAILABLE_KEYS.include?(key)

    @data[key] = value
  end

  @loaded = true
end

Returns result.

Parameters:

Returns:

  • (Boolean)

    result



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ballantine/config.rb', line 68

def print_data(key, **options)
  load_file unless @loaded

  if key
    raise InvalidParameter, "Key must be within #{AVAILABLE_KEYS}" unless AVAILABLE_KEYS.include?(key)

    puts @data[key]
  else
    @data.each do |key, value|
      puts "#{key}: #{value}"
    end
  end

  true
end

Parameters:

  • binding (Binding)

Returns:

  • (NilClass)


106
107
108
109
110
111
112
113
114
# File 'lib/ballantine/config.rb', line 106

def print_log(binding)
  method = caller(1..1)[0][/`([^']*)'/, 1]

  puts [
    "#{binding.receiver.class.name}##{method}",
    (binding.receiver.instance_variables & AVAILABLE_PRINT_INSTANCE_VARIABLES).map { |var| "#{var}:#{binding.receiver.instance_variable_get(var).inspect}" }.join(" "),
    binding.receiver.method(method).parameters.map { |_, arg| arg }.map { |arg| "#{arg}:#{binding.local_variable_get(arg)}" }.join(" "),
  ].compact.join("\t")
end

#set_data(key, value, **options) ⇒ Stirng

Returns value.

Parameters:

Returns:

  • (Stirng)

    value



88
89
90
91
92
93
94
# File 'lib/ballantine/config.rb', line 88

def set_data(key, value, **options)
  load_file unless @loaded

  @data[key] = value
  File.write(file_path, JSON.dump(@data))
  value
end