Class: Conf::Configuration

Inherits:
Object
  • Object
show all
Includes:
ConfigValue
Defined in:
lib/conf.rb

Instance Method Summary collapse

Methods included from ConfigValue

#__setup__, create, #method_missing

Constructor Details

#initialize(parent = nil) ⇒ Configuration

Returns a new instance of Configuration.



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

def initialize(parent = nil)
  if parent and not parent.kind_of? self.class
    raise TypeError, "expected #{self.class}, got #{parent.inspect}:#{parent.class}"
  end

  @parent   = parent
  @data     = {}
  @locked   = false
  @__root__ = self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Conf::ConfigValue

Instance Method Details

#[](key) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/conf.rb', line 156

def [](key)
  val = @data[key]

  if val.nil?
    @parent && @parent[key]
  else
    val
  end
end

#[]=(key, value) ⇒ Object



166
167
168
# File 'lib/conf.rb', line 166

def []=(key, value)
  @data[key] = value
end

#check_lockObject



117
118
119
120
121
# File 'lib/conf.rb', line 117

def check_lock
  if locked?
    raise InvalidStateError, "config is locked #{@data.keys.inspect}"
  end
end

#dataObject



144
# File 'lib/conf.rb', line 144

def data() @data end

#edit(&blk) ⇒ Object



123
124
125
126
127
# File 'lib/conf.rb', line 123

def edit(&blk)
  edit!
  instance_eval(&blk)
  done!
end

#fetch(key, &blk) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/conf.rb', line 146

def fetch(key, &blk)
  val = self[key]
  if val.nil?
    @data[key] = yield(key)
  else
    val
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def key?(key)
  @data.key?(key) || (@parent && @parent.key?(key))
end

#lock!Object



103
104
105
# File 'lib/conf.rb', line 103

def lock!
  @locked = true
end

#locked?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/conf.rb', line 129

def locked?
  @locked
end

#section(start_key) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/conf.rb', line 133

def section(start_key)
  result = @parent ? @parent.section(start_key) : {}
  rx = /^#{Regexp.escape(start_key).gsub("\\*", ".+?")}/

  @data.each do |key, value|
    result[key] = value if key =~ rx and not value.instance_of? Object
  end

  result
end

#unlock!Object



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

def unlock!
  @locked = false
end

#unlocked(&blk) ⇒ Object



111
112
113
114
115
# File 'lib/conf.rb', line 111

def unlocked(&blk)
  unlock!
  yield
  lock!
end