Class: FunWith::Gems::ConstantBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/gems/constant_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_constant(*args, &block) ⇒ Object



4
5
6
# File 'lib/fun_with/gems/constant_builder.rb', line 4

def self.build_constant( *args, &block )
  self.new.build_constant( *args, &block )
end

Instance Method Details

#build_constant(c) ⇒ Object

Expecting that constants will only be passed as strings when the constants hasn't been previously defined.

returns the constant



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fun_with/gems/constant_builder.rb', line 11

def build_constant( c )
  if c.is_a?( String )
    #create the module
    const = Object
    
    # go down the ::Module stack, creating modules along the way
    full_const_name = ""
    c.split("::").each do |module_name|
      full_const_name << "::" unless full_const_name.fwf_blank?
      full_const_name << module_name
      
      if eval( "defined?(#{full_const_name})")
        const = eval( full_const_name )
      else
        const = const.const_set( module_name, Module.new )
      end
    end
  
    eval( c )
  else
    c
  end
end