Class: DSL

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ DSL

Returns a new instance of DSL.



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

def initialize(parent)
  @parent = parent
end

Class Method Details

.call(parent, &blk) ⇒ Object



5
6
7
8
# File 'lib/dsl.rb', line 5

def call(parent, &blk)
  # `instance_eval` returns the last thing in the block whereas `tap` return the Object that was tapped
  new(parent).tap { |instance| instance.instance_eval(&blk) }
end

.def_dsl_delegator(*method_names) ⇒ Object

Defines a method that accepts a variable number of arguments that will delegate to the parent. This will attempt to call the setter/getter method before attempting to access the instance variable. This way, any modifications you do to the instance variables in those methods will be used.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dsl.rb', line 15

def def_dsl_delegator(*method_names)
  method_names.each do |method_name|
    raise TypeError unless method_name.respond_to?(:to_sym)
    method_name = method_name.to_sym
    
    define_method(method_name) do |*args|
      unless args.empty?
        args = args.first if args.length == 1
        @parent.respond_to?("#{method_name}=") ? @parent.send("#{method_name}=", args) : @parent.instance_variable_set("@#{method_name}", args)
      end
      
      @parent.respond_to?(method_name) ? @parent.send(method_name) : @parent.instance_variable_get("@#{method_name}")
    end
    
  end
end