Module: Dslify::InstanceMethods

Defined in:
lib/dslify/dslify.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &block) ⇒ Object

instance_eval <<-EOM

def #{meth}(n=nil)
  puts "called #{meth}(\#\{n\}) from \#\{self\}"
  n ? (__h[:#{meth}] = n) : __h[:#{meth}]
end
def #{meth}=(n)
  __h[:#{meth}] = n
end

EOM



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dslify/dslify.rb', line 36

def method_missing(m,*a,&block)
  if block
    if a.empty?
      (a[0].class == self.class) ? a[0].instance_eval(&block) : super
    else
      inst = a[0]
      inst.instance_eval(&block)
      dsl_options[m] = inst
    end
  else
    if a.empty?          
      if dsl_options.has_key?(m)
        dsl_options[m]
      elsif m.to_s.index("?") == (m.to_s.length - 1)
        if options.has_key?(val = m.to_s.gsub(/\?/, '').to_sym)
          options[val] != false
        else
          false
        end
      else
        if self.class.superclass.respond_to?(:default_options) && self.class.superclass.default_options.has_key?(m)
          self.class.superclass.default_options[m]
        elsif ((respond_to? :parent) && (parent != self))
          parent.send m, *a, &block
        else
          super
        end
      end
    else
      clean_meth = m.to_s.gsub(/\=/,"").to_sym
      dsl_options[clean_meth] = (a.size > 1 ? a : a[0])
    end
  end
end

Instance Method Details

#add_method(meth) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dslify/dslify.rb', line 25

def add_method(meth)
  # instance_eval <<-EOM        
  #   def #{meth}(n=nil)
  #     puts "called #{meth}(\#\{n\}) from \#\{self\}"
  #     n ? (__h[:#{meth}] = n) : __h[:#{meth}]
  #   end
  #   def #{meth}=(n)
  #     __h[:#{meth}] = n
  #   end
  # EOM
end

#default_dsl_optionsObject



10
# File 'lib/dslify/dslify.rb', line 10

def default_dsl_options;self.class.default_options;end

#dsl_options(hsh = {}) ⇒ Object Also known as: options



11
12
13
# File 'lib/dslify/dslify.rb', line 11

def dsl_options(hsh={})
  @dsl_options ||= default_dsl_options.merge(hsh)
end

#set_vars_from_options(h, contxt = self) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/dslify/dslify.rb', line 15

def set_vars_from_options(h, contxt=self)
  h.each do |k,v| 
    if contxt.respond_to?(k.to_sym)
      contxt.send k.to_sym, v 
    else
      clean_meth = k.to_s.gsub(/\=/,"").to_sym
      dsl_options[clean_meth] = v
    end        
  end
end