Skip to content

Commit

Permalink
Fill out the templates and add ERB variable
Browse files Browse the repository at this point in the history
Add content to the template files and add a @class_name_underscore
variable to the ERB processing. Now, you pass a ConstantStyle name to
the executable and it will convert it to an underscored format where
needed.
  • Loading branch information
Tim Harvey committed Feb 8, 2012
1 parent 7398a62 commit 8bad2be
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
22 changes: 14 additions & 8 deletions bin/mini-cutter
Expand Up @@ -2,23 +2,29 @@

require 'erb'

class_name = ARGV[0]

class MyVars
def initialize(class_name)
@class_name = class_name
def initialize(class_name, class_name_underscore)
@class_name = class_name
@class_name_underscore = class_name_underscore
end
end

class_name = ARGV[0]
# Dirt simple way to convert 'AbcDef' to 'abc_def'
class_name_underscore = class_name.gsub(/(.)([A-Z])/, '\1_\2').downcase

var_binding = MyVars.new(class_name, class_name_underscore).send(:binding)

Dir.glob("#{File.dirname(__FILE__)}/../templates/*.erb") do |file|
erb_template = ERB.new File.open(file, 'r').read
erb_result = erb_template.result(MyVars.new(class_name).send(:binding))
erb_result = erb_template.result(var_binding)

# Remove the path from the filename
# Remove the path from the file string
file_name = File.basename file

# Remove .erb from the filename, and replace
# the 'class_name' from the file name
new_file_name = file_name.gsub(/.erb/, '').gsub(/class_name/, class_name)
# the 'class_name' placeholder from the file name
new_file_name = file_name.gsub(/.erb/, '').gsub(/class_name/, class_name_underscore)

puts "Creating: #{new_file_name}"

Expand Down
2 changes: 1 addition & 1 deletion mini-cutter.gemspec
Expand Up @@ -2,7 +2,7 @@ Gem::Specification.new do |s|
s.name = 'mini-cutter'
s.summary = 'Mini Cutter: create the boilerplate files needed for a mini-gem'
s.description = 'Quick tool for building out a quick set of files needed to make a micro-gem'
s.version = '0.1.1'
s.version = '0.1.2'
s.platform = Gem::Platform::RUBY

s.files = Dir['bin/*'] + Dir['templates/*']
Expand Down
17 changes: 16 additions & 1 deletion templates/README.md.erb
@@ -1 +1,16 @@
Class: <%= @class_name %>
# <%= @class_name %>

Description and usage:

require '<%= @class_name_underscore %>'
<%= @class_name %>.new

# Running the tests

Install the rspec gem

gem install rspec

Run the spec

rspec <%= @class_name_underscore %>_spec.rb
18 changes: 18 additions & 0 deletions templates/class_name.gemspec.erb
@@ -0,0 +1,18 @@
Gem::Specification.new do |s|
s.name = '<%= @class_name_underscore %>'
s.summary = ''
s.description = ''
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY

s.files = ['<%= @class_name_underscore %>.rb']
s.require_path = '.'

s.author = ''
s.email = ''
s.homepage = ''

s.test_file = '<%= @class_name_underscore %>_spec.rb'
s.add_development_dependency('rspec', ["~> 2.8"])
end

3 changes: 3 additions & 0 deletions templates/class_name.rb.erb
@@ -0,0 +1,3 @@
class <%= @class_name %>

This comment has been minimized.

Copy link
@trucktank

trucktank Jan 25, 2020

suqp88

end
7 changes: 7 additions & 0 deletions templates/class_name_spec.rb.erb
@@ -0,0 +1,7 @@
require File.expand_path('<%= @class_name_underscore %>')

describe <%= @class_name %> do
it 'should have tests' do
pending
end
end

0 comments on commit 8bad2be

Please sign in to comment.