Class: MODL::Parser::ClassProcessor

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

Overview

This class handles the conversion of objects that refer to classes into instances of those classes. It works recursively since class usage can be nested.

Constant Summary collapse

MAX_RECURSION_DEPTH =

How deep can the class structure be?

50

Class Method Summary collapse

Class Method Details

.process(global, obj) ⇒ Object

global is a GlobalParseContext and obj is the extracted Array or Hash from MODL::Parser::Parsed.extract_json

Raises:

  • (StandardError)


33
34
35
36
37
38
39
40
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
# File 'lib/modl/parser/class_processor.rb', line 33

def self.process(global, obj)
  # Process each object in the array or just process the object if its a hash.
  # Any other object is ignored.
  raise StandardError, 'parameter "global" should be a GlobalParseContext' unless global.is_a?(GlobalParseContext)

  if obj.is_a? Array
    root_class = global.classs 'root'
    unless root_class.nil?
      raise StandardError, 'root class has no *assign statement.' if root_class.assign.nil?
      raise StandardError, 'root class *assign statement should be of the form "*assign=[[class_name]]".' if root_class.assign.length > 1 || root_class.assign[0].length > 1
      root_class_assign = root_class.assign[0][0]

      array_class = global.classs root_class_assign
      classes = array_class.keylist_of_length obj.length
      new_obj = []

      # The top level array can be an array of arrays or an array of hashes, so we need to handle both.
      obj.each_index do |i|
        item = obj[i]
        if item.is_a? Array
          new_obj << {classes[i] => item}
        elsif item.is_a? Hash
          new_obj << item
        end
      end

      obj = new_obj
      process_recursive global, obj

      result = []
      obj.each_index do |i|
        result << obj[i][global.classs(classes[i]).name_or_id]
      end

      return result
    end
  end
  process_recursive global, obj
  return obj
end