Class: Cubic::Generator::Base

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

Overview

All generators (model, view, controller) inherit from the base class.

Direct Known Subclasses

App, Controller, Gemfile, Migrations, Model, View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize
  @files = []
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



7
8
9
# File 'lib/cubic/generators/base.rb', line 7

def files
  @files
end

Instance Method Details

#callbackObject

If callback is not defined within a generator, this method will be called to avoid a ‘NoMethodError’.



15
# File 'lib/cubic/generators/base.rb', line 15

def callback; end

#generateObject

Generate takes an array of hashes from each generator, then creates a file from those params. If a generator requires a unique file generation, this method will be overwritten in that file.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cubic/generators/base.rb', line 21

def generate
  @files.each do |info|
    path = File.join(Config[:root_path], info[:path])

    FileUtils.mkdir_p(path) unless File.directory?(path)

    full_path = File.join(path, info[:name])
    File.open(full_path, 'w') { |f| f.write(info[:content]) }
  end
  callback
end