Class: GrapeBootstrap::Commands::Generate
- Defined in:
- lib/grape_bootstrap/commands/generate.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(path, type, options = []) ⇒ Generate
constructor
A new instance of Generate.
- #render_template(name) ⇒ Object (also: #models, #api)
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, =[] super path, if type.nil? throw "Type not specified" end # Ugly hack for models vs api @type = 'model' == type ? 'models' : type @name = .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 |