Class: GrapeBootstrap::Commands::Generate

Inherits:
Base
  • Object
show all
Defined in:
lib/grape_bootstrap/commands/generate.rb

Instance Attribute Summary

Attributes inherited from Base

#path

Instance Method Summary collapse

Constructor Details

#initialize(path, type, options = []) ⇒ Generate

Returns a new instance of Generate.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/grape_bootstrap/commands/generate.rb', line 7

def initialize path, type, options=[]
  super path, options
  if type.nil?
    throw "Type not specified"
  end
  # Ugly hack for models vs api
  @type = 'model' == type ? 'models' : type
  @name = options.shift

  if @name.nil?
    throw "#{@type} needs a name to be generated"
  end

  self.send @type.downcase, @name
end

Instance Method Details

#render_template(name) ⇒ Object Also known as: models, api



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grape_bootstrap/commands/generate.rb', line 23

def render_template name
  template_type = @type
  erb_binding = -> {
    name = name.camelize
    binding
  }
  
  template_path = [
                    File.dirname(__FILE__), 
                    "..", 
                    "templates",
                    "app",
                    template_type,
                    "new.rb.erb"
                  ].join('/')
  tpl = ERB.new(File.read(template_path))
  target_path = "app/#{template_type}/#{name.underscore}.rb"

  File.open("#{@path}/#{target_path}", 'w+') do |file|
    file << tpl.result(erb_binding.call)
  end
end