Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move bundle and helpers into own files

  • Loading branch information...
commit 7198692a75f625ff852fc20fb62d2f635fb69117 1 parent 8d8ee4e
@jnicklas authored
View
2  Manifest.txt
@@ -5,6 +5,8 @@ Rakefile
app/controllers/bistro_car/bundle_controller.rb
config/routes.rb
lib/bistro_car.rb
+lib/bistro_car/bundle.rb
+lib/bistro_car/helpers.rb
script/console
script/destroy
script/generate
View
3  Rakefile
@@ -2,7 +2,8 @@ require 'rubygems'
gem 'hoe', '>= 2.1.0'
require 'hoe'
require 'fileutils'
-require './lib/bistro_car'
+$:.unshift(File.expand_path('./lib', File.dirname(__FILE__)))
+require 'bistro_car'
Hoe.plugin :newgem
View
84 lib/bistro_car.rb
@@ -1,85 +1,7 @@
require 'coffee-script'
+require 'bistro_car/bundle'
+require 'bistro_car/helpers'
module BistroCar
-
VERSION = "0.1"
-
- class Bundle
- attr_reader :name
-
- def initialize(name)
- @name = name.to_sym
- end
-
- def file_paths
- Dir.glob(path.join('*.coffee')).to_a
- end
-
- def render(mode)
- __send__("render_#{mode}")
- end
-
- def to_javascript
- file_paths.map { |path| CoffeeScript.compile(File.read(path)) }.join
- end
-
- private
-
- def javascript_url
- "/javascripts/bundle/#{name}.js"
- end
-
- def render_bundled
- %(<script src="#{javascript_url}" type="text/javascript" charset="utf-8"></script>)
- end
-
- def render_inline
- <<-HTML
- <script type="text/javascript" charset="utf-8">
- //<![CDATA[
- #{to_javascript}
- //]]>
- </script>
- HTML
- end
-
- def path
- if name == :default
- Rails.root.join('app/scripts')
- else
- Rails.root.join('app/scripts', name.to_s)
- end
- end
- end
-
-
- module Helpers
- def coffee_script_bundle(*bundles)
- [:default, *bundles].map { |name| Bundle.new(name).render(:bundled) }.join
- end
-
- def coffee_script(&block)
- input = realign_indentation(capture(&block))
- output = CoffeeScript.compile(input)
-
- concat <<-HTML
- <script type="text/javascript" charset="utf-8">
- //<![CDATA[
- #{output}
- //]]>
- </script>
- HTML
- end
-
- private
-
- def realign_indentation(string)
- lines = string.split(/[\n\r]+/).select(&:present?)
- basis = lines.first.index(/\S/) # find the first non-whitespace character
- return lines.map { |s| s.sub(/^\s{#{basis}}/, '') }.join("\n")
- end
-
- end
-end
-
-ActionView::Base.send(:include, BistroCar::Helpers) if defined?(ActionView::Base)
+end
View
49 lib/bistro_car/bundle.rb
@@ -0,0 +1,49 @@
+module BistroCar
+ class Bundle
+ attr_reader :name
+
+ def initialize(name)
+ @name = name.to_sym
+ end
+
+ def file_paths
+ Dir.glob(path.join('*.coffee')).to_a
+ end
+
+ def render(mode)
+ __send__("render_#{mode}")
+ end
+
+ def to_javascript
+ file_paths.map { |path| CoffeeScript.compile(File.read(path)) }.join
+ end
+
+ private
+
+ def javascript_url
+ "/javascripts/bundle/#{name}.js"
+ end
+
+ def render_bundled
+ %(<script src="#{javascript_url}" type="text/javascript" charset="utf-8"></script>)
+ end
+
+ def render_inline
+ <<-HTML
+ <script type="text/javascript" charset="utf-8">
+ //<![CDATA[
+ #{to_javascript}
+ //]]>
+ </script>
+ HTML
+ end
+
+ def path
+ if name == :default
+ Rails.root.join('app/scripts')
+ else
+ Rails.root.join('app/scripts', name.to_s)
+ end
+ end
+ end
+end
View
33 lib/bistro_car/helpers.rb
@@ -0,0 +1,33 @@
+module BistroCar
+ module Helpers
+ def coffee_script_bundle(*bundles)
+ options = bundles.extract_options!
+ options[:mode] ||= :bundled
+ [:default, *bundles].map { |name| Bundle.new(name).render(options[:mode]) }.join
+ end
+
+ def coffee_script(&block)
+ input = realign_indentation(capture(&block))
+ output = CoffeeScript.compile(input)
+
+ concat <<-HTML
+ <script type="text/javascript" charset="utf-8">
+ //<![CDATA[
+ #{output}
+ //]]>
+ </script>
+ HTML
+ end
+
+ private
+
+ def realign_indentation(string)
+ lines = string.split(/[\n\r]+/).select(&:present?)
+ basis = lines.first.index(/\S/) # find the first non-whitespace character
+ return lines.map { |s| s.sub(/^\s{#{basis}}/, '') }.join("\n")
+ end
+
+ end
+end
+
+ActionView::Base.send(:include, BistroCar::Helpers) if defined?(ActionView::Base)
Please sign in to comment.
Something went wrong with that request. Please try again.