class GrapeBootstrap::Commands::Generate

Public Class Methods

new(path, type, options=[]) click to toggle source
Calls superclass method GrapeBootstrap::Commands::Base::new
# 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

Public Instance Methods

api(name)
Alias for: render_template
models(name)
Alias for: render_template
render_template(name) click to toggle source
# 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
Also aliased as: models, api