Skip to content

Instantly share code, notes, and snippets.

@jsmestad
Created January 13, 2009 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmestad/46663 to your computer and use it in GitHub Desktop.
Save jsmestad/46663 to your computer and use it in GitHub Desktop.
module DataMapper
module Resource
def delegate(*methods)
options = methods.pop
unless options.is_a?(Hash) && to = options[:to]
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
end
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
end
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
allow_nil = options[:allow_nil] && "#{to} && "
methods.each do |method|
module_eval(<<-EOS, "(__DELEGATION__)", 1)
def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
#{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block)
end # end
EOS
end
end
end
end
gems/dm-core-0.9.9/lib/dm-core/model.rb:478:in `method_missing': Unknown property 'delegate' (ArgumentError)
from /Users/justinsmestad/Documents/Factory/smoke_hut/app/models/resort.rb:11
Merb::BootLoader.before_app_loads do
# This will get executed after dependencies have been loaded but before your app's classes have loaded.
require Merb.root / "lib" / "delegation"
end
Merb::BootLoader.after_app_loads do
# This will get executed after your app's classes have been loaded.
end
class Resort
include DataMapper::Resource
property :id, Serial
property :name, String
# Potential Resort Stats
has n, :passholders, :class_name => 'User'
has 1, :shortcode
delegate(:messages, :to => :shortcode)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment