I am creating a gem that has some model, controller and view components in it. I need to emulate a rails like mvc pattern for it. There are two options that come to my mind and I need to choose one of them. The following code is a simpler extraction of the problem.
Pattern1
project/model.rb
module Application
module Namespace
class Model
def name
'Mr. Model'
end
end
end
end
project/controller.rb
module Application
module Namespace
class Controller
def action
Model.new.name
end
end
end
end
project/application.rb
require_relative 'controller'
require_relative 'model'
module Application
class Runner
def run
Namespace::Controller.new.action
end
end
end
Pattern2
project/model.rb
class Model
def name
'Mr. Model'
end
end
project/controller.rb
class Controller
def action
Model.new.name
end
end
project/application.rb
module Application
module Namespace
module_eval File.read(File.expand_path '../controller.rb', __FILE__)
module_eval File.read(File.expand_path '../model.rb', __FILE__)
end
class Runner
def run
Namespace::Controller.new.action
end
end
end
irb(main):001:0> require 'project/application'
=> true
irb(main):002:0> Application::Runner.new.run
=> "Mr. Model"
Both patterns include the models and controllers under a namespace, but with the pattern1 whenever I add new files, I will have to duplicate the ugly modular nesting. The pattern2 creates cleaner models and controllers with some extra magic being done in the application.
I would like some suggestions on these approaches or if there are better solutions to the problem. Questions like why a mvc pattern is needed anyways will be complicated to answer. Lets assume that a mvc pattern is needed and try to answer whats the cleanest way to emulate it.
EDIT:
On further thoughts, Rails uses subclasses. So we have a third pattern now.
Pattern3
project/application_controller.rb
module Application
module Namespace
class ApplicationController
end
end
end
project/active_model.rb
module Application
module Namespace
class ActiveModel
end
end
end
project/model.rb
class Model < Application::Namespace::ActiveModel
def name
'Mr. Model'
end
end
project/controller.rb
class Controller < Application::Namespace::ApplicationController
def action
Model.new.name
end
end
project/application.rb
module Application
require_relative 'active_model'
require_relative 'application_controller'
require_relative 'controller'
require_relative 'model'
class Runner
def run
Controller.new.action
end
end
end
Still looking for some brighter ideas.