Class: Thwart::Dsl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map = {}) ⇒ Dsl

Returns a new instance of Dsl.



9
10
11
# File 'lib/thwart/dsl.rb', line 9

def initialize(map = {})
  self.extra_methods = map
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/thwart/dsl.rb', line 33

def method_missing(name, *args, &block)
  if self.respond_to?(name)
    return self.target.send(self.method_map[name], *args, &block) if self.method_map.has_key?(name)
    return self.target.send(name, *args, &block) if @all
  end
  super
end

Instance Attribute Details

#allObject

Hash of the extra method mappings of this DSL => target



4
5
6
# File 'lib/thwart/dsl.rb', line 4

def all
  @all
end

#extra_methodsObject

Hash of the extra method mappings of this DSL => target



4
5
6
# File 'lib/thwart/dsl.rb', line 4

def extra_methods
  @extra_methods
end

#method_mapObject

Hash of the extra method mappings of this DSL => target



4
5
6
# File 'lib/thwart/dsl.rb', line 4

def method_map
  @method_map
end

#targetObject

Hash of the extra method mappings of this DSL => target



4
5
6
# File 'lib/thwart/dsl.rb', line 4

def target
  @target
end

Instance Method Details

#evaluate(a_target, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/thwart/dsl.rb', line 13

def evaluate(a_target, &block)
  self.target = a_target
  self.method_map = target.public_methods.inject({}) do |acc, m| 
    key = m.to_s.gsub(/=$/, "").to_sym
    acc[key] = m if acc[key].nil? || m != key
    acc 
  end.merge(self.extra_methods)
  self.instance_eval(&block)
  self.target
end

#respond_to?(name, other = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/thwart/dsl.rb', line 24

def respond_to?(name, other = false)
  if @all
    return target.respond_to?(name)
  else
    return true if self.method_map.has_key?(name) && !!self.method_map[name]
    super
  end
end