Sha256: aa3433917250dcddef43e7522f2d80f6544279c4041d7d0f65e6b1b10806b1f6

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

#          Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

# Pretty DSL to express Sequel relations
#
# Usage:
#   SequelRelation.relations do
#     the User do
#       has_many Article
#       has_one Avatar
#     end
#
#     the Article do
#       belongs_to User
#     end
#
#     the Avatar do
#       belongs_to User
#     end
#   end

module SequelRelation
  def self.relations(&block)
    rema = RelationshipManager.new(&block)
    rema.finalize
  end

  class RelationshipManager
    TODO = {}

    def initialize(&block)
      instance_eval(&block)
    end

    def finalize
      TODO.keys.each do |model|
        model.create_table unless model.table_exists?
      end

      TODO.each do |model, instructions|
        instructions.each do |args|
          model.send(*args)
        end
      end

=begin
     # uncomment for debugging

      pp TODO

      TODO.keys.each do |model|
        puts "the #{model}"
        assoc = model.send(:association_reflections)
        assoc.each do |key, reflection|
          puts "  #{reflection[:type]} => #{key}"
        end
      end
=end
    end

    def the(left_model, &block)
      @left = left_model
      TODO[@left] = []
      instance_eval(&block)
    end

    def belongs_to(model)
      todo :belongs_to, model.to_s.downcase.to_sym
    end

    def has_many(model)
      todo :create_join, model
      todo :many_to_many, model.to_s.downcase.pluralize.to_sym
    end

    def has_one(model)
      todo :belongs_to, model.to_s.downcase.to_sym
    end

    def todo(method, *args)
      TODO[@left] << [method, *args]
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
Pistos-ramaze-2009.04.08 lib/ramaze/contrib/sequel/relation.rb
manveru-ramaze-2009.04.01 lib/ramaze/contrib/sequel/relation.rb
manveru-ramaze-2009.04.08 lib/ramaze/contrib/sequel/relation.rb