Class: RubyConf::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-conf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, parent = nil, &block) ⇒ Config

Returns a new instance of Config.



91
92
93
94
95
96
# File 'lib/ruby-conf.rb', line 91

def initialize(name = nil, parent = nil, &block)
  @__rc_locked, @__rc_attributes, @__rc_chains, @__rc_parent, @__rc_static_values = false, {}, [], parent, {}
  @__rc_name = name.to_sym if name
  instance_eval(&block) if block_given?
  __rc_lock
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ruby-conf.rb', line 164

def method_missing(method_name, *args, &block)
  name, modifier = method_name.to_s.match(/^(?<name>.*?)(?<modifier>[?!])?$/).captures
  name = name.to_sym

  options = args.last.is_a?(Hash) ? args.last : {}

  if block_given?
    if options.key?(:inherits)
      self[name] = [*options[:inherits]].inject(Config.new(name, self, &block)) do |conf, inherited|
        inherited = self[inherited.to_sym] unless inherited.is_a?(Config)
        __rc_copy(inherited.__rc_attributes).each do |key, value|
          __rc_set_defaults(conf, key, value)
        end
        conf
      end
    elsif self[name].is_a?(Config)
      self[name].instance_eval(&block)
    else
      self[name] = Config.new(name, self, &block)
    end

    while (chain = self[name].__rc_chains.pop)
      self[name][chain] = chain.__rc_chain.nil? ?  "" : chain.__rc_chain.__rc_gather
    end

  else

    if @__rc_locked && args.size == 0 && !@__rc_attributes.key?(name)
      return case modifier
        when '!'
          super
        when '?'
          false
        else
          nil
      end
    end

    if !@__rc_attributes.key?(name) && (args.size == 0 || args.first.is_a?(Magic))
      str = name.to_s
      str.extend(Magic)
      if args.first.is_a?(Magic)
        str.__rc_chain = args.first
        @__rc_chains.delete_if { |a| a.equal? args.first }
      end
      @__rc_chains << str
      return str
    end

    if args.empty?
      if modifier == '?'
        !!self[name]
      elsif modifier == '!' && __rc_attributes[name.to_sym].is_a?(Proc)
        __rc_static_proc(name)
      else
        self[name]
      end
    else
      arg = args.size == 1 ? args.first : args
      if @__rc_locked && __rc_attributes[name.to_sym].is_a?(Proc)
        if modifier == '!'
          __rc_static_proc(name, *args)
        else
          self[name, *args]
        end
      else
        self[name] = arg
      end
    end
  end
end

Instance Attribute Details

#__rc_attributesObject (readonly)

Returns the value of attribute __rc_attributes.



76
77
78
# File 'lib/ruby-conf.rb', line 76

def __rc_attributes
  @__rc_attributes
end

#__rc_chainsObject (readonly)

Returns the value of attribute __rc_chains.



76
77
78
# File 'lib/ruby-conf.rb', line 76

def __rc_chains
  @__rc_chains
end

#__rc_lockedObject (readonly)

Returns the value of attribute __rc_locked.



76
77
78
# File 'lib/ruby-conf.rb', line 76

def __rc_locked
  @__rc_locked
end

#__rc_nameObject (readonly)

Returns the value of attribute __rc_name.



76
77
78
# File 'lib/ruby-conf.rb', line 76

def __rc_name
  @__rc_name
end

#__rc_parentObject (readonly)

Returns the value of attribute __rc_parent.



76
77
78
# File 'lib/ruby-conf.rb', line 76

def __rc_parent
  @__rc_parent
end

#__rc_static_valuesObject (readonly)

Returns the value of attribute __rc_static_values.



76
77
78
# File 'lib/ruby-conf.rb', line 76

def __rc_static_values
  @__rc_static_values
end

Instance Method Details

#[](name, *args) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ruby-conf.rb', line 110

def [](name, *args)
  name = name.to_sym
  value = @__rc_attributes[name]
  if value.is_a?(Proc)
    args += [nil] * (value.arity.abs - args.size) if value.arity.abs > args.size
    @@stack ||= []
    if @@stack.include?(name)
      RubyConf.err("[ruby-conf] Detected recursive proc: #{name}")
      "[RECURSIVE]"
    else
      @@stack << name
      begin
        value = __rc_root.instance_exec(*args, &value)
        static_key = "#{name}:#{args}"
        @__rc_static_values[static_key] = value
        value
      ensure
        @@stack.delete(name)
      end
    end
  else
    value
  end
end

#[]=(name, value) ⇒ Object



147
148
149
# File 'lib/ruby-conf.rb', line 147

def []=(name, value)
  @__rc_attributes[name.to_sym] = value
end

#__rc_build_inspectObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/ruby-conf.rb', line 257

def __rc_build_inspect()
  istr = ""
  istr += "[#{@__rc_name || "CONFIG"}] " unless @__rc_parent
  istr += @__rc_attributes.keys.map{|k| k.to_s }.sort.map{ |key|
    str = ""
    value = begin
      self[key]
    rescue => e
      "[UNRESOLVED:#{e}]"
    end
    str += "#{key}: "
    str += value.is_a?(RubyConf::Config) ? (value == self ? "[SELF]" : "{ #{value.__rc_build_inspect} }") : value.inspect
    str
  }.join(", ")
  istr
end

#__rc_build_string(depth = 0) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ruby-conf.rb', line 240

def __rc_build_string(depth = 0)
  str = ""
  str += "[#{@__rc_name || "CONFIG"}]\n" unless @__rc_parent
  str += "\n"
  @__rc_attributes.keys.map{|k| k.to_s }.sort.each do |key|
    value = begin
      self[key]
    rescue => e
      "[UNRESOLVED]"
    end

    str += "  " * depth
    str += "#{key}:"
    str += value.is_a?(Config) ? (value == self ? " [SELF]\n" : value.__rc_build_string(depth+1)) : " #{value}\n"
  end
  str
end

#__rc_copy(o) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/ruby-conf.rb', line 135

def __rc_copy(o)
  if o.is_a?(Hash)
    o.inject({}) { |copy, (key, val)| copy[key] = __rc_copy(val); copy }
  elsif o.is_a?(Array)
    o.inject([]) { |copy, i| copy << __rc_copy(i); copy }
  else
    o
  end
end

#__rc_lockObject



98
99
100
101
102
# File 'lib/ruby-conf.rb', line 98

def __rc_lock
  @__rc_locked = true
  @__rc_attributes.each_value { |value| value.__rc_lock if value.is_a?(Config) }
  self
end

#__rc_rootObject



78
# File 'lib/ruby-conf.rb', line 78

def __rc_root() __rc_parent ? __rc_parent.__rc_root : self end

#__rc_set_defaults(conf, key, value) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ruby-conf.rb', line 151

def __rc_set_defaults(conf, key, value)
  if conf.__rc_attributes.key?(key)
    if value.is_a?(Config)
      sub = conf.__rc_attributes[key]
      value.__rc_attributes.each do |k, v|
        __rc_set_defaults(sub, k, v)
      end
    end
  else
    conf.__rc_attributes[key] = value
  end
end

#__rc_static_proc(name, *args) ⇒ Object



104
105
106
107
108
# File 'lib/ruby-conf.rb', line 104

def __rc_static_proc(name, *args)
  name = name.to_sym
  static_key = "#{name}:#{args}"
  @__rc_static_values[static_key] || self[name, *args]
end

#detach(parent = nil) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/ruby-conf.rb', line 79

def detach(parent = nil)
  @__rc_parent = parent
  @__rc_attributes.values.each do |child|
    child.detach(self) if child.is_a?(Config)
  end
  self
end

#inspectObject



276
# File 'lib/ruby-conf.rb', line 276

def inspect() __rc_build_inspect end

#rckeysObject



87
88
89
# File 'lib/ruby-conf.rb', line 87

def rckeys
  @__rc_attributes.keys
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/ruby-conf.rb', line 236

def respond_to?(name)
  super || @__rc_attributes.key?(name) || @__rc_parent.respond_to?(name)
end

#to_aObject



145
# File 'lib/ruby-conf.rb', line 145

def to_a() [self] end

#to_sObject



274
# File 'lib/ruby-conf.rb', line 274

def to_s() __rc_build_string end

#to_strObject



275
# File 'lib/ruby-conf.rb', line 275

def to_str() to_s end