Created
November 24, 2010 03:41
-
-
Save burtlo/713068 to your computer and use it in GitHub Desktop.
submodule_override.rb =====================
Given a base module that may be extended that defines a core set of methods for a core set of services. Allow for a nested service override the base service methods based on an environment variable.
This file contains hidden or 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
require 'active_resource' | |
class User | |
DEFAULT_CONTEXT_USER = "Models::Database::User" | |
class << self | |
def method_missing(method,*args,&block) | |
contextual_user = "Models::#{ENV['context']}::User".constantize | |
if contextual_user.respond_to?(method) | |
contextual_user.send(method) | |
elsif DEFAULT_CONTEXT_USER.constantize.respond_to?(method) | |
DEFAULT_CONTEXT_USER.constantize.send(method) | |
else | |
super(method,*args,&block) | |
end | |
end | |
end | |
end | |
module Models | |
module Database | |
class User < ActiveRecord::Base ; end | |
end | |
module Browser | |
class User | |
class << self | |
def create | |
# creation through browser | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or 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 Models | |
module Browser | |
# | |
# When this module is extended open the classes up and alias override the operations. | |
# | |
def self.extended(base) | |
Models::User.class_eval do | |
class << self | |
alias_method :create, :create_through_browser | |
alias_method :validate, :validate_through_browser | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or 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
require 'active_resource' | |
module Services | |
# | |
# Services defined in the module Services have been extended. | |
# Upon extension there may be overrides that exist that we may want to apply. | |
# | |
# | |
def self.extended(base) | |
puts "Extending Basic Services" | |
puts "Extending #{ENV['services']} Services" | |
base.extend "#{self}::#{ENV['services']}".constantize | |
end | |
def get_time | |
puts "Basic Services Time" | |
Time.now | |
end | |
end | |
module Services | |
module Mock | |
def get_time | |
puts "Tricked you, there is no time" | |
end | |
end | |
end | |
module Services | |
module SOAP | |
def get_time | |
puts "Making a soap call to get the time" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment