class ROM::StructCompiler

@api private

Public Class Methods

new(*args) click to toggle source

@api private

Calls superclass method
# File lib/rom/struct_compiler.rb, line 21
def initialize(*args)
  super
  @cache = cache.namespaced(:structs)
end

Public Instance Methods

[](*args)
Alias for: call
call(*args) click to toggle source

Build a struct class based on relation header ast

@api private

# File lib/rom/struct_compiler.rb, line 29
def call(*args)
  cache.fetch_or_store(args) do
    name, header, ns = args
    attributes = header.map(&method(:visit)).compact

    if attributes.empty?
      ROM::OpenStruct
    else
      build_class(name, ROM::Struct, ns) do |klass|
        attributes.each do |(attr_name, type)|
          klass.attribute(attr_name, type)
        end
      end
    end
  end
end
Also aliased as: []

Private Instance Methods

build_class(name, parent, ns, &block) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 99
def build_class(name, parent, ns, &block)
  Dry::Core::ClassBuilder.
    new(name: class_name(name), parent: parent, namespace: ns).
    call(&block)
end
class_name(name) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 106
def class_name(name)
  Inflector.classify(Inflector.singularize(name))
end
visit_attribute(node) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 72
def visit_attribute(node)
  name, type, meta = node

  [meta[:alias] && !meta[:wrapped] ? meta[:alias] : name, visit(type).meta(meta)]
end
visit_constrained(node) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 86
def visit_constrained(node)
  definition, _ = node

  visit(definition)
end
visit_constructor(node) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 79
def visit_constructor(node)
  definition, * = node

  visit(definition)
end
visit_enum(node) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 93
def visit_enum(node)
  type_node, * = node
  visit(type_node)
end
visit_relation(node) click to toggle source

@api private

# File lib/rom/struct_compiler.rb, line 50
def visit_relation(node)
  _, header, meta = node
  name = meta[:combine_name] || meta[:alias]
  namespace = meta.fetch(:struct_namespace)

  model = meta[:model] || call(name, header, namespace)

  member =
    if model < Dry::Struct
      model
    else
      Dry::Types::Definition.new(model).constructor(&model.method(:new))
    end

  if meta[:combine_type] == :many
    [name, Types::Array.of(member)]
  else
    [name, member.optional]
  end
end