Module: DomainSwitcher::ConfigBuilder

Included in:
Domain, Website
Defined in:
lib/domain_switcher/config_builder.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m) ⇒ Object

Do nothing when a method is missing



35
36
37
# File 'lib/domain_switcher/config_builder.rb', line 35

def method_missing(m)
  nil
end

Instance Method Details

#build_config!(hash) ⇒ Object

Create a bunch of method for the configuration hash



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/domain_switcher/config_builder.rb', line 5

def build_config!(hash)
  raise "Build config only accept hashes and not #{conf.class}" unless Hash === hash
  
  # From: https://github.com/kwi/awesome_conf
  m = Module.new do
    instance_methods.each { |selector| remove_method(selector) }

    hash.each do |k, v|
      const_set('AC_' + k.to_s, v)
      module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
        def #{k}
          #{'AC_'+ k.to_s}
        end
      END_EVAL
      
      if TrueClass === v or FalseClass === v
        module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
          def #{k}?
            #{v ? true : false}
          end
        END_EVAL
      end
    end

  end

  extend m
end