class ROM::StructCompiler
@api private
Public Class Methods
new(*)
click to toggle source
@api private
Calls superclass method
# File lib/rom/struct_compiler.rb, line 23 def initialize(*) super @cache = cache.namespaced(:structs) end
Public Instance Methods
call(*args)
click to toggle source
Build a struct class based on relation header ast
@api private
# File lib/rom/struct_compiler.rb, line 32 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| klass.attributes(attributes.to_h) 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 100 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 107 def class_name(name) Inflector.classify(name) end
visit_attribute(node)
click to toggle source
@api private
# File lib/rom/struct_compiler.rb, line 73 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 87 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 80 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 94 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 51 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::Nominal.new(model).constructor(&model.method(:new)) end if meta[:combine_type] == :many [name, Types::Array.of(member)] else [name, member.optional] end end