Sha256: f71577ebcee2b18228349a35a8bb3ee050d86597bad8e7feee3cee250403ab8a

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module Rasti
  module DB
    module Relations
      class Base

        include Sequel::Inflections

        attr_reader :name, :source_collection_class

        def initialize(name, source_collection_class, options={})
          @name = name
          @source_collection_class = source_collection_class
          @options = options
        end

        def target_collection_class
          @target_collection_class ||= options[:collection].is_a?(Class) ? options[:collection] : Consty.get(options[:collection] || camelize(pluralize(name)), source_collection_class)
        end

        def one_to_many?
          self.class == OneToMany
        end

        def many_to_one?
          self.class == ManyToOne
        end

        def many_to_many?
          self.class == ManyToMany
        end

        def one_to_one?
          self.class == OneToOne
        end

        def from_one?
          one_to_one? || one_to_many?
        end

        def from_many?
          many_to_one? || many_to_many?
        end

        def to_one?
          one_to_one? || many_to_one?
        end

        def to_many?
          one_to_many? || many_to_many?
        end

        def join_relation_name(prefix)
          with_prefix prefix, name
        end

        private

        attr_reader :options

        def with_prefix(prefix, name)
          [prefix, name].compact.join('__').to_sym
        end

        def validate_join!
          if source_collection_class.data_source_name != target_collection_class.data_source_name
            raise "Invalid join of multiple data sources: #{source_collection_class.data_source_name}.#{source_collection_class.collection_name} > #{target_collection_class.data_source_name}.#{target_collection_class.collection_name}"
          end
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rasti-db-4.2.0 lib/rasti/db/relations/base.rb
rasti-db-4.1.1 lib/rasti/db/relations/base.rb
rasti-db-4.1.0 lib/rasti/db/relations/base.rb
rasti-db-4.0.0 lib/rasti/db/relations/base.rb
rasti-db-3.0.0 lib/rasti/db/relations/base.rb