Class: BELParser::Script::StateAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/bel_parser/script/state_aggregator.rb

Overview

StateAggregator aggregates BEL Script state for each AST node it processes.

Constant Summary collapse

STATE_PATH =
'state'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast_enum, options = {}) ⇒ StateAggregator

Returns a new instance of StateAggregator.



9
10
11
12
13
14
15
# File 'lib/bel_parser/script/state_aggregator.rb', line 9

def initialize(ast_enum, options = {})
  @ast_enum        = ast_enum
  @script_context  = {}.merge(options)

  StateAggregator.require_script_path
  @state_functions = StateAggregator.state_constants(State)
end

Class Method Details

.require_script_pathObject



32
33
34
35
36
37
38
39
# File 'lib/bel_parser/script/state_aggregator.rb', line 32

def self.require_script_path
  base_path = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
  Dir[File.join(base_path, STATE_PATH, '*.rb')]
    .each do |ruby_file|
      ruby_file.sub!(/^#{Regexp.escape(base_path)}/, '')
      require_relative ruby_file
    end
end

.state_constants(mod) ⇒ Object



41
42
43
44
45
46
# File 'lib/bel_parser/script/state_aggregator.rb', line 41

def self.state_constants(mod)
  mod.constants.collect do |symbol|
    const = mod.const_get(symbol)
    const if const.respond_to?(:consume)
  end.compact
end

Instance Method Details

#eachObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bel_parser/script/state_aggregator.rb', line 17

def each
  if block_given?
    @ast_enum.each do |(line_number, line, ast_node)|
      ast_node.traverse.each do |node|
        @state_functions.each do |func|
          func.consume(node, @script_context)
        end
      end
      yield [line_number, line, ast_node, @script_context]
    end
  else
    enum_for(:each)
  end
end