Module: Quiescent::CM

Defined in:
lib/quiescent.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/quiescent.rb', line 45

def method_missing(m, *args, &blk)
  if m.to_s =~ /^([A-Z]\w*?)=$/ && __qcs[$1.to_sym]
    quiesce($1.to_sym, *args)
  else
    super
  end
end

Instance Method Details

#__qcsObject



23
24
25
# File 'lib/quiescent.rb', line 23

def __qcs
  @__qcs ||= {}
end

#const_missing(name) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/quiescent.rb', line 36

def const_missing(name)
  name = name.to_sym
  if __qcs[name]
    quiesce(name, __qcs[name].call)
  else
    super
  end
end

#quiesce(name, val) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/quiescent.rb', line 27

def quiesce(name, val)
  if __qcs[name]
    const_set(name, val)
    __qcs.delete(name)
    return val
  end
  return nil
end

#quiescent(name, value = nil, &blk) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/quiescent.rb', line 15

def quiescent(name, value=nil, &blk)
  name = name.to_s
  fail NameError.new("Invalid constant name '#{name}'") unless name =~ /^[A-Z]\w*$/
  blk ||= lambda {value}
  __qcs[name.to_sym] = blk
  self
end