Class: Ballantine::Config
- Inherits:
-
Object
- Object
- Ballantine::Config
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#env ⇒ Object
Returns the value of attribute env.
-
#loaded ⇒ Object
readonly
Returns the value of attribute loaded.
-
#print_type ⇒ Object
Returns the value of attribute print_type.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
-
#get_data(key, **options) ⇒ Stirng
Value.
-
#init_file(**options) ⇒ Boolean
Result.
-
#initialize ⇒ Config
constructor
A new instance of Config.
-
#load_file(**options) ⇒ Boolean
Result.
-
#print_data(key, **options) ⇒ Boolean
Result.
- #print_log(binding) ⇒ NilClass
-
#set_data(key, value, **options) ⇒ Stirng
Value.
Constructor Details
#initialize ⇒ Config
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/ballantine/config.rb', line 20 def data @data end |
#env ⇒ Object
Returns the value of attribute env.
21 22 23 |
# File 'lib/ballantine/config.rb', line 21 def env @env end |
#loaded ⇒ Object (readonly)
Returns the value of attribute loaded.
20 21 22 |
# File 'lib/ballantine/config.rb', line 20 def loaded @loaded end |
#print_type ⇒ Object
Returns the value of attribute print_type.
21 22 23 |
# File 'lib/ballantine/config.rb', line 21 def print_type @print_type end |
#verbose ⇒ Object
Returns the value of attribute verbose.
21 22 23 |
# File 'lib/ballantine/config.rb', line 21 def verbose @verbose end |
Class Method Details
.instance ⇒ Config
Note:
singleton method
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.
99 100 101 102 |
# File 'lib/ballantine/config.rb', line 99 def get_data(key, **) load_file unless @loaded @data[key] end |
#init_file(**options) ⇒ Boolean
Returns result.
43 44 45 46 47 48 |
# File 'lib/ballantine/config.rb', line 43 def init_file(**) raise NotAllowed, "#{FILE_BALLANTINE_CONFIG} already exists." if Dir[file_path].any? && ![:force] File.write(file_path, {}) @loaded = false end |
#load_file(**options) ⇒ Boolean
Returns result.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ballantine/config.rb', line 52 def load_file(**) 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 |
#print_data(key, **options) ⇒ Boolean
Returns 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, **) 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 |
#print_log(binding) ⇒ 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.
88 89 90 91 92 93 94 |
# File 'lib/ballantine/config.rb', line 88 def set_data(key, value, **) load_file unless @loaded @data[key] = value File.write(file_path, JSON.dump(@data)) value end |