Module: Metafun::Switchmaker

Included in:
Class
Defined in:
lib/metafun/switchmaker.rb

Instance Method Summary collapse

Instance Method Details

#switch(argument, default = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/metafun/switchmaker.rb', line 3

def switch argument, default = false
  class_variable_set :"@@#{argument}", default
  class_eval <<-STRING
    def self.#{argument}?
      return @@#{argument}.call if @@#{argument}.kind_of? Proc
      return @@#{argument}
    end
    
    def self.#{argument} the_arg
      if the_arg.kind_of? Proc
        @@#{argument} = the_arg
      else
        case the_arg
          when :on;     @@#{argument} = true
          when :yes;    @@#{argument} = true
          when 1;       @@#{argument} = true
          when true;    @@#{argument} = true
          when :off;    @@#{argument} = false
          when :no;     @@#{argument} = false
          when 0;       @@#{argument} = false
          when false;   @@#{argument} = false
        end
      end
    end
  STRING
end