Class: Contextizer::Configuration

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



22
23
24
# File 'lib/contextizer/configuration.rb', line 22

def initialize(options)
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



26
27
28
29
30
31
32
33
# File 'lib/contextizer/configuration.rb', line 26

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

Class Method Details

.deep_merge(hash1, hash2) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/contextizer/configuration.rb', line 51

def self.deep_merge(hash1, hash2)
  hash1.merge(hash2) do |key, old_val, new_val|
    if old_val.is_a?(Hash) && new_val.is_a?(Hash)
      deep_merge(old_val, new_val)
    else
      new_val
    end
  end
end

.find_project_config(path = Dir.pwd) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contextizer/configuration.rb', line 39

def self.find_project_config(path = Dir.pwd)
  path = Pathname.new(path)
  loop do
    config_file = path.join(".contextizer.yml")
    return config_file if config_file.exist?
    break if path.root?

    path = path.parent
  end
  nil
end

.load(cli_options = {}) ⇒ Object

CLI options > Project Config > Default Config



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/contextizer/configuration.rb', line 9

def self.load(cli_options = {})
  default_config_path = Pathname.new(__dir__).join("../../config/default.yml")
  default_config = YAML.load_file(default_config_path)

  project_config_path = find_project_config
  project_config = project_config_path ? YAML.load_file(project_config_path) : {}

  merged_config = deep_merge(default_config, project_config)
  merged_config = deep_merge(merged_config, cli_options)

  new(merged_config)
end

Instance Method Details

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/contextizer/configuration.rb', line 35

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