Class: ActionView::RenderParser::RipperASTParser::Node

Inherits:
Array
  • Object
show all
Defined in:
lib/action_view/ripper_ast_parser.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, arr, opts = {}) ⇒ Node

Returns a new instance of Node.



11
12
13
14
# File 'lib/action_view/ripper_ast_parser.rb', line 11

def initialize(type, arr, opts = {})
  @type = type
  super(arr)
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/action_view/ripper_ast_parser.rb', line 9

def type
  @type
end

Instance Method Details

#argument_nodesObject



35
36
37
38
39
40
41
42
43
# File 'lib/action_view/ripper_ast_parser.rb', line 35

def argument_nodes
  raise unless fcall?
  return [] if self[1].nil?
  if self[1].last == false || self[1].last.type == :vcall
    self[1][0...-1]
  else
    self[1][0..-1]
  end
end

#call?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/action_view/ripper_ast_parser.rb', line 57

def call?
  type == :call
end

#call_method_nameObject



65
66
67
# File 'lib/action_view/ripper_ast_parser.rb', line 65

def call_method_name
  self[2].first
end

#childrenObject



16
17
18
# File 'lib/action_view/ripper_ast_parser.rb', line 16

def children
  to_a
end

#fcall?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/action_view/ripper_ast_parser.rb', line 25

def fcall?
  type == :command || type == :fcall
end

#fcall_named?(name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/action_view/ripper_ast_parser.rb', line 29

def fcall_named?(name)
  fcall? &&
    self[0].type == :@ident &&
    self[0][0] == name
end

#hash?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/action_view/ripper_ast_parser.rb', line 74

def hash?
  type == :bare_assoc_hash || type == :hash
end

#hash_from_body(body) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/action_view/ripper_ast_parser.rb', line 88

def hash_from_body(body)
  body.to_h do |hash_node|
    return nil if hash_node.type != :assoc_new

    [hash_node[0], hash_node[1]]
  end
end

#inspectObject



20
21
22
23
# File 'lib/action_view/ripper_ast_parser.rb', line 20

def inspect
  typeinfo = type && type != :list ? ":" + type.to_s + ", " : ""
  "s(" + typeinfo + map(&:inspect).join(", ") + ")"
end

#string?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/action_view/ripper_ast_parser.rb', line 45

def string?
  type == :string_literal
end

#symbol?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/action_view/ripper_ast_parser.rb', line 96

def symbol?
  type == :@label || type == :symbol_literal
end

#to_hashObject



78
79
80
81
82
83
84
85
86
# File 'lib/action_view/ripper_ast_parser.rb', line 78

def to_hash
  if type == :bare_assoc_hash
    hash_from_body(self[0])
  elsif type == :hash && self[0] == nil
    {}
  elsif type == :hash && self[0].type == :assoclist_from_args
    hash_from_body(self[0][0])
  end
end

#to_stringObject



69
70
71
72
# File 'lib/action_view/ripper_ast_parser.rb', line 69

def to_string
  raise unless string?
  self[0][0][0]
end

#to_symbolObject



100
101
102
103
104
105
106
107
108
# File 'lib/action_view/ripper_ast_parser.rb', line 100

def to_symbol
  if type == :@label && self[0] =~ /\A(.+):\z/
    $1.to_sym
  elsif type == :symbol_literal && self[0].type == :symbol && self[0][0].type == :@ident
    self[0][0][0].to_sym
  else
    raise "not a symbol?: #{self.inspect}"
  end
end

#variable_nameObject



61
62
63
# File 'lib/action_view/ripper_ast_parser.rb', line 61

def variable_name
  self[0][0]
end

#variable_reference?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/action_view/ripper_ast_parser.rb', line 49

def variable_reference?
  type == :var_ref
end

#vcall?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/action_view/ripper_ast_parser.rb', line 53

def vcall?
  type == :vcall
end