Class: MODL::Parser::MethodExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/modl_method.rb

Overview

Extracts a method definition from a ParsedPair

Class Method Summary collapse

Class Method Details

.extract(pair, global) ⇒ Object

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/modl/parser/modl_method.rb', line 82

def self.extract(pair, global)
  return unless pair.type == 'method'

  mthd = MODLMethod.new
  map = pair.map if pair.map
  map = pair.valueItem&.value&.map if pair.valueItem&.value&.map

  map.mapItems.each do |item|
    next unless item&.pair&.type

    case item&.pair&.type
    when 'id'
      mthd.id = item.pair.valueItem.value.primitive.string.string
    when 'transform'
      mthd.transform = item.pair.valueItem.value.primitive.string.string
    when 'name'
      mthd.name = item.pair.valueItem.value.primitive.string.string
    else
      raise InterpreterError, 'Invalid *method - only *id, *name, and *transform fields expected'
    end
  end

  raise InterpreterError, 'Missing id for method' if mthd.id.nil?
  raise InterpreterError, 'Missing name for method' if mthd.name.nil?
  raise InterpreterError, 'Duplicate method name or id: ' + mthd.name if global.has_user_method?(mthd.name)
  raise InterpreterError, 'Duplicate method name or id: ' + mthd.id if global.has_user_method?(mthd.id)

  # store the methods by id and name to make them easier to find later
  global.user_method_id(mthd.id, mthd)
  global.user_method(mthd.name, mthd)
end