class MotionBlender::Source
Attributes
ast[R]
code[R]
file[R]
line[R]
method[R]
parent[R]
type[R]
Public Class Methods
new(attrs = {})
click to toggle source
# 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
parse(code, attrs = {})
click to toggle source
# 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)
click to toggle source
# File lib/motion_blender/source.rb, line 19 def self.parse_file file ast = ::Parser::CurrentRuby.parse_file(file) new(ast: ast) end
Public Instance Methods
ancestors()
click to toggle source
# File lib/motion_blender/source.rb, line 70 def ancestors root? ? [self] : [self, *parent.ancestors] end
attributes()
click to toggle source
# 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)
click to toggle source
# 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
children()
click to toggle source
# 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?()
click to toggle source
# File lib/motion_blender/source.rb, line 74 def like_module? type.module? || type.class? end
root()
click to toggle source
# File lib/motion_blender/source.rb, line 66 def root root? ? self : parent.root end
root?()
click to toggle source
# File lib/motion_blender/source.rb, line 62 def root? parent.nil? end
to_s()
click to toggle source
# File lib/motion_blender/source.rb, line 45 def to_s "#{file}:#{line}:in `#{method || type}'" end