add a totally improper set of inflection rules for plurals

This commit is contained in:
John Bintz 2011-04-18 11:36:46 -04:00
parent 77210490df
commit 2d6a3b81c9
6 changed files with 63 additions and 22 deletions

View File

@ -13,11 +13,19 @@ class BackboneGenerator < Thor
no_tasks do no_tasks do
def underscore_name def underscore_name
Thor::Util.snake_case(@name.gsub("::", "/")) singularize(Thor::Util.snake_case(@name.gsub("::", "/")))
end
def plural_underscore_name
pluralize(underscore_name)
end end
def object_name def object_name
@name.gsub('::', '') singularize(@name.gsub('::', ''))
end
def plural_object_name
pluralize(object_name)
end end
def generate_model def generate_model
@ -32,14 +40,14 @@ class BackboneGenerator < Thor
end end
def generate_collection def generate_collection
template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js") template('collection.js.erb', "public/javascripts/collections/#{plural_underscore_name}.js")
template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js") template('collection_spec.js.erb', "spec/javascripts/collections/#{plural_underscore_name}_spec.js")
end end
def generate_collection_view def generate_collection_view
template('collection_view.js.erb', "public/javascripts/views/#{underscore_name}s_view.js") template('collection_view.js.erb', "public/javascripts/views/#{plural_underscore_name}_view.js")
template('collection_view.jst.erb', "app/views/#{underscore_name}s/list.jst") template('collection_view.jst.erb', "app/views/#{plural_underscore_name}/list.jst")
template('collection_view_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_view_spec.js") template('collection_view_spec.js.erb', "spec/javascripts/views/#{plural_underscore_name}_view_spec.js")
end end
end end
@ -77,6 +85,15 @@ class BackboneGenerator < Thor
def spec_helper def spec_helper
template('spec_helper.js.erb', 'spec/javascripts/helpers/backbone_spec_helper.js') template('spec_helper.js.erb', 'spec/javascripts/helpers/backbone_spec_helper.js')
end end
private
def pluralize(string)
singularize(string) + 's'
end
def singularize(string)
string.gsub(%r{s$}, '')
end
end end
BackboneGenerator.start BackboneGenerator.start

View File

@ -35,8 +35,10 @@ describe 'backbone-generator' do
File.file?(spec = 'spec/javascripts/collections/section/models_spec.js').should be_true File.file?(spec = 'spec/javascripts/collections/section/models_spec.js').should be_true
File.read(collection).should match(/SectionModels/) File.read(collection).should match(/SectionModels/)
File.read(collection).should_not match(/SectionModelss/)
File.read(collection).should match(%r{section/model}) File.read(collection).should match(%r{section/model})
File.read(spec).should match(/SectionModels/) File.read(spec).should match(/SectionModels/)
File.read(spec).should_not match(/SectionModelss/)
end end
def should_generate_collection_view def should_generate_collection_view
@ -45,10 +47,12 @@ describe 'backbone-generator' do
File.file?(template = 'app/views/section/models/list.jst').should be_true File.file?(template = 'app/views/section/models/list.jst').should be_true
File.read(view).should match(/SectionModelsView/) File.read(view).should match(/SectionModelsView/)
File.read(view).should_not match(/SectionModelssView/)
File.read(view).should match(/SectionModelView/) File.read(view).should match(/SectionModelView/)
File.read(view).should match(/return this/) File.read(view).should match(/return this/)
File.read(view).should match(%r{template: JST\['section/models/list'\]}) File.read(view).should match(%r{template: JST\['section/models/list'\]})
File.read(spec).should match(/SectionModelsView/) File.read(spec).should match(/SectionModelsView/)
File.read(spec).should_not match(/SectionModelssView/)
end end
describe 'model' do describe 'model' do
@ -68,18 +72,38 @@ describe 'backbone-generator' do
end end
describe 'collection view' do describe 'collection view' do
it "should generate the collection view files" do context 'without trailing s' do
system %{bin/backbone-generator collection-view Section::Model} it "should generate the collection view files" do
system %{bin/backbone-generator collection-view Section::Model}
should_generate_collection_view should_generate_collection_view
end
end
context 'with trailing s' do
it "should generate the collection view files" do
system %{bin/backbone-generator collection-view Section::Models}
should_generate_collection_view
end
end end
end end
describe 'collection' do describe 'collection' do
it "should generate the collection files" do context 'without trailing s' do
system %{bin/backbone-generator collection Section::Model} it "should generate the collection files" do
system %{bin/backbone-generator collection Section::Model}
should_generate_collection should_generate_collection
end
end
context 'with trailing s' do
it "should generate the collection files" do
system %{bin/backbone-generator collection Section::Models}
should_generate_collection
end
end end
end end

View File

@ -1,4 +1,4 @@
var <%= object_name %>s = Backbone.Collection.extend({ var <%= plural_object_name %> = Backbone.Collection.extend({
url: '/<%= underscore_name %>', url: '/<%= underscore_name %>',
model: <%= object_name %> model: <%= object_name %>
}); });

View File

@ -1,12 +1,12 @@
describe('<%= object_name %>s', function() { describe('<%= plural_object_name %>', function() {
var collection; var collection;
withServer(); withServer();
it('should fetch records from the API', function() { it('should fetch records from the API', function() {
collection = new <%= object_name %>s(); collection = new <%= plural_object_name %>();
this.server.respondWith('GET', '<%= underscore_name %>', this.validJSONResponse([{id: 1}])); this.server.respondWith('GET', '<%= plural_underscore_name %>', this.validJSONResponse([{id: 1}]));
collection.fetch() collection.fetch()
this.server.respond(); this.server.respond();

View File

@ -1,5 +1,5 @@
var <%= object_name %>sView = Backbone.View.extend({ var <%= plural_object_name %>View = Backbone.View.extend({
template: JST['<%= underscore_name %>s/list'], template: JST['<%= plural_underscore_name %>/list'],
initialize: function(collection) { initialize: function(collection) {
_.bindAll(this, 'render', 'addOne', 'addAll'); _.bindAll(this, 'render', 'addOne', 'addAll');

View File

@ -1,12 +1,12 @@
describe('<%= object_name %>sView', function() { describe('<%= plural_object_name %>View', function() {
var view, collection; var view, collection;
beforeEach(function() { beforeEach(function() {
collection = new <%= object_name %>s(); collection = new <%= plural_object_name %>();
}); });
it('should render', function() { it('should render', function() {
view = new <%= object_name %>sView(collection); view = new <%= plural_object_name %>View(collection);
view.render(); view.render();
expect($(view.el)).toContain('.list'); expect($(view.el)).toContain('.list');