Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
class User def exists true end def authenticate true end @@user_types = [] def User.inherited(user_type) puts "New sublcass inherited: #{user_type}" @@user_types.push(user_type) end def User.load(user_type) puts "Searching loaded user types for #{user_type}" @ret_val = nil @@user_types.each do |user_object| puts "Comparing #{user_type} to #{user_object}" if user_type == user_object.to_s puts "Found a positive match." @ret_val = user_object.new else puts "No match, checking next registered user subclass..." end end puts "Completed search of User subclasses..." @ret_val end # Fully working constructor system def User.create(user_type) puts "Searching loaded user types for #{user_type}" @ret_val = nil @@user_types.each do |user_object| tmp_obj = user_object.new() puts "Checking #{user_type} against #{tmp_obj.class}" if user_type == tmp_obj.class.to_s puts "Found a positive match." @ret_val = tmp_obj else puts "No match, checking next registered user subclass..." end end puts "Completed search of User subclasses..." @ret_val end end class Manager < User end class Employee < User end class Customer < User def authenticate false end end puts "-----------------------" @test_group = ["Manager", "Employee", "Customer", "bogus"] @test_group.each do |user_type| # I would like to be able to use user_type.new() or even "Employee".new() puts "" puts "Creating Object: " puts "" @me = User.load(user_type) puts "--------------------------" if @me puts "Object exists: " << @me.exists.to_s puts "Object able to be authenticated? " << @me.authenticate.to_s else puts "Object type: #{user_type} was not found in the User class tree." end puts "--------------------------" end
From the Design Piracy series on my blog: