Class: ForwardingDsl::Dsl

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(this, that) ⇒ Dsl

Returns a new instance of Dsl.



20
21
22
23
# File 'lib/forwarding_dsl/dsl.rb', line 20

def initialize this, that
  @this = this
  @that = that
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing name, *args, &block
  if this.respond_to? name
    this.public_send name, *args, &block
  else
    that.public_send name, *args, &block
  end
end

Instance Attribute Details

#thatObject

Returns the value of attribute that.



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

def that
  @that
end

#thisObject

Returns the value of attribute this.



3
4
5
# File 'lib/forwarding_dsl/dsl.rb', line 3

def this
  @this
end

Class Method Details

.run(target, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/forwarding_dsl/dsl.rb', line 6

def self.run target, &block
  return target unless block_given?

  case block.arity
  when 0 then
    new(target, block.binding.eval('self')).
      send(:instance_exec, &block)
  when 1 then
    block.call target
  else
    raise ArgumentError.new "Wrong number of arguments. Pass 1 or none."
  end
end

Instance Method Details

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/forwarding_dsl/dsl.rb', line 33

def respond_to_missing? name, *args
  this.respond_to?(name, *args) ||
    that.respond_to?(name, *args) ||
    super
end