Sha256: a4bee1b08fdd6e26b30091aea14397a0fb240c573375514f90a12070092eb1ae

Contents?: true

Size: 1.78 KB

Versions: 11

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module ROM
  class Relation
    # ViewDSL is exposed in `Relation.view` method
    #
    # This is used to establish pre-defined relation views with explicit schemas.
    # Such views can be used to compose relations together, even from multiple
    # adapters. In advanced adapters like rom-sql using view DSL is not required though,
    # as relation schemas are dynamic and they always represent current tuple structure.
    #
    # @api public
    class ViewDSL
      # @!attribute [r] name
      #   @return [Symbol] The view name (relation method)
      attr_reader :name

      # @!attribute [r] relation_block
      #   @return [Proc] The relation block that will be evaluated by the view method
      attr_reader :relation_block

      # @!attribute [r] new_schema
      #   @return [Proc] The schema proc returned by the schema DSL
      attr_reader :new_schema

      # @api private
      def initialize(name, schema, &block)
        @name = name
        @schema = schema
        @new_schema = nil
        @relation_block = nil
        instance_eval(&block)
      end

      # Define a schema for a relation view
      #
      # @return [Proc]
      #
      # @see Relation::ClassInterface.view
      #
      # @api public
      def schema(&block)
        @new_schema = -> relations { @schema.with(relations: relations).instance_exec(&block) }
      end

      # Define a relation block for a relation view
      #
      # @return [Proc]
      #
      # @see Relation::ClassInterface.view
      #
      # @api public
      def relation(&block)
        @relation_block = lambda(&block)
      end

      # Return procs captured by the DSL
      #
      # @return [Array]
      #
      # @api private
      def call
        [name, new_schema, relation_block]
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rom-core-5.2.5 lib/rom/relation/view_dsl.rb
rom-core-5.2.4 lib/rom/relation/view_dsl.rb
rom-core-5.2.3 lib/rom/relation/view_dsl.rb
rom-core-5.2.2 lib/rom/relation/view_dsl.rb
rom-core-5.2.1 lib/rom/relation/view_dsl.rb
rom-core-5.1.2 lib/rom/relation/view_dsl.rb
rom-core-5.1.1 lib/rom/relation/view_dsl.rb
rom-core-5.1.0 lib/rom/relation/view_dsl.rb
rom-core-5.0.2 lib/rom/relation/view_dsl.rb
rom-core-5.0.1 lib/rom/relation/view_dsl.rb
rom-core-5.0.0 lib/rom/relation/view_dsl.rb