Class: ParseTreeArray

Inherits:
Array show all
Defined in:
lib/airbrush/core_ext/get_args.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.translate(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/airbrush/core_ext/get_args.rb', line 5

def self.translate(*args)
  sexp = ::ParseTree.translate(*args)
  # ParseTree.translate returns [nil] if called on an inherited method, so walk
  # up the inheritance chain to find the class that the method was defined in
  unless sexp.first
    klass = args.first.ancestors.detect do |klass|
      klass.public_instance_methods(false).include?(args.last.to_s)
    end
    sexp = ::ParseTree.translate(klass, args.last) if klass
  end
  self.new(sexp)
end

Instance Method Details

#arg_nodesObject



27
28
29
30
31
32
# File 'lib/airbrush/core_ext/get_args.rb', line 27

def arg_nodes
  self[1..-1].inject([]) do |sum,item|
    sum << [item] unless item.is_a?(Array)
    sum
  end
end

#deep_array_node(type = nil) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/airbrush/core_ext/get_args.rb', line 18

def deep_array_node(type = nil)
  each do |node|
    return ParseTreeArray.new(node) if node.is_a?(Array) && (!type || node[0] == type)
    next unless node.is_a?(Array)
    return ParseTreeArray.new(node).deep_array_node(type)
  end
  nil
end

#get_argsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/airbrush/core_ext/get_args.rb', line 34

def get_args
  if arg_node = deep_array_node(:args)
    # method defined with def keyword
    args = arg_node.arg_nodes
    default_node = arg_node.deep_array_node(:block)
    return args unless default_node
  else
    # assuming method defined with Module#define_method
    return []
  end

  # if it was defined with def, and we found the default_node,
  # that should bring us back to regularly scheduled programming..

  lasgns = default_node[1..-1]
  lasgns.each do |asgn|
    args.assoc(asgn[1]) << eval(RubyToRuby.new.process(asgn[2]))
  end
  args
end