Skip to content

Commit

Permalink
initial commit, still need to figure out a good way to do collections
Browse files Browse the repository at this point in the history
  • Loading branch information
ebertech committed Oct 24, 2012
0 parents commit 8e293e6
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
@@ -0,0 +1,18 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
.rvmrc
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in backbone-bootstrapper.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2012 Andrew Eberbach

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
# Backbone::Bootstrapper

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'backbone-bootstrapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install backbone-bootstrapper

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
21 changes: 21 additions & 0 deletions backbone-bootstrapper.gemspec
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'backbone/bootstrapper/version'

Gem::Specification.new do |gem|
gem.name = "backbone-bootstrapper"
gem.version = Backbone::Bootstrapper::VERSION
gem.authors = ["Andrew Eberbach"]
gem.email = ["andrew@ebertech.ca"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_runtime_dependency "rabl"
end
9 changes: 9 additions & 0 deletions lib/backbone-bootstrapper.rb
@@ -0,0 +1,9 @@
module Backbone
module Bootstrapper
autoload :Base, "backbone/bootstrapper/base"
autoload :Collection, "backbone/bootstrapper/collection"
autoload :Model, "backbone/bootstrapper/model"
autoload :Helper, "backbone/bootstrapper/helper"
autoload :VERSION, "backbone/bootstrapper/version"
end
end
22 changes: 22 additions & 0 deletions lib/backbone/bootstrapper/base.rb
@@ -0,0 +1,22 @@
module Backbone
module Bootstrapper
class Base
def initialize(key, object, template, type = nil)
@key = key
@object = object
@template = template
@type = type

@value = render
end

def declaration
%Q{#{@key} = new #{@type};}
end

def render
raise NotImplementedError
end
end
end
end
20 changes: 20 additions & 0 deletions lib/backbone/bootstrapper/collection.rb
@@ -0,0 +1,20 @@
module Backbone
module Bootstrapper
class Collection < Base
def initialize(*args)
super
@type ||= "Backbone.Collection"
end

def initialization
%Q{#{@key}.reset(#{@value});}
end

def render
@object.map do |item|
Rabl::Renderer.json(object, template, :view_path => 'app/views', :object => object)
end.join("\n")
end
end
end
end
33 changes: 33 additions & 0 deletions lib/backbone/bootstrapper/helper.rb
@@ -0,0 +1,33 @@
module Backbone
module Bootstrapper
module Helper
class << self
def included(base)
base.class_eval do
include InstanceMethods
helper_method :backbone_bootstraps
end
end
end

module InstanceMethods
def backbone_bootstraps
@template.javascript_tag do
(@bootstrappers || []).map do |bootstrapper|
[bootstrapper.declaration, bootstrapper.initialization].join("\n")
end.join("\n")
end
end

def bootstrap_backbone(key, object, template, type = nil)
@bootstrappers ||= []
@bootstrappers << if object.is_a?(Array)
Bootstrapper::Collection.new(key, object, template, type)
else
Bootstrapper::Model.new(key, object, template, type)
end
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/backbone/bootstrapper/model.rb
@@ -0,0 +1,18 @@
module Backbone
module Bootstrapper
class Model < Base
def initialize(*args)
super
@type ||= "Backbone.Model"
end

def initialization
%Q{#{@key}.set(#{@value});}
end

def render
Rabl::Renderer.json(@object, @template, :view_path => 'app/views', :object => @object)
end
end
end
end
5 changes: 5 additions & 0 deletions lib/backbone/bootstrapper/version.rb
@@ -0,0 +1,5 @@
module Backbone
module Bootstrapper
VERSION = "1.0.0"
end
end

0 comments on commit 8e293e6

Please sign in to comment.