class MODL::Parser::MethodExtractor

Extracts a method definition from a ParsedPair

Public Class Methods

extract(pair, global) click to toggle source
# 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