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:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/modl/parser/modl_method.rb', line 54

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: ' + mthd.name if global.has_user_method?(mthd.name)
  raise InterpreterError, 'Duplicate method 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