class Cubic::Generator::Controller

Controller fulfils the C in the MVC pattern. Files the Model generator creates will include a class inheriting from the CubicController.

Public Class Methods

new() click to toggle source
Calls superclass method Cubic::Generator::Base::new
# File lib/cubic/generators/controller.rb, line 7
def initialize
  @view = View.new
  super()
end

Public Instance Methods

design(name, options = {}) click to toggle source

Creates a hash that will be used for file generation purposes

# File lib/cubic/generators/controller.rb, line 13
def design(name, options = {})
  if options[:actions] && options[:actions].any?
    actions = options[:actions]
    create_views(name, actions)
  else
    actions = []
  end

  @files << { name: "#{name}_controller.rb",
              path: '/app/controllers/',
              content: build_controller(name, actions) }
  self
end

Private Instance Methods

build_actions(name, actions) click to toggle source
# File lib/cubic/generators/controller.rb, line 52
def build_actions(name, actions)
  actions.map! do |a|
    build_method(name, a)
  end
  "\s\snamespace '#{name}' do\n#{actions.join}\n\s\send"
end
build_controller(name, actions) click to toggle source
# File lib/cubic/generators/controller.rb, line 40
def build_controller(name, actions)
  "#{controller_name(name)} < Cubic::CubicController\n#{build_actions(name, actions)}\nend"
end
build_method(name, a) click to toggle source
# File lib/cubic/generators/controller.rb, line 44
def build_method(name, a)
  "\n#{tab}get '#{a}' do\n#{tab}\s\shaml '#{name}/#{a}'\n#{tab}end\n"
end
callback() click to toggle source

Called after the Controller generated has created its files.

# File lib/cubic/generators/controller.rb, line 36
def callback
  @view.generate
end
controller_name(name) click to toggle source
# File lib/cubic/generators/controller.rb, line 48
def controller_name(name)
  "class #{name.to_s.split('_').map(&:capitalize).join('')}Controller"
end
create_views(name, actions) click to toggle source
# File lib/cubic/generators/controller.rb, line 29
def create_views(name, actions)
  actions.each do |a|
    @view.design(name, a)
  end
end
tab() click to toggle source
# File lib/cubic/generators/controller.rb, line 59
def tab
  "\s" * 4
end