Class: Configatron::Store

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/configatron/store.rb

Overview

< BasicObject

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Store

Returns a new instance of Store.



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

def initialize(attributes = {})
  @__locked = false
  @attributes = attributes || {}
  @attributes.send(:extend, DeepClone)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/configatron/store.rb', line 79

def method_missing(name, *args, &block)
  if block_given?
    yield self[name]
  else
    name = name.to_s
    if /(.+)=$/.match(name)
      return store($1, args[0])
    elsif /(.+)!/.match(name)
      key = $1
      if self.has_key?(key)
        return self[key]
      else
        raise Configatron::UndefinedKeyError.new($1)
      end
    else
      return self[name]
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  fetch(key.to_sym) do
    if @__locked
      raise Configatron::UndefinedKeyError.new("Key Not Found: #{key}")
    end
    ::Configatron::Store.new
  end
end

#configure_from_hash(hash) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/configatron/store.rb', line 55

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

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



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

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

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

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/configatron/store.rb', line 50

def key?(key)
  val = self[key.to_sym]
  !val.is_a?(Configatron::Store)
end

#lock!Object



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

def lock!
  @__locked = true
end

#nil?Boolean Also known as: blank?

Returns:

  • (Boolean)


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

def nil?
  @attributes.empty?
end

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



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

def store(key, value)
  if @__locked
    raise Configatron::LockedError.new("Locked! Can not set key #{key}!")
  end
  @attributes[key.to_sym] = value
end

#temp(&block) ⇒ Object



65
66
67
68
69
# File 'lib/configatron/store.rb', line 65

def temp(&block)
  temp_start
  yield
  temp_end
end

#temp_endObject



75
76
77
# File 'lib/configatron/store.rb', line 75

def temp_end
  @attributes = @__temp
end

#temp_startObject



71
72
73
# File 'lib/configatron/store.rb', line 71

def temp_start
  @__temp = @attributes.deep_clone
end