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 = {}, path = []) ⇒ Store

Returns a new instance of Store


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/configatron/store.rb', line 7

def initialize(root_store, name='configatron', attributes={}, path=[])
  @root_store = root_store
  @name = name
  @attributes = attributes

  # We could derive @name from @path, though this would break
  # backwards-compatibility.
  @path = path

  @cow = root_store.__cow
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


125
126
127
# File 'lib/configatron/store.rb', line 125

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

Instance Method Details

#[](key) ⇒ Object


41
42
43
44
45
46
47
48
49
# File 'lib/configatron/store.rb', line 41

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}", {}, @path + [key])
  end
  return val
end

#__cowObject


28
29
30
# File 'lib/configatron/store.rb', line 28

def __cow
  @cow
end

#__cow_cloneObject


32
33
34
35
36
37
38
39
# File 'lib/configatron/store.rb', line 32

def __cow_clone
  # A temp has started since the last time this was written
  if @root_store.__cow != @cow
    self.clone
  else
    self
  end
end

#cloneObject


19
20
21
22
23
24
25
26
# File 'lib/configatron/store.rb', line 19

def clone
  Store.new(
    @root_store,
    @name,
    @attributes.clone,
    @path
    )
end

#configure_from_hash(hash) ⇒ Object


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

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


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

def fetch(key, default_value = nil, &block)
  key = key.to_sym
  if key?(key)
    val = @attributes[key]
  else
    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


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/configatron/store.rb', line 103

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)

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

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)

143
144
145
# File 'lib/configatron/store.rb', line 143

def nil?
  false
end

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


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/configatron/store.rb', line 51

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

  key = key.to_sym
  if @root_store.__cow != @cow
    copy = @root_store.__cow_path(@path)
    # Cow should now match, so this won't recurse. (Note this is
    # not particularly thread-safe.)
    copy.store(key, value)
  else
    @attributes[key] = value
  end
end

#to_aryObject

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


137
138
139
# File 'lib/configatron/store.rb', line 137

def to_ary
  nil
end

#to_hObject Also known as: to_hash


129
130
131
132
133
134
# File 'lib/configatron/store.rb', line 129

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


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

def to_s
  @name
end