Class: MotionBlender::Source

Inherits:
Object
  • Object
show all
Includes:
FlagAttribute, GlobalConstants, ReferringConstants, WrappingModules
Defined in:
lib/motion_blender/source.rb,
lib/motion_blender/source/global_constants.rb,
lib/motion_blender/source/wrapping_modules.rb,
lib/motion_blender/source/referring_constants.rb

Defined Under Namespace

Modules: GlobalConstants, ReferringConstants, WrappingModules

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReferringConstants

#referring_constant?, #referring_constants

Methods included from GlobalConstants

#find_global_constants, #global_constants

Methods included from WrappingModules

#class_content?, #module_content?, #parent_module, #wrapping_modules

Constructor Details

#initialize(attrs = {}) ⇒ Source

Returns a new instance of Source.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/motion_blender/source.rb', line 27

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
  @type = @type.to_s.inquiry
  @method = @method.try(:to_sym)
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



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

def ast
  @ast
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.parse(code, attrs = {}) ⇒ Object



14
15
16
17
# File 'lib/motion_blender/source.rb', line 14

def self.parse code, attrs = {}
  attrs[:ast] = ::Parser::CurrentRuby.parse(code)
  new(attrs)
end

.parse_file(file) ⇒ Object



19
20
21
22
# File 'lib/motion_blender/source.rb', line 19

def self.parse_file file
  ast = ::Parser::CurrentRuby.parse_file(file)
  new(ast: ast)
end

Instance Method Details

#ancestorsObject



70
71
72
# File 'lib/motion_blender/source.rb', line 70

def ancestors
  root? ? [self] : [self, *parent.ancestors]
end

#attributesObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/motion_blender/source.rb', line 78

def attributes
  {
    'code' => @code,
    'file' => @file,
    'line' => @line,
    'type' => @type.to_s,
    'method' => @method.try(:to_s),
    'global_constants' => global_constants,
    'wrapping_modules' => wrapping_modules
  }
end

#child_at(*args) ⇒ Object



57
58
59
60
# File 'lib/motion_blender/source.rb', line 57

def child_at *args
  i = args.shift
  args.present? ? children[i].child_at(*args) : children[i]
end

#childrenObject



49
50
51
52
53
54
55
# File 'lib/motion_blender/source.rb', line 49

def children
  @children ||=
    @ast
    .try(:children).to_a
    .select { |ast| ast.nil? || ast.is_a?(::Parser::AST::Node) }
    .map { |ast| Source.new(ast: ast, parent: self) }
end

#like_module?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/motion_blender/source.rb', line 74

def like_module?
  type.module? || type.class?
end

#rootObject



66
67
68
# File 'lib/motion_blender/source.rb', line 66

def root
  root? ? self : parent.root
end

#root?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/motion_blender/source.rb', line 62

def root?
  parent.nil?
end

#to_sObject



45
46
47
# File 'lib/motion_blender/source.rb', line 45

def to_s
  "#{file}:#{line}:in `#{method || type}'"
end