Module: ConfigDefault

Extended by:
ConfigDefault
Included in:
ConfigDefault
Defined in:
lib/config_default.rb,
lib/config_default/version.rb

Defined Under Namespace

Modules: RailsApplicationConfigurationPatch, RailsApplicationPatch Classes: Config, Struct

Constant Summary collapse

VERSION =
"0.6.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/config_default.rb', line 13

def config
  @config
end

Instance Method Details

#apply_rails_patch!Object



21
22
23
24
25
26
# File 'lib/config_default.rb', line 21

def apply_rails_patch!
  return unless Object.const_defined?(:Rails)

  Rails::Application.prepend(ConfigDefault::RailsApplicationPatch)
  Rails::Application::Configuration.prepend(ConfigDefault::RailsApplicationConfigurationPatch)
end

#configure {|config| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/config_default.rb', line 17

def configure
  yield(config) if block_given?
end

#hash(name, key: Rails.env, symbolize_keys: false, deep_symbolize_keys: false) ⇒ Object

Raises:

  • (Errno::ENOENT)


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
53
54
# File 'lib/config_default.rb', line 28

def hash(name, key: Rails.env, symbolize_keys: false, deep_symbolize_keys: false)
  path1 = File.join(config.config_path, "#{name}.#{config.postfix}.yml")
  path2 = File.join(config.config_path, "#{name}.yml")

  exist1 = File.exist?(path1)
  exist2 = File.exist?(path2)

  raise Errno::ENOENT.new("#{path1} && #{path2}") unless exist1 || exist2

  config1 = exist1 ? ActiveSupport::ConfigurationFile.parse(path1) : {}
  config2 = exist2 ? ActiveSupport::ConfigurationFile.parse(path2) : {}

  if key
    config1 = config1[key] || {}
    config2 = config2[key] || {}
  end

  data = config1.deep_merge(config2)

  if deep_symbolize_keys
    data.deep_symbolize_keys
  elsif symbolize_keys
    data.symbolize_keys
  else
    data
  end
end

#struct(name, key: Rails.env, recursive: false, allow_nil: false, &block) ⇒ Object



56
57
58
59
60
61
# File 'lib/config_default.rb', line 56

def struct(name, key: Rails.env, recursive: false, allow_nil: false, &block)
  attributes = hash(name, key: key)
  struct = ConfigDefault::Struct.new(attributes, recursive: recursive, allow_nil: allow_nil)
  struct.class_eval(&block) if block
  struct
end