Class: Strata::CLI::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/strata/cli/configuration.rb

Constant Summary collapse

STRATA_CONFIG_FILE =
".strata"
PROJECT_CONFIG_FILE =
"project.yml"
DEFAULT =
{
  "api_key" => "",
  "server" => "",
  "log_level" => "info"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



19
20
21
22
# File 'lib/strata/cli/configuration.rb', line 19

def initialize
  @data = DEFAULT.merge({})
  load_config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/strata/cli/configuration.rb', line 28

def method_missing(method_name, *args, &block)
  if @data.key?(method_name.to_s)
    @data[method_name.to_s]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/strata/cli/configuration.rb', line 17

def data
  @data
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
# File 'lib/strata/cli/configuration.rb', line 24

def [](key)
  @data[key]
end

#config_file_pathObject



44
45
46
# File 'lib/strata/cli/configuration.rb', line 44

def config_file_path
  @config_file_path ||= File.expand_path(STRATA_CONFIG_FILE)
end

#get(key, type: nil) ⇒ Object

Get configuration value with type conversion



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/strata/cli/configuration.rb', line 53

def get(key, type: nil)
  value = self[key]
  return value unless type

  case type
  when :boolean
    convert_to_boolean(value)
  when :integer
    Integer(value)
  when :float
    Float(value)
  when :array
    Array(value)
  else
    value
  end
rescue ArgumentError, TypeError => e
  raise StrataError, "Invalid #{type} value for '#{key}': #{value} \n\t#{e.message}"
end

#get_for_environment(env) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/strata/cli/configuration.rb', line 78

def get_for_environment(env)
  root_config = extract_root_config
  return root_config if env.nil? || env.empty?

  env_data = @data[env.to_s]
  root_config.merge(env_data)
end

#project_config_file_pathObject



48
49
50
# File 'lib/strata/cli/configuration.rb', line 48

def project_config_file_path
  @project_config_file_path ||= File.expand_path(PROJECT_CONFIG_FILE)
end

#reload!Object



73
74
75
76
# File 'lib/strata/cli/configuration.rb', line 73

def reload!
  @data.clear
  load_config
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/strata/cli/configuration.rb', line 36

def respond_to_missing?(method_name, include_private = false)
  @data.key?(method_name.to_s) || super
end

#strata_project?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/strata/cli/configuration.rb', line 40

def strata_project?
  File.exist?(project_config_file_path)
end