Class: MotionBlender::Analyzer::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_blender/analyzer/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Source

Returns a new instance of Source.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/motion_blender/analyzer/source.rb', line 8

def initialize attrs = {}
  @evaluated = false
  ast = attrs.delete :ast
  if ast
    @code = ast.loc.expression.try(:source)
    @file = ast.loc.expression.try(:source_buffer).try(:name)
    @line = ast.loc.expression.try(:line)
    @type = ast.type.to_s.inquiry
    @method = @type.send? ? ast.children[1] : nil
    @ast = ast
  end
  attrs.each do |k, v|
    instance_variable_set "@#{k}", v
  end
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def ast
  @ast
end

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def code
  @code
end

#evaluatedObject (readonly) Also known as: evaluated?

Returns the value of attribute evaluated.



5
6
7
# File 'lib/motion_blender/analyzer/source.rb', line 5

def evaluated
  @evaluated
end

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def line
  @line
end

#methodObject (readonly)

Returns the value of attribute method.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def method
  @method
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/motion_blender/analyzer/source.rb', line 4

def type
  @type
end

Instance Method Details

#attributesObject



39
40
41
42
43
# File 'lib/motion_blender/analyzer/source.rb', line 39

def attributes
  %w(code file line type method).map do |k|
    [k, instance_variable_get("@#{k}")]
  end.to_h.as_json
end

#childrenObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/motion_blender/analyzer/source.rb', line 28

def children
  @children ||=
    if @ast
      @ast.children.grep(::Parser::AST::Node).map do |ast|
        Source.new(ast: ast, parent: self)
      end
    else
      []
    end
end

#evaluated!Object



24
25
26
# File 'lib/motion_blender/analyzer/source.rb', line 24

def evaluated!
  @evaluated = true
end