Created
January 13, 2009 22:38
-
-
Save jsmestad/46663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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