Class: DepsGrapher::DSL

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

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ DSL

Returns a new instance of DSL.



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

def initialize(obj)
  @obj = obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object (private)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/deps_grapher/dsl.rb', line 19

def method_missing(symbol, *args, &block)
  ["#{symbol}=", symbol].each do |method_name|
    next unless @obj.respond_to?(method_name)

    parameters = @obj.method(method_name).parameters

    return @obj.send(method_name) if parameters.empty?

    if parameters.size == 1
      parameter = parameters.first
      case parameter[0]
      when :req, :opt
        return @obj.send(method_name, args.first.freeze)
      when :block
        return @obj.send(method_name, &block)
      else
        raise ArgumentError, "unsupported parameter type: #{parameter[0]}"
      end
    end

    next unless parameters.size == 2

    arg_type_1st, arg_type_2nd = parameters.flat_map(&:first)
    return @obj.send(method_name, args.first.freeze, &block) if %i[req opt].include?(arg_type_1st) && arg_type_2nd == :block

    raise ArgumentError, "unsupported parameter types: #{arg_type_1st}, #{arg_type_2nd}"
  end

  super
end

Instance Method Details

#respond_to_missing?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/deps_grapher/dsl.rb', line 9

def respond_to_missing?(symbol, include_private = false)
  ["#{symbol}=", symbol].each do |method_name|
    return true if @obj.respond_to?(method_name)
  end

  super
end