Class: MotionBlender::Analyzer::Parser

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/motion_blender/analyzer/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Parser

Returns a new instance of Parser.



15
16
17
18
# File 'lib/motion_blender/analyzer/parser.rb', line 15

def initialize file
  @file = file.to_s
  @evaluators = []
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



13
14
15
# File 'lib/motion_blender/analyzer/parser.rb', line 13

def cache
  @cache
end

#evaluatorsObject (readonly)

Returns the value of attribute evaluators.



13
14
15
# File 'lib/motion_blender/analyzer/parser.rb', line 13

def evaluators
  @evaluators
end

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/motion_blender/analyzer/parser.rb', line 13

def file
  @file
end

#referring_constantsObject (readonly)

Returns the value of attribute referring_constants.



13
14
15
# File 'lib/motion_blender/analyzer/parser.rb', line 13

def referring_constants
  @referring_constants
end

Instance Method Details

#autoloads_with(autoloads) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/motion_blender/analyzer/parser.rb', line 69

def autoloads_with autoloads
  referring_constants.map do |mods, const|
    key =
      mods
      .length.downto(0)
      .map { |i| [*mods.take(i), const].join('::') }
      .find { |k| autoloads.key?(k) }
    autoloads[key]
  end.flatten.compact
end

#dependent_requires(opts = {}) ⇒ Object



80
81
82
83
84
85
# File 'lib/motion_blender/analyzer/parser.rb', line 80

def dependent_requires opts = {}
  autoloads = opts[:autoloads]
  reqs = requires.reject(&:autoload?)
  reqs += autoloads_with(autoloads) if autoloads
  reqs.uniq(&:file)
end

#evaluate(source) ⇒ Object



52
53
54
55
# File 'lib/motion_blender/analyzer/parser.rb', line 52

def evaluate source
  @evaluators << Evaluator.new(source)
  @evaluators.last.run
end

#last_traceObject



65
66
67
# File 'lib/motion_blender/analyzer/parser.rb', line 65

def last_trace
  @evaluators.last.try :trace
end

#parseObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/motion_blender/analyzer/parser.rb', line 20

def parse
  cached = cache.fetch do
    run_callbacks :parse do
      root = Source.parse_file(@file)
      traverse(root)
      {
        sources: processed_sources.map(&:attributes),
        referring_constants: root.referring_constants
      }
    end
  end
  if cached && cache.hit?
    cached[:sources].each do |attrs|
      evaluate Source.new(attrs)
    end
  end
  @referring_constants = cached[:referring_constants]
  self
end

#processed_sourcesObject



61
62
63
# File 'lib/motion_blender/analyzer/parser.rb', line 61

def processed_sources
  @evaluators.select(&:done?).map(&:source)
end

#requiresObject



57
58
59
# File 'lib/motion_blender/analyzer/parser.rb', line 57

def requires
  @evaluators.map(&:requires).flatten
end

#traverse(source) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/motion_blender/analyzer/parser.rb', line 44

def traverse source
  if Collector.requirable?(source)
    evaluate source
  elsif Collector.acceptable?(source)
    source.children.compact.each { |src| traverse src }
  end
end