commit cfa1fbe2bf688b1998eb0d35f056dfda8fedb295 Author: John Bintz Date: Fri Apr 15 14:55:37 2011 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4040c6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.gem +.bundle +Gemfile.lock +pkg/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7bec1ad --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in backbone-generator.gemspec +gemspec + +gem 'autotest' + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..14cfe0b --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require 'bundler' +Bundler::GemHelper.install_tasks diff --git a/autotest/discover.rb b/autotest/discover.rb new file mode 100644 index 0000000..008a5c4 --- /dev/null +++ b/autotest/discover.rb @@ -0,0 +1,2 @@ +Autotest.add_discovery { 'rspec2' } + diff --git a/backbone-generator.gemspec b/backbone-generator.gemspec new file mode 100644 index 0000000..d91edb0 --- /dev/null +++ b/backbone-generator.gemspec @@ -0,0 +1,24 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "backbone-generator/version" + +Gem::Specification.new do |s| + s.name = "backbone-generator" + s.version = Backbone::Generator::VERSION + s.platform = Gem::Platform::RUBY + s.authors = ["John Bintz"] + s.email = ["john@coswellproductions.com"] + s.homepage = "" + s.summary = %q{Generate Backbone-related files similar to Rails generators.} + s.description = %q{Generate Backbone-related files similar to Rails generators.} + + s.rubyforge_project = "backbone-generator" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.add_dependency 'thor' + s.add_development_dependency 'rspec' +end diff --git a/bin/backbone-generator b/bin/backbone-generator new file mode 100755 index 0000000..2c4b563 --- /dev/null +++ b/bin/backbone-generator @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +require 'rubygems' +require 'thor' +require 'thor/group' + +class BackboneGenerator < Thor + include Thor::Actions + + def self.source_root + File.expand_path('../../templates', __FILE__) + end + + no_tasks do + def underscore_name + Thor::Util.snake_case(@name.gsub("::", "/")) + end + + def object_name + @name.gsub('::', '') + end + end + + desc 'model Namespaced::Name', "Create a model" + def model(name) + @name = name + template('model.js.erb', "public/javascripts/models/#{underscore_name}.js") + template('model_spec.js.erb', "spec/javascripts/models/#{underscore_name}_spec.js") + end + + desc 'view Namespaced::Name', "Create a view" + def view(name) + @name = name + template('view.js.erb', "public/javascripts/views/#{underscore_name}.js") + template('view.jst.erb', "app/views/#{underscore_name}.jst") + template('view_spec.js.erb', "spec/javascripts/views/#{underscore_name}_spec.js") + end + + desc 'collection Namespaced::Name', "Create a collection" + def collection(name) + @name = name + template('collection.js.erb', "public/javascripts/collections/#{underscore_name}.js") + template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}_spec.js") + end +end + +BackboneGenerator.start diff --git a/lib/backbone-generator.rb b/lib/backbone-generator.rb new file mode 100644 index 0000000..877bd31 --- /dev/null +++ b/lib/backbone-generator.rb @@ -0,0 +1,5 @@ +module Backbone + module Generator + # Your code goes here... + end +end diff --git a/lib/backbone-generator/version.rb b/lib/backbone-generator/version.rb new file mode 100644 index 0000000..9c9af8f --- /dev/null +++ b/lib/backbone-generator/version.rb @@ -0,0 +1,5 @@ +module Backbone + module Generator + VERSION = "0.0.1" + end +end diff --git a/spec/bin/backbone-generator_spec.rb b/spec/bin/backbone-generator_spec.rb new file mode 100644 index 0000000..ee26c48 --- /dev/null +++ b/spec/bin/backbone-generator_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' +require 'fileutils' + +describe 'backbone-generator' do + def clean! + FileUtils.rm_rf 'public' + FileUtils.rm_rf 'spec/javascripts' + FileUtils.rm_rf 'app' + end + + before { clean! } + after { clean! } + + describe 'model' do + it "should generate the model files" do + system %{bin/backbone-generator model Section::Model} + + File.file?(model = 'public/javascripts/models/section/model.js').should be_true + File.file?(spec = 'spec/javascripts/models/section/model_spec.js').should be_true + + File.read(model).should match(/SectionModel/) + File.read(spec).should match(/SectionModel/) + end + end + + describe 'view' do + it "should generate the view files" do + system %{bin/backbone-generator view Section::View} + + File.file?(view = 'public/javascripts/views/section/view.js').should be_true + File.file?(spec = 'spec/javascripts/views/section/view_spec.js').should be_true + File.file?(template = 'app/views/section/view.jst').should be_true + + File.read(view).should match(/SectionView/) + File.read(view).should match(%r{template: JST\['section/view'\]}) + File.read(spec).should match(/SectionView/) + end + end + + describe 'collection' do + it "should generate the collection files" do + system %{bin/backbone-generator collection Section::Collection} + + File.file?(collection = 'public/javascripts/collections/section/collection.js').should be_true + File.file?(spec = 'spec/javascripts/collections/section/collection_spec.js').should be_true + + File.read(collection).should match(/SectionCollection/) + File.read(collection).should match(%r{section/collection}) + File.read(spec).should match(/SectionCollection/) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e69de29 diff --git a/templates/collection.js.erb b/templates/collection.js.erb new file mode 100644 index 0000000..c73cbd3 --- /dev/null +++ b/templates/collection.js.erb @@ -0,0 +1,3 @@ +var <%= object_name %>Collection = Backbone.Collection.extend({ + url: '/<%= underscore_name %>' +}); diff --git a/templates/collection_spec.js.erb b/templates/collection_spec.js.erb new file mode 100644 index 0000000..c825a16 --- /dev/null +++ b/templates/collection_spec.js.erb @@ -0,0 +1,21 @@ +describe('<%= object_name %>Collection', function() { + var collection, server; + + beforeEach(function() { + server = sinon.fakeServer().create(); + }); + + afterEach(function() { + server.restore(); + }); + + it('should fetch records from the API', function() { + collection = new <%= object_name %>Collection(); + + server.respondWith('GET', '<%= underscore_name %>', [ 200, { 'Content-type': 'application/json' }, "[{id: 1}]" ]); + collection.fetch() + server.respond(); + + expect(collection.length).toEqual(1); + }); +}); diff --git a/templates/model.js.erb b/templates/model.js.erb new file mode 100644 index 0000000..110d0e4 --- /dev/null +++ b/templates/model.js.erb @@ -0,0 +1,4 @@ +var <%= object_name %>Model = Backbone.Model.extend({ + +}); + diff --git a/templates/model_spec.js.erb b/templates/model_spec.js.erb new file mode 100644 index 0000000..ca9351f --- /dev/null +++ b/templates/model_spec.js.erb @@ -0,0 +1,10 @@ +describe('<%= object_name %>Model', function() { + var model; + + it('should have some tests', function() { + model = new <%= object_name %>Model(); + + expect(true).toEqual(false); + }); +}); + diff --git a/templates/view.js.erb b/templates/view.js.erb new file mode 100644 index 0000000..defd16c --- /dev/null +++ b/templates/view.js.erb @@ -0,0 +1,10 @@ +var <%= object_name %>View = Backbone.View.extend({ + template: JST['<%= underscore_name %>'], + initialize: function() { + _.bindAll(this, 'render'); + }, + render: function() { + $(this.el).html(this.template()); + } +}); + diff --git a/templates/view.jst.erb b/templates/view.jst.erb new file mode 100644 index 0000000..6e68442 --- /dev/null +++ b/templates/view.jst.erb @@ -0,0 +1 @@ + diff --git a/templates/view_spec.js.erb b/templates/view_spec.js.erb new file mode 100644 index 0000000..264f6bd --- /dev/null +++ b/templates/view_spec.js.erb @@ -0,0 +1,10 @@ +describe('<%= object_name %>View', function() { + var view; + + it('should render', function() { + view = new <%= object_name %>View(); + view.render(); + + expect($(view.el)).toContain('.something'); + }); +});