Class: MODL::Parser::ClassExtractor

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

Overview

Extract a class from a ParsedPair object

Class Method Summary collapse

Class Method Details

.extract(pair, global) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
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
85
86
87
88
89
90
91
92
93
94
# File 'lib/modl/parser/modl_class.rb', line 41

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

  clazz = MODLClass.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'
      str_value = item.pair.valueItem.value.primitive.string.string
      raise InterpreterError, 'Reserved class id - cannot redefine: ' + str_value if reserved?(str_value)

      clazz.id = str_value
    when 'name'
      str_value = item.pair.valueItem.value.primitive.string.string
      raise InterpreterError, 'Reserved class name - cannot redefine: ' + str_value if reserved?(str_value)

      clazz.name = str_value
    when 'superclass'
      str_value = item.pair.valueItem.value.primitive.string.string
      clazz.superclass = str_value
    when 'keylist'
      clazz.assign = item.pair.key_lists
    when 'allow'
      clazz.allow = item.pair.array if item.pair.array
      clazz.allow = item.pair.valueItem.value.array if item.pair.valueItem.value.array
    else
      clazz.content[item.pair.key] = item.pair.array if item.pair.array
      clazz.content[item.pair.key] = item.pair.map if item.pair.map
      clazz.content[item.pair.key] = item.pair.valueItem.value if item.pair.valueItem.value
    end
  end

  superclass = clazz.superclass

  if superclass && !reserved?(superclass) && !global.has_class?(superclass)
    raise InterpreterError, 'Invalid superclass: ' + superclass.to_s
  end
  raise InterpreterError, 'Missing id for class' if clazz.id.nil?

  # Make sure the class name isn't redefining an existing class
  if !global.has_class?(clazz.id) && !global.has_class?(clazz.name)

    # store the classes by id and name to make them easier to find later
    global.classs(clazz)
  else
    id = clazz.id.nil? ? 'undefined' : clazz.id
    name = clazz.name.nil? ? 'undefined' : clazz.name
    raise InterpreterError, 'Class name or id already defined - cannot redefine: ' + id + ', ' + name
  end
end

.reserved?(str) ⇒ Boolean

Check for a reserved class name

Returns:

  • (Boolean)


97
98
99
# File 'lib/modl/parser/modl_class.rb', line 97

def self.reserved?(str)
  %w[map str arr num].include?(str)
end