coffee-views 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md CHANGED
@@ -1,8 +1,21 @@
1
1
  CoffeeViews
2
2
  =================
3
+ Inspired by [coffeebeans](https://github.com/markbates/coffeebeans) but with fixes:
4
+
5
+ 1. Doesn't compile coffeescript everytime in production
6
+ 2. Smart replacements for `<%=%>`
7
+
8
+ Use #to_json in safe mode
9
+
10
+ <%= x %> # replaced with `<%== (x).to_json %>`
11
+
12
+ Use raw mode in raw mode
13
+
14
+ <%== x %> # replaced with `<%== x %>`
3
15
 
4
16
  Installation
5
17
  ------------
18
+ In your Gemfile:
6
19
 
7
20
  gem "coffee-views"
8
21
 
data/coffee-views.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'coffee-views'
5
- s.version = '0.1.0'
5
+ s.version = '0.1.1'
6
6
  s.date = Date.today.to_s
7
7
  s.authors = ['Yury Korolev']
8
8
  s.email = 'yury.korolev@gmail.com'
data/lib/coffee_views.rb CHANGED
@@ -8,18 +8,23 @@ module CoffeeViews
8
8
  @@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
9
9
  end
10
10
 
11
- def self.prepare source
12
- source.gsub!(/<%==(.*?)%>/, '`<%==\1%>`')
13
- source.gsub!(/<%=([^=].*?)%>/, '`<%==(\1).to_json%>`')
14
- source.gsub!(/<%([^=].*?)%>/, '`<%\1%>`')
15
- source.gsub!(/#\{==(.*?)\}/, '#{`<%== \1 %>`}')
16
- source.gsub!(/#\{=([^=].*?)\}/, '#{`<%=(\1).to_json%>`}')
11
+ def self.preprocess source
12
+ source ||= ""
13
+ source.gsub! /<%==(.*?)%>/, '`<%==\1%>`'
14
+ source.gsub! /<%=([^=].*?)%>/, '`<%==(\1).to_json%>`'
15
+ source.gsub! /<%([^=].*?)%>/, '`<%\1%>`'
16
+ source.gsub! /#\{==(.*?)\}/, '#{`<%==\1%>`}'
17
+ source.gsub! /#\{=([^=].*?)\}/, '#{`<%==(\1).to_json%>`}'
17
18
  source
18
19
  end
20
+
21
+ def self.compile_coffee(source)
22
+ source = self.preprocess(source)
23
+ ::CoffeeScript.compile(source)
24
+ end
19
25
 
20
26
  def self.call(template)
21
- source = self.prepare(template.source)
22
- source = ::CoffeeScript.compile(source)
27
+ source = self.compile_coffee(template.source)
23
28
  # TODO: find how to set source back to template without instance_variable_set
24
29
  template.instance_variable_set :@source, source
25
30
 
@@ -29,4 +34,14 @@ module CoffeeViews
29
34
  end
30
35
 
31
36
  ActionView::Template.register_template_handler :coffee, CoffeeViews::Handlers::CoffeeScript
37
+
38
+ if defined?(Slim)
39
+ class CoffeeViewEngine < Slim::EmbeddedEngine::ERBEngine
40
+ def on_slim_embedded(engine, body)
41
+ source = CoffeeViews::Handlers::CoffeeScript.compile_coffee(collect_text(body))
42
+ source = Temple::ERB::Parser.new.call(source)
43
+ end
44
+ end
45
+ Slim::EmbeddedEngine.register :coffeeview, Slim::EmbeddedEngine::TagEngine, :tag => :script, :attributes => { :type => 'text/javascript' }, :engine => CoffeeViewEngine
46
+ end
32
47
  end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe CoffeeViews::Handlers::CoffeeScript do
4
+
5
+ def preprocess source
6
+ subject.preprocess(source)
7
+ end
8
+
9
+ describe "Without substitutions" do
10
+ it "should work with empty source" do
11
+ preprocess('').should == ''
12
+ preprocess(nil).should == ''
13
+ end
14
+
15
+ it "should preprocess sources" do
16
+ preprocess("x").should == "x"
17
+ end
18
+ end
19
+
20
+ describe "<%= code %>" do
21
+ it "should wrap with `` and call #to_json on code" do
22
+ preprocess("<%=x%>").should == "`<%==(x).to_json%>`"
23
+ end
24
+ end
25
+
26
+ describe "<%%>" do
27
+ it "should wrap with ``" do
28
+ preprocess("<%x%>").should == "`<%x%>`"
29
+ end
30
+ end
31
+
32
+ describe "<%==%>" do
33
+ it "should wrap with ``" do
34
+ preprocess("<%==x%>").should == "`<%==x%>`"
35
+ end
36
+ end
37
+
38
+ describe '#{= code}' do
39
+ it "should wrap with `<%== %>` and call #to_json on code" do
40
+ preprocess('alert "#{=code}"').should == 'alert "#{`<%==(code).to_json%>`}"'
41
+ end
42
+ end
43
+
44
+ describe '#{== code}' do
45
+ it "should wrap with `<%== %>`" do
46
+ preprocess('alert "#{==code}"').should == 'alert "#{`<%==code%>`}"'
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require 'coffee-views'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coffee-views
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-09 00:00:00.000000000Z
12
+ date: 2011-07-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: coffee-script
16
- requirement: &2157017340 !ruby/object:Gem::Requirement
16
+ requirement: &2156165280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2157017340
24
+ version_requirements: *2156165280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: actionpack
27
- requirement: &2156974080 !ruby/object:Gem::Requirement
27
+ requirement: &2156164660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2156974080
35
+ version_requirements: *2156164660
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2156973440 !ruby/object:Gem::Requirement
38
+ requirement: &2156160320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '2.6'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2156973440
46
+ version_requirements: *2156160320
47
47
  description: Allows you to create .js.coffee views
48
48
  email: yury.korolev@gmail.com
49
49
  executables: []
@@ -52,11 +52,14 @@ extra_rdoc_files:
52
52
  - README.md
53
53
  files:
54
54
  - .gitignore
55
+ - Gemfile
55
56
  - LICENSE
56
57
  - README.md
57
58
  - coffee-views.gemspec
58
59
  - lib/coffee-views.rb
59
60
  - lib/coffee_views.rb
61
+ - spec/preprocess_spec.rb
62
+ - spec/spec_helper.rb
60
63
  homepage: http://github.com/yury/coffee-views
61
64
  licenses: []
62
65
  post_install_message: