Sha256: d6743d4f8b3036e2d76c7f12f0b4e5ac77602a35171fbf746cb570e9298905b0

Contents?: true

Size: 765 Bytes

Versions: 3

Compression:

Stored size: 765 Bytes

Contents

require 'anima'

require 'rom/struct'

module ROM
  class Repository
    # @api private
    class StructBuilder
      attr_reader :registry

      def self.registry
        @__registry__ ||= {}
      end

      def initialize
        @registry = self.class.registry
      end

      def call(*args)
        name, columns = args
        registry[args.hash] ||= build_class(name) { |klass|
          klass.send(:include, Anima.new(*columns))
        }
      end
      alias_method :[], :call

      private

      def build_class(name, &block)
        ROM::ClassBuilder.new(name: class_name(name), parent: Struct).call(&block)
      end

      def class_name(name)
        "ROM::Struct[#{Inflector.classify(Inflector.singularize(name))}]"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-repository-0.2.0 lib/rom/repository/struct_builder.rb
rom-repository-0.2.0.rc1 lib/rom/repository/struct_builder.rb
rom-repository-0.2.0.beta1 lib/rom/repository/struct_builder.rb