lib/rom/repository/struct_builder.rb in rom-repository-0.3.1 vs lib/rom/repository/struct_builder.rb in rom-repository-1.0.0.beta1
- old
+ new
@@ -1,26 +1,28 @@
+require 'dry/core/inflector'
+require 'dry/core/cache'
+require 'dry/core/class_builder'
+
require 'rom/struct'
+require 'rom/schema/type'
-require 'rom/support/cache'
-require 'rom/support/constants'
-require 'rom/support/class_builder'
-
-require 'rom/repository/struct_attributes'
-
module ROM
class Repository
# @api private
class StructBuilder
- extend Cache
+ extend Dry::Core::Cache
def call(*args)
fetch_or_store(*args) do
name, header = args
+ attributes = visit(header).compact
- build_class(name) { |klass|
- klass.send(:include, StructAttributes.new(visit(header)))
- }
+ build_class(name, ROM::Struct) do |klass|
+ attributes.each do |(name, type)|
+ klass.attribute(name, type)
+ end
+ end
end
end
alias_method :[], :call
private
@@ -33,23 +35,33 @@
def visit_header(node)
node.map(&method(:visit))
end
def visit_relation(node)
- relation_name, meta, * = node
- meta[:combine_name] || relation_name.relation
+ relation_name, meta, header = node
+ name = meta[:combine_name] || relation_name.relation
+
+ model = call(name, header)
+
+ if meta[:combine_type] == :many
+ [name, Types::Array.member(model)]
+ else
+ [name, model.optional]
+ end
end
- def visit_attribute(node)
- node
+ def visit_attribute(attr)
+ unless attr.foreign_key?
+ [attr.aliased? && !attr.wrapped? ? attr.alias : attr.name, attr.type]
+ end
end
- def build_class(name, &block)
- ROM::ClassBuilder.new(name: class_name(name), parent: Struct).call(&block)
+ def build_class(name, parent, &block)
+ Dry::Core::ClassBuilder.new(name: class_name(name), parent: parent).call(&block)
end
def class_name(name)
- "ROM::Struct[#{Inflector.classify(Inflector.singularize(name))}]"
+ "ROM::Struct[#{Dry::Core::Inflector.classify(Dry::Core::Inflector.singularize(name))}]"
end
end
end
end