module Transform

Public Instance Methods

after_array_lambda(index: 0, decl: nil, depth: 0, before: nil) click to toggle source
# File src/transform.rb, line 61
def after_array_lambda index: 0, decl: nil, depth: 0, before: nil
  return [index: index, decl: decl, depth: depth, before: before]
end
after_group_lambda(name: nil, before: nil, depth: 0) click to toggle source
# File src/transform.rb, line 45
def after_group_lambda name: nil, before: nil, depth: 0
  return [name: name, before: before, depth: depth]
end
after_model_lambda(model: nil, before: nil) click to toggle source
# File src/transform.rb, line 29
def after_model_lambda model: nil, before: nil
  return [model: model, before: before]
end
after_type_lambda(type: nil, before: nil, depth: 0) click to toggle source
# File src/transform.rb, line 53
def after_type_lambda type: nil, before: nil, depth: 0
  return [type: type, before: before, depth: depth]
end
attribute_lambda(id:, val:, depth: 0, type: nil, index:, total: return [id: id, val: val, depth: depth, type: type, index: index, total: total]) click to toggle source
# File src/transform.rb, line 65
def attribute_lambda id:, val:, depth: 0, type: nil, index:, total:
  return [id: id, val: val, depth: depth, type: type, index: index, total: total]
end
before_array_lambda(name: nil, decl: nil, depth: 0, total: 1) click to toggle source
# File src/transform.rb, line 57
def before_array_lambda name: nil, decl: nil, depth: 0, total: 1
  return [name: name, decl: decl, depth: depth, total: total]
end
before_codes_lambda(model: nil) click to toggle source
# File src/transform.rb, line 33
def before_codes_lambda model: nil
  return [model: model]
end
before_group_lambda(name: nil) click to toggle source
# File src/transform.rb, line 41
def before_group_lambda name: nil
  return [name: name]
end
before_model_lambda(model: nil) click to toggle source
# File src/transform.rb, line 25
def before_model_lambda model: nil
  return [model: model]
end
before_type_lambda(type: nil, depth: 0, index: 0, total: 1) click to toggle source
# File src/transform.rb, line 49
def before_type_lambda type: nil, depth: 0, index: 0, total: 1
  return [type: type, depth: depth, index: index, total: total]
end
code_lambda(model: nil, code: nil) click to toggle source
# File src/transform.rb, line 37
def code_lambda model: nil, code: nil
  return [model: model, code: code]
end
cond_call(lambdas, lam, *args) click to toggle source
# File src/transform.rb, line 112
def cond_call(lambdas, lam, *args)
  if lambdas[lam]
    return lambdas[lam].(*args)
  end
  return 0
end
models_to_data(models) click to toggle source
# File src/transform.rb, line 144
def models_to_data(models)
  map = Hash.new
  stack = [map]
  transform_datamodel(
      {
          :before_group => lambda do |name:|
            # group all instances of the same type name into the same array in the top level map
            if map[name]
              stack.push(map[name])
            else
              n = Array.new
              map[name] = n
              stack.push(map[name]) # add a new container to the stack to use next
            end
          end,
          :before_type => lambda do |type:, depth:, index:, total:|
            last = stack.last
            n = Hash.new
            stack.push(n) # add a new container to the stack to use next
            if last.class <= Hash
              last[type.name] = n
            elsif last.class <= Array
              last.push(n)
            end
          end,
          :before_array => lambda do |name:, decl:, depth:, total:|
            last = stack.last
            n = Array.new
            if last.class <= Array
              last << n
            else
              last[name] = n
            end
            stack.push(n) # add a new container to the stack to use next
          end,
          :attribute => lambda do |id:, val:, depth:, type:, index:, total:|
            last = stack.last
            if last.class <= Hash
              last[id] = val
            elsif last.class <= Array
              last.push val
            end
          end,
          :after_group => lambda do |name:, depth:, before:|
            stack.pop
          end,
          :after_type => lambda do |type:, depth:, before:|
            stack.pop
          end,
          :after_array => lambda do |index:, decl:, depth:, before: nil|
            stack.pop
          end,
      }, *models)
  map
end
transform_datamodel(lambdas, *models) click to toggle source

@param models - the models to transform @lambdas - set of function callbacks to transform

# File src/transform.rb, line 72
def transform_datamodel lambdas, *models
  for model in models
    dom = cond_call(lambdas, :before_model, *before_model_lambda(model: model))
    for typename in model.contents.keys
      grp = cond_call(lambdas, :before_group, *before_group_lambda(name: typename))
      t = 0
      # there will be more than one of each type
      for type in model.contents[typename]
        transform_entry(lambdas, decl: type, name: typename, depth: 0, index: 0, total: 1)
        t = t + 1
      end
      grp = cond_call(lambdas, :after_group, *after_group_lambda(name: typename, before: grp))
    end
    cond_call(lambdas, :after_model, *after_model_lambda(model: model, before: dom))
  end
end
transform_entry(lambdas, index:, name:, decl:, depth:, total:) click to toggle source
# File src/transform.rb, line 119
def transform_entry(lambdas, index:, name:, decl:, depth:, total:)

  if decl.class <= Array
    arrctx = cond_call(lambdas, :before_array, *before_array_lambda(name: name, decl: decl, depth: depth, total: decl.length))
    j = 0
    for aa in decl
      transform_entry(lambdas, index: j, decl: aa, name: name, depth: depth, total: decl.length)
      j = j + 1
    end
    cond_call(lambdas, :after_array, *after_array_lambda(index: index, decl: decl, depth: depth, before: arrctx))
  elsif decl.class <= DataType
    before = cond_call(lambdas, :before_type, *before_type_lambda(type: decl, depth: depth, index: index, total: total))
    i = 0
    for ak in decl.attributes.keys
      av = decl.attributes[ak]
      transform_entry(lambdas, name: ak, decl: av, depth: depth + 1, index: i, total: decl.attributes.keys.length)
      i = i + 1
    end
    cond_call(lambdas, :after_type, *after_type_lambda(type: decl, depth: depth, before: before))
  else
    cond_call(lambdas, :attribute, *attribute_lambda(id: name, val: decl, index: index, depth: depth, total: total))
  end
  self
end
transform_metamodel(lambdas, *models) click to toggle source
# File src/transform.rb, line 89
def transform_metamodel lambdas, *models
  for model in models
    dom = cond_call(lambdas, :before_model, *before_model_lambda(model: model))
    for type in model.types.values
      before = cond_call(lambdas, :before_type, *before_type_lambda(type: type, index: 0, total: 1))
      i = 0
      for ak in type.attributes.keys
        av = type.attributes[ak]
        cond_call(lambdas, :attribute, *attribute_lambda(id: ak, val: av, type: type, index: i, total: type.attributes.keys.length))
        i = i + 1
      end
      cond_call(lambdas, :after_type, *after_type_lambda(type: type, before: before))
    end
    cond_call(lambdas, :after_model, *after_model_lambda(model: model, before: dom))
    if model.respond_to? :codes
      cond_call(lambdas, :before_codes, *before_codes_lambda(model: model))
      for code in model.codes.values
        cond_call(lambdas, :code, *code_lambda(model: model, code: code))
      end
    end
  end
end