Class: Configatron::Store

Inherits:
BasicObject
Extended by:
Forwardable
Defined in:
lib/configatron/store.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_store, name = 'configatron', attributes = {}) ⇒ Store

Returns a new instance of Store.



7
8
9
10
11
# File 'lib/configatron/store.rb', line 7

def initialize(root_store, name='configatron', attributes={})
  @root_store = root_store
  @name = name
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



86
87
88
# File 'lib/configatron/store.rb', line 86

def method_missing(name, *args, &block)
  do_lookup(name, *args, &block)
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/configatron/store.rb', line 13

def [](key)
  val = fetch(key.to_sym) do
    if @root_store.locked?
      ::Kernel.raise ::Configatron::UndefinedKeyError.new("Key not found: #{key} (for locked #{self})")
    end
    ::Configatron::Store.new(@root_store, "#{@name}.#{key}")
  end
  return val
end

#clone(cloned = {}) ⇒ Object

Needed for deep_clone to actually clone this object



91
92
93
94
95
96
97
# File 'lib/configatron/store.rb', line 91

def clone(cloned={})
  root_store = DeepClone.deep_clone(@root_store, cloned)
  name = DeepClone.deep_clone(@name, cloned)
  attributes = DeepClone.deep_clone(@attributes, cloned)

  Store.new(root_store, name, attributes)
end

#configure_from_hash(hash) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/configatron/store.rb', line 50

def configure_from_hash(hash)
  hash.each do |key, value|
    if ::Hash === value
      self[key].configure_from_hash(value)
    else
      store(key, value)
    end
  end
end

#fetch(key, default_value = nil, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/configatron/store.rb', line 30

def fetch(key, default_value = nil, &block)
  val = @attributes[key.to_sym]
  if val == nil
    if block
      val = block.call
    elsif default_value
      val = default_value
    end
    store(key, val)
  end
  if ::Configatron::Proc === val
    val = val.call
  end
  return val
end

#inspectObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/configatron/store.rb', line 64

def inspect
  f_out = []
  @attributes.each do |k, v|
    if ::Configatron::Store === v
      v.inspect.each_line do |line|
        if line.match(/\n/)
          line.each_line do |l|
            l.strip!
            f_out << l
          end
        else
          line.strip!
          f_out << line
        end
      end
    else
      f_out << "#{@name}.#{k} = #{v.inspect}"
    end
  end
  f_out.compact.sort.join("\n")
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


46
47
48
# File 'lib/configatron/store.rb', line 46

def key?(key)
  @attributes.key?(key.to_sym)
end

#nil?Boolean

So that we keep backward-compatibility in case people are using nil? to check configatron settings:

Returns:

  • (Boolean)


113
114
115
# File 'lib/configatron/store.rb', line 113

def nil?
  false
end

#store(key, value) ⇒ Object Also known as: []=



23
24
25
26
27
28
# File 'lib/configatron/store.rb', line 23

def store(key, value)
  if @root_store.locked?
    ::Kernel.raise ::Configatron::LockedError.new("Cannot set key #{key} for locked #{self}")
  end
  @attributes[key.to_sym] = value
end

#to_aryObject

So that puts works (it expects the object to respond to to_ary)



107
108
109
# File 'lib/configatron/store.rb', line 107

def to_ary
  nil
end

#to_hObject Also known as: to_hash



99
100
101
102
103
104
# File 'lib/configatron/store.rb', line 99

def to_h
  @attributes.each_with_object({}) do |(k, v), h|
    v = v.call if ::Configatron::Proc === v
    h[k] = Store === v ? v.to_h : v
  end
end

#to_sObject



60
61
62
# File 'lib/configatron/store.rb', line 60

def to_s
  @name
end