Class: KungFigure::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kung_figure.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kung_figure.rb', line 60

def method_missing(key,&block)
  @props ||= {}
  klazz_name = camelize(key.to_s).to_sym
  child_cfg_clazz = self.class.const_get(klazz_name) if self.class.const_defined?(klazz_name)
  child_cfg_clazz ||= get_from_enclosing_module(klazz_name)

  raise "No such configuration #{key}" unless child_cfg_clazz

  unless @props.key?(key)
    @props[key] = if child_cfg_clazz.ancestors.include?(KungFigure::Base)
                    child_cfg_clazz.new
                  elsif child_cfg_clazz.respond_to?(:config)
                    child_cfg_clazz.config
                  end
  end
  @props[key].instance_eval(&block) if block_given?
  return @props[key]
end

Class Method Details

.define_prop(name, default) ⇒ Object



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

def define_prop(name,default)
  define_method(name) do |*args|
    @props ||= {}
    if args.length > 0
      @props[name] = args[0]
    else
      @props[name] || default
    end
  end
end

Instance Method Details

#camelize(str) ⇒ Object



47
48
49
# File 'lib/kung_figure.rb', line 47

def camelize(str)
  str.split('_').map{|l| l.capitalize}.join('')
end

#get_from_enclosing_module(klazz_name) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/kung_figure.rb', line 51

def get_from_enclosing_module(klazz_name)
  config_klazz_path=self.class.name.to_s.split('::')[0..-2]
  config_klazz_path<< klazz_name
  config_klazz_path.inject(Object){|parent,nxt|
     break unless parent.const_defined?(nxt.to_sym) 
     parent.const_get(nxt.to_sym)
  }
end