Sha256: 277eea759ad606da0f757d27976c7a653c03c51cdc6ad35232b986a776d4fa67

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

module ROM
  module SQL
    class Relation < ROM::Relation
      module ClassMethods
        def inherited(klass)
          klass.class_eval do
            class << self
              attr_reader :model, :associations
            end
          end
          klass.instance_variable_set('@model', Class.new(Sequel::Model))
          klass.instance_variable_set('@associations', [])
          super
        end

        def one_to_many(name, options)
          associations << [__method__, name, options.merge(relation: name)]
        end

        def many_to_many(name, options = {})
          associations << [__method__, name, options.merge(relation: name)]
        end

        def many_to_one(name, options = {})
          relation_name = Inflector.pluralize(name).to_sym
          new_options = options.merge(relation: relation_name)
          associations << [__method__, name, new_options]
        end

        def finalize(relations, relation)
          model.set_dataset(relation.dataset)
          model.dataset.naked!

          associations.each do |*args, options|
            model = relation.model
            other = relations[options.fetch(:relation)].model

            model.public_send(*args, options.merge(class: other))
          end

          model.freeze

          super
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rom-fmp-0.0.4 lib/rom/fmp/relation/class_methods.rb
rom-sql-0.4.1 lib/rom/sql/relation/class_methods.rb
rom-sql-0.4.0 lib/rom/sql/relation/class_methods.rb
rom-sql-0.4.0.rc1 lib/rom/sql/relation/class_methods.rb
rom-sql-0.4.0.beta2 lib/rom/sql/relation/class_methods.rb