initial commit

This commit is contained in:
John Bintz 2011-04-15 14:55:37 -04:00
commit cfa1fbe2bf
17 changed files with 207 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*

7
Gemfile Normal file
View File

@ -0,0 +1,7 @@
source "http://rubygems.org"
# Specify your gem's dependencies in backbone-generator.gemspec
gemspec
gem 'autotest'

2
Rakefile Normal file
View File

@ -0,0 +1,2 @@
require 'bundler'
Bundler::GemHelper.install_tasks

2
autotest/discover.rb Normal file
View File

@ -0,0 +1,2 @@
Autotest.add_discovery { 'rspec2' }

View File

@ -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

47
bin/backbone-generator Executable file
View File

@ -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

View File

@ -0,0 +1,5 @@
module Backbone
module Generator
# Your code goes here...
end
end

View File

@ -0,0 +1,5 @@
module Backbone
module Generator
VERSION = "0.0.1"
end
end

View File

@ -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

0
spec/spec_helper.rb Normal file
View File

View File

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

View File

@ -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);
});
});

4
templates/model.js.erb Normal file
View File

@ -0,0 +1,4 @@
var <%= object_name %>Model = Backbone.Model.extend({
});

View File

@ -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);
});
});

10
templates/view.js.erb Normal file
View File

@ -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());
}
});

1
templates/view.jst.erb Normal file
View File

@ -0,0 +1 @@
<!-- your <%= object_name %>View template goes here -->

View File

@ -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');
});
});