Class: UltraConfig::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/ultra_config/namespace.rb

Defined Under Namespace

Classes: ObjectNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parents = [], &block) ⇒ Namespace

Returns a new instance of Namespace.



12
13
14
15
16
17
# File 'lib/ultra_config/namespace.rb', line 12

def initialize(parents = [], &block)
  @configuration = [block]
  @parents = parents

  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m) ⇒ Object



93
94
95
# File 'lib/ultra_config/namespace.rb', line 93

def method_missing(m)
  raise ObjectNotFoundError
end

Instance Attribute Details

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



9
10
11
# File 'lib/ultra_config/namespace.rb', line 9

def logger=(value)
  @logger = value
end

#objectsObject (readonly)

Returns the value of attribute objects.



10
11
12
# File 'lib/ultra_config/namespace.rb', line 10

def objects
  @objects
end

Instance Method Details

#config(name, options = {}, &block) ⇒ Object



33
34
35
36
37
# File 'lib/ultra_config/namespace.rb', line 33

def config(name, options = {}, &block)
  @objects[name] = Config.new(name, @parents, options, &block)
  define_singleton_method("#{name}=") { |value| @objects[name].value = value }
  define_singleton_method(name) { @objects[name].value }
end

#extend(&block) ⇒ Object



19
20
21
22
# File 'lib/ultra_config/namespace.rb', line 19

def extend(&block)
  @configuration << block
  self.instance_eval(&block)
end

#helper(name, &block) ⇒ Object



39
40
41
# File 'lib/ultra_config/namespace.rb', line 39

def helper(name, &block)
  define_singleton_method(name, &block)
end

#merge_hash!(hash, parents = []) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ultra_config/namespace.rb', line 61

def merge_hash!(hash, parents = [])
  hash.each do |k, v|
    options = send_chain(parents)
    if options.objects[k.to_sym].is_a?(Config)
      options.send("#{k}=", v) unless v.nil?
    elsif options.objects[k.to_sym].is_a?(Namespace)
      merge_hash!(v, parents + [k])
    else
      logger.warn { "received an unknown config #{k} with value #{v} and parents: #{parents}" }
    end
  end

  self
end

#namespace(name, &block) ⇒ Object



28
29
30
31
# File 'lib/ultra_config/namespace.rb', line 28

def namespace(name, &block)
  @objects[name] = Namespace.new(@parents + [name], &block)
  define_singleton_method(name) { @objects[name] }
end

#resetObject



43
44
45
46
# File 'lib/ultra_config/namespace.rb', line 43

def reset
  @objects = {}
  @configuration.each { |config| self.instance_eval(&config) }
end

#setting(name, value) ⇒ Object



24
25
26
# File 'lib/ultra_config/namespace.rb', line 24

def setting(name, value)
  Settings.set(name, value)
end

#to_hObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ultra_config/namespace.rb', line 48

def to_h
  hash = {}
  @objects.each do |name, object|
    if object.is_a?(Config)
      hash[name] = object.value
    else
      hash[name] = object.to_h
    end
  end

  hash
end

#to_sanitized_hObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ultra_config/namespace.rb', line 76

def to_sanitized_h
  hash = {}
  @objects.each do |name, object|
    if object.is_a?(Config)
      if object.sanitize?
        hash[name] = object.value.nil? ? nil : '*****'
      else
        hash[name] = object.value
      end
    else
      hash[name] = object.to_sanitized_h
    end
  end

  hash
end