class Cubic::Generator::Model

Model fulfils the M in the MVC pattern. Files the Model generator creates will include a class inheriting from the class your ORM suggests.

Public Class Methods

new() click to toggle source
Calls superclass method Cubic::Generator::Base::new
# File lib/cubic/generators/model.rb, line 8
def initialize
  @migration = Migrations.new
  super()
end

Public Instance Methods

add(*code) click to toggle source

Allows user to specify code that should be included in generated model file. Example: add('has_many :users')

# File lib/cubic/generators/model.rb, line 32
def add(*code)
  code *= "\n"
  @files.last[:content] = insert_code(@files.last[:content], code)
end
callback() click to toggle source

Called after a file is successfully generated from hash created by design method.

# File lib/cubic/generators/model.rb, line 25
def callback
  @migration.generate
end
design(name, options = {}) click to toggle source

Creates a hash that will be used for file generation purposes

# File lib/cubic/generators/model.rb, line 14
def design(name, options = {})
  @migration.design(name, options)

  @files << { name: "#{name}.rb",
              path: '/app/models',
              content: format_model(name) }
  self
end

Private Instance Methods

format_model(name) click to toggle source
# File lib/cubic/generators/model.rb, line 39
def format_model(name)
  "class #{model_name(name)} < #{orm_class}\n\nend".chomp
end
insert_code(model, code) click to toggle source

Inserts code into the file to be generated.

# File lib/cubic/generators/model.rb, line 48
def insert_code(model, code)
  model.split(/(?<=#{orm_class})/).insert(1, code).join("\n")
end
model_name(name) click to toggle source
# File lib/cubic/generators/model.rb, line 43
def model_name(name)
  name.to_s.split('_').map(&:capitalize).join('')
end
orm_class() click to toggle source

Returns what class should be inherted from based on ORM specified in sitemap file.

# File lib/cubic/generators/model.rb, line 54
def orm_class
  case Config[:orm]
  when 'Sequel' || 'sequel'
    'Sequel::Model'
  else
    Config[:orm]
  end
end