Class: BlueprintConfig::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/blueprint_config/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendsObject

Returns the value of attribute backends.



11
12
13
# File 'lib/blueprint_config/configuration.rb', line 11

def backends
  @backends
end

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/blueprint_config/configuration.rb', line 11

def config
  @config
end

Instance Method Details

#clear_memory!Object



79
80
81
82
83
84
85
# File 'lib/blueprint_config/configuration.rb', line 79

def clear_memory!
  memory_backend = @backends[:memory]
  return unless memory_backend

  memory_backend.clear
  reload!
end

#init(&block) ⇒ Object



24
25
26
27
28
29
# File 'lib/blueprint_config/configuration.rb', line 24

def init(&block)
  backends = BackendCollection.new
  block.call(backends)
  @backends = backends
  reload!
end

#process_erb(object) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/blueprint_config/configuration.rb', line 47

def process_erb(object)
  case object
  when String
    if object.start_with?('<%=') && object.end_with?('%>')
      ERB.new(object).result(binding)
    else
      object
    end
  when OptionsArray
    object.each_with_index { |o, index| object[index] = process_erb(o) }
  when OptionsHash
    object.each { |k, v| object[k] = process_erb(v) }
  else
    object
  end
end

#refine(&block) ⇒ Object



31
32
33
34
35
36
# File 'lib/blueprint_config/configuration.rb', line 31

def refine(&block)
  backends = @backends
  block.call(backends)
  @backends = backends
  reload!
end

#reload!Object



38
39
40
41
42
43
44
45
# File 'lib/blueprint_config/configuration.rb', line 38

def reload!
  new_config = @backends.each_with_object(OptionsHash.new) do |backend, config|
    config.deep_merge! OptionsHash.new(backend.load_keys, source: backend.source)
  end

  @config = new_config
  @config = process_erb(new_config)
end

#set(key, value = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/blueprint_config/configuration.rb', line 64

def set(key, value = nil)
  memory_backend = @backends[:memory]
  raise "Memory backend not configured. Only available in test environment." unless memory_backend

  if key.is_a?(Hash)
    # Handle hash argument: AppConfig.set(foo: { bar: 'baz' })
    key.each do |k, v|
      set(k.to_s, v)
    end
  else
    memory_backend.set(key.to_s, value)
  end
  reload!
end

#to_hObject



87
88
89
90
# File 'lib/blueprint_config/configuration.rb', line 87

def to_h
  reload! unless backends&.fresh?
  config.to_h
end

#with_sourcesObject



92
93
94
95
# File 'lib/blueprint_config/configuration.rb', line 92

def with_sources
  reload! unless backends&.fresh?
  config.with_sources
end