Class: MetaCommit::Extension::RubySupport::Models::Ast

Inherits:
Contracts::Ast
  • Object
show all
Defined in:
lib/meta_commit_ruby_support/models/ast.rb

Overview

Adapter which implements MetaCommit::Contracts::Ast contract and wraps Parser::AST::Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ Ast

Returns a new instance of Ast.

Parameters:

  • ast (Parser::AST::Node)


8
9
10
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 8

def initialize(ast)
  @ast = ast
end

Instance Attribute Details

#astArray<Parser::AST::Node>

Returns the current value of ast.

Returns:

  • (Array<Parser::AST::Node>)

    the current value of ast



4
5
6
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 4

def ast
  @ast
end

Instance Method Details

#childrenArray<MetaCommit::Contracts::Ast>

Returns:

  • (Array<MetaCommit::Contracts::Ast>)


13
14
15
16
17
18
19
20
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 13

def children
  begin
    @ast.children
        .map {|child| Ast.new(child)}
  rescue NoMethodError
    return []
  end
end

#class_nameString

Returns:

  • (String)


74
75
76
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 74

def class_name
  @ast.children.first.children.last.to_s
end

#classesArray<Ast>

Returns:



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 84

def classes
  accumulator=[]
  begin
    return if empty_ast?
    return self if is_class?
    accumulator.concat(children.map {|child| child.classes})
  rescue NoMethodError
    return nil
  end
  accumulator.flatten.compact
end

#empty_ast?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 43

def empty_ast?
  @ast.nil?
end

#first_lineInteger

Returns:

  • (Integer)


23
24
25
26
27
28
29
30
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 23

def first_line
  return unless @ast.respond_to?(:location)
  begin
    return @ast.location.first_line
  rescue NoMethodError
    return nil
  end
end

#is_class?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 53

def is_class?
  type == :class
end

#is_method?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 58

def is_method?
  type == :def
end

#is_module?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 48

def is_module?
  type == :module
end

#last_lineInteger

Returns:

  • (Integer)


33
34
35
36
37
38
39
40
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 33

def last_line
  return unless @ast.respond_to?(:location)
  begin
    return @ast.location.last_line
  rescue NoMethodError
    return nil
  end
end

#method_nameString

Returns:

  • (String)


79
80
81
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 79

def method_name
  @ast.children.first.to_s
end

#module_nameString

Returns:

  • (String)


69
70
71
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 69

def module_name
  @ast.children.first.children.last.to_s
end

#to_sObject



96
97
98
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 96

def to_s
  @ast.to_s
end

#typeSymbol

Returns:

  • (Symbol)


63
64
65
66
# File 'lib/meta_commit_ruby_support/models/ast.rb', line 63

def type
  return @ast.class.to_s.to_sym unless @ast.respond_to?(:type)
  @ast.type
end