Sha256: e366d157edf253d5f4cd7b58d6cc189d664dcaa5baf55f86c39759eb7c519243

Contents?: true

Size: 870 Bytes

Versions: 5

Compression:

Stored size: 870 Bytes

Contents

module ROM

  # @api private
  class RelationBuilder
    attr_reader :schema, :mod

    # @api private
    def initialize(schema)
      @schema = schema
      @mod = schema.each_with_object(Module.new) do |(name, relation), m|
        m.send(:define_method, name) { relation.dataset }
      end
    end

    # @api private
    def call(name)
      schema_relation = schema[name]

      klass = Class.new(Relation)

      klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def self.name
          "#{Relation.name}[#{Inflecto.camelize(name)}]"
        end

        def self.inspect
          name
        end

        def self.to_s
          name
        end

        def name
          #{name.inspect}
        end
      RUBY

      klass.send(:include, mod)

      yield(klass)

      klass.new(schema_relation.dataset, schema_relation.header)
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rom-0.4.2 lib/rom/relation_builder.rb
rom-0.4.1 lib/rom/relation_builder.rb
rom-0.4.0 lib/rom/relation_builder.rb
rom-0.3.1 lib/rom/relation_builder.rb
rom-0.3.0 lib/rom/relation_builder.rb