Class: Configatron::RootStore

Inherits:
BasicObject
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/configatron/root_store.rb

Overview

This is the root configatron object, and contains methods which operate on the entire configatron hierarchy.

Constant Summary collapse

@@cow =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRootStore

Returns a new instance of RootStore.


20
21
22
23
24
25
26
27
28
# File 'lib/configatron/root_store.rb', line 20

def initialize
  @locked = false
  @cow = nil

  @temporary_locks = []
  @temporary_states = []

  reset!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


57
58
59
# File 'lib/configatron/root_store.rb', line 57

def method_missing(name, *args, &block)
  store.__send__(name, *args, &block)
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store


10
11
12
# File 'lib/configatron/root_store.rb', line 10

def store
  @store
end

Instance Method Details

#__cowObject


30
31
32
# File 'lib/configatron/root_store.rb', line 30

def __cow
  @cow
end

#__cow_path(path) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/configatron/root_store.rb', line 34

def __cow_path(path)
  start = @store.__cow_clone

  node = start
  branch = path.map do |key|
    node = node[key]
    node.__cow_clone
  end
  nodes = [start] + branch

  # [node1, node2, node3] with
  # [node2, node3, node4] and
  # ['key1', 'key2, 'key3']
  nodes[0...-1].zip(nodes[1..-1], path) do |parent, child, key|
    # These are all cow_clones, so won't trigger a further cow
    # modification.
    parent[key] = child
  end

  @store = nodes.first
  nodes.last
end

#lock!(&blk) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/configatron/root_store.rb', line 96

def lock!(&blk)
  if blk
    orig = @locked
    begin
      @locked = true
      blk.call
    ensure
      @locked = orig
    end
  else
    @locked = true
  end
end

#locked?Boolean

Returns:

  • (Boolean)

92
93
94
# File 'lib/configatron/root_store.rb', line 92

def locked?
  @locked
end

#reset!Object


61
62
63
# File 'lib/configatron/root_store.rb', line 61

def reset!
  @store = ::Configatron::Store.new(self)
end

#temp(&block) ⇒ Object


65
66
67
68
69
70
71
72
73
# File 'lib/configatron/root_store.rb', line 65

def temp(&block)
  temp_start

  begin
    yield
  ensure
    temp_end
  end
end

#temp_endObject


85
86
87
88
89
90
# File 'lib/configatron/root_store.rb', line 85

def temp_end
  @cow = @temp_cow

  @locked = @temporary_locks.pop
  @store = @temporary_states.pop
end

#temp_startObject


75
76
77
78
79
80
81
82
83
# File 'lib/configatron/root_store.rb', line 75

def temp_start
  @temp_cow = @cow

  # Just need to have a unique Copy-on-Write generation ID
  @cow = @@cow += 1

  @temporary_locks.push(@locked)
  @temporary_states.push(@store)
end

#unlock!(&blk) ⇒ Object


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/configatron/root_store.rb', line 110

def unlock!(&blk)
  if blk
    orig = @locked
    begin
      @locked = false
      blk.call
    ensure
      @locked = orig
    end
  else
    @locked = false
  end
end