Class: Cubic::Generator::Model

Inherits:
Base
  • Object
show all
Defined in:
lib/cubic/generators/model.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Base

#files

Instance Method Summary collapse

Methods inherited from Base

#generate

Constructor Details

#initializeModel

Returns a new instance of Model.



8
9
10
11
# File 'lib/cubic/generators/model.rb', line 8

def initialize
  @migration = Migrations.new
  super()
end

Instance Method Details

#add(*code) ⇒ Object

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



32
33
34
35
# File 'lib/cubic/generators/model.rb', line 32

def add(*code)
  code *= "\n"
  @files.last[:content] = insert_code(@files.last[:content], code)
end

#callbackObject

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



25
26
27
# File 'lib/cubic/generators/model.rb', line 25

def callback
  @migration.generate
end

#design(name, options = {}) ⇒ Object

Creates a hash that will be used for file generation purposes



14
15
16
17
18
19
20
21
# 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