Class: Configature::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

hashify

Constructor Details

#initialize(config_dir: nil, path: nil, env: ENV, data: nil) ⇒ Config

Instance Methods =====================================================



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/configature/config.rb', line 60

def initialize(config_dir: nil, path: nil, env: ENV, data: nil)
  super(
    self.class.namespaces.map do |name, namespace|
      config = data || begin
        path ||= File.expand_path('%s.yml' % namespace.config_name, config_dir || self.class.config_dir)

        Configature::Support.yaml_if_exist(path)
      end

      [
        name,
        Configature::Support.convert_hashes(
          Configature::Data,
          namespace.__instantiate(source: config, env: env)
        )
      ]
    end.to_h
  )
end

Class Method Details

.config_dirObject

Class Methods ========================================================



15
16
17
# File 'lib/configature/config.rb', line 15

def self.config_dir
  @config_dir ||= defined?(Rails) && Rails.root.join('config/')
end

.config_dir=(dir) ⇒ Object



19
20
21
# File 'lib/configature/config.rb', line 19

def self.config_dir=(dir)
  @config_dir = dir
end

.namespace(name, file: nil, env_suffix: '', extends: nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/configature/config.rb', line 23

def self.namespace(name, file: nil, env_suffix: '', extends: nil, &block)
  extends &&= self.namespaces[extends]

  namespace = self.namespaces[name] = Configature::Namespace.new(name, env_suffix: env_suffix, extends: extends).tap do |n|
    case (block&.arity)
    when nil
      nil
    when 1
      block[n]
    else
      n.instance_eval(&block)
    end
  end

  file = (file || extends&.config_name || name).to_s

  if (file and !file.include?('.'))
    file += '.yml'
  end

  unless (self.respond_to?(name))
    iv = :"@#{name}"

    singleton_class.send(:define_method, name) do
      instance_variable_get(iv) or instance_variable_set(iv, namespace.__instantiate(
        source: Configature::Support.yaml_if_exist(File.expand_path(file, self.config_dir))
      ))
    end
  end
end

.namespacesObject



54
55
56
# File 'lib/configature/config.rb', line 54

def self.namespaces
  @namespaces ||= { }
end

Instance Method Details

#to_hObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/configature/config.rb', line 80

def to_h
  super.map do |k, v|
    [
      k,
      case (v)
      when Array
        v.map do |e|
          e.respond_to?(:to_h) ? e.to_h : e
        end
      else
        v.respond_to?(:to_h) ? v.to_h : v
      end
    ]
  end.to_h
end