bistro_car 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 0.1 2010-01-03
2
+
3
+ First release
data/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ app/controllers/bistro_car/bundle_controller.rb
6
+ config/routes.rb
7
+ lib/bistro_car.rb
8
+ lib/bistro_car/bundle.rb
9
+ lib/bistro_car/helpers.rb
10
+ script/console
11
+ script/destroy
12
+ script/generate
data/README.rdoc ADDED
@@ -0,0 +1,76 @@
1
+ = BistroCar
2
+
3
+ * http://github.com/jnicklas/bistro_car
4
+
5
+ == Description:
6
+
7
+ BistroCar serves up {CoffeeScript}[http://jashkenas.github.com/coffee-script/]
8
+ from within your Rails application.
9
+
10
+ == Install:
11
+
12
+ Add it as a gem dependency to you Rails application:
13
+
14
+ config.gem "bistro_car"
15
+
16
+ == Usage:
17
+
18
+ Add your CoffeeScript files to <tt>app/scripts</tt>. Then in your layout or wherever else suits you, add:
19
+
20
+ <%= coffee_script_bundle %>
21
+
22
+ This will insert automatically link to your JavaScriptified CoffeeScript files. You might find it useful, for debugging purposes, to render your CoffeeScript inline, instead of linking to external scripts, you can do that with:
23
+
24
+ <%= coffee_script_bundle :mode => :inline %>
25
+
26
+ You can set this for the entire application:
27
+
28
+ BistroCar.mode = :inline
29
+
30
+ == Secondary bundles:
31
+
32
+ You're encouraged to create only a single bundle, but sometimes you might find it useful/necessary to create multiple bundles for different parts of your application. In that case you can create a directory structure like this:
33
+
34
+ + app
35
+ + scripts
36
+ + admin
37
+ - admin.coffee
38
+ - other_admin.coffee
39
+ - application.coffee
40
+
41
+ Now you can require both bundles like this:
42
+
43
+ <%= coffee_script_bundle :admin %>
44
+
45
+ == Minification:
46
+
47
+ You can make BistroCar minify your JavaScript bundles using JSMin by setting:
48
+
49
+ BistroCar.minify = true
50
+
51
+ This is done automatically for the production environment.
52
+
53
+ == License:
54
+
55
+ (The MIT License)
56
+
57
+ Copyright (c) 2010 Jonas Nicklas
58
+
59
+ Permission is hereby granted, free of charge, to any person obtaining
60
+ a copy of this software and associated documentation files (the
61
+ 'Software'), to deal in the Software without restriction, including
62
+ without limitation the rights to use, copy, modify, merge, publish,
63
+ distribute, sublicense, and/or sell copies of the Software, and to
64
+ permit persons to whom the Software is furnished to do so, subject to
65
+ the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be
68
+ included in all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
71
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
72
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
73
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
74
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
75
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
76
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ $:.unshift(File.expand_path('./lib', File.dirname(__FILE__)))
6
+ require 'bistro_car'
7
+
8
+ Hoe.plugin :newgem
9
+
10
+ Hoe.spec 'bistro_car' do
11
+ self.developer 'Jonas Nicklas', 'jonas.nicklas@gmail.com'
12
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
13
+ self.rubyforge_name = self.name # TODO this is default value
14
+ self.extra_deps = [['coffee-script','>= 0.1.6']]
15
+ self.extra_deps = [['jsmin','>= 1.0.1']]
16
+ self.version = BistroCar::VERSION
17
+ end
@@ -0,0 +1,8 @@
1
+ class BistroCar::BundleController < ActionController::Base
2
+ caches_page :show
3
+
4
+ def show
5
+ headers['Content-Type'] = "application/javascript"
6
+ render :text => BistroCar::Bundle.new(params[:bundle]).to_javascript
7
+ end
8
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect 'javascripts/bundle/:bundle.js', :controller => 'bistro_car/bundle', :action => 'show'
3
+ end
@@ -0,0 +1,53 @@
1
+ module BistroCar
2
+ class Bundle
3
+ attr_reader :name
4
+
5
+ def initialize(name)
6
+ @name = name.to_sym
7
+ end
8
+
9
+ def file_paths
10
+ Dir.glob(path.join('*.coffee')).to_a
11
+ end
12
+
13
+ def render(mode)
14
+ __send__("render_#{mode}")
15
+ end
16
+
17
+ def to_javascript
18
+ minify(file_paths.map { |path| CoffeeScript.compile(File.read(path)) }.join)
19
+ end
20
+
21
+ private
22
+
23
+ def minify(javascript)
24
+ if BistroCar.minify then JSMin.minify(javascript) else javascript end
25
+ end
26
+
27
+ def javascript_url
28
+ "/javascripts/bundle/#{name}.js"
29
+ end
30
+
31
+ def render_bundled
32
+ %(<script src="#{javascript_url}" type="text/javascript" charset="utf-8"></script>)
33
+ end
34
+
35
+ def render_inline
36
+ <<-HTML
37
+ <script type="text/javascript" charset="utf-8">
38
+ //<![CDATA[
39
+ #{to_javascript}
40
+ //]]>
41
+ </script>
42
+ HTML
43
+ end
44
+
45
+ def path
46
+ if name == :default
47
+ Rails.root.join('app/scripts')
48
+ else
49
+ Rails.root.join('app/scripts', name.to_s)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ module BistroCar
2
+ module Helpers
3
+ def coffee_script_bundle(*bundles)
4
+ options = bundles.extract_options!
5
+ options[:mode] ||= BistroCar.mode
6
+ [:default, *bundles].map { |name| Bundle.new(name).render(options[:mode]) }.join
7
+ end
8
+
9
+ def coffee_script(&block)
10
+ input = realign_indentation(capture(&block))
11
+ output = CoffeeScript.compile(input)
12
+
13
+ concat <<-HTML
14
+ <script type="text/javascript" charset="utf-8">
15
+ //<![CDATA[
16
+ #{output}
17
+ //]]>
18
+ </script>
19
+ HTML
20
+ end
21
+
22
+ private
23
+
24
+ def realign_indentation(string)
25
+ lines = string.split(/[\n\r]+/).select(&:present?)
26
+ basis = lines.first.index(/\S/) # find the first non-whitespace character
27
+ return lines.map { |s| s.sub(/^\s{#{basis}}/, '') }.join("\n")
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ ActionView::Base.send(:include, BistroCar::Helpers) if defined?(ActionView::Base)
data/lib/bistro_car.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'coffee-script'
2
+ require 'jsmin'
3
+ require 'bistro_car/bundle'
4
+ require 'bistro_car/helpers'
5
+
6
+ module BistroCar
7
+ VERSION = "0.1"
8
+
9
+ class << self
10
+ attr_accessor :mode, :minify
11
+ end
12
+ end
13
+
14
+ BistroCar.mode = :bundled
15
+ BistroCar.minify = true if defined?(Rails) and Rails.env.production?
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/bistro_car.rb'}"
9
+ puts "Loading bistro_car gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bistro_car
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Nicklas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-03 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jsmin
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.4.0
34
+ version:
35
+ description: |-
36
+ BistroCar serves up {CoffeeScript}[http://jashkenas.github.com/coffee-script/]
37
+ from within your Rails application.
38
+ email:
39
+ - jonas.nicklas@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ files:
48
+ - History.txt
49
+ - Manifest.txt
50
+ - README.rdoc
51
+ - Rakefile
52
+ - app/controllers/bistro_car/bundle_controller.rb
53
+ - config/routes.rb
54
+ - lib/bistro_car.rb
55
+ - lib/bistro_car/bundle.rb
56
+ - lib/bistro_car/helpers.rb
57
+ - script/console
58
+ - script/destroy
59
+ - script/generate
60
+ has_rdoc: true
61
+ homepage: http://github.com/jnicklas/bistro_car
62
+ licenses: []
63
+
64
+ post_install_message: PostInstall.txt
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: bistro_car
85
+ rubygems_version: 1.3.5
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: BistroCar serves up {CoffeeScript}[http://jashkenas.github.com/coffee-script/] from within your Rails application.
89
+ test_files: []
90
+