/usr/lib/ruby/1.9.1/ramaze/contrib/sequel/relation.rb is in libramaze-ruby1.9.1 2010.06.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | # 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 :many_to_one, singular_sym(model), :class => model
end
def has_many(model)
todo :create_join, model
todo :many_to_many, plural_sym(model), :class => model
end
def has_one(model)
todo :many_to_one, singular_sym(model), :class => model
end
def todo(method, *args)
TODO[@left] << [method, *args]
end
private
def plural_sym(obj)
basename(obj).pluralize.to_sym
end
def singular_sym(obj)
basename(obj).to_sym
end
def basename(obj)
obj.to_s.split('::').last.downcase
end
end
end
|