Skip to content

Commit

Permalink
code cleanup and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Jul 9, 2011
1 parent 06d8c7f commit 84ecf28
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,2 +1,4 @@
*.swp
*.gem
*.gem
.bundle
Gemfile.lock
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source :rubygems

gemspec
5 changes: 4 additions & 1 deletion coffee-views.gemspec
Expand Up @@ -13,8 +13,11 @@ Gem::Specification.new do |s|
s.rdoc_options = %w(--charset=UTF-8)

s.files = `git ls-files`.split("\n")
s.require_path = 'lib'

s.add_runtime_dependency('coffee-script')
s.add_runtime_dependency('actionpack')
s.add_runtime_dependency('actionpack')

s.add_development_dependency("rspec", ["~> 2.6"])

end
22 changes: 14 additions & 8 deletions lib/coffee_views.rb
@@ -1,3 +1,4 @@
require 'action_view'
require 'coffee-script'

module CoffeeViews
Expand All @@ -6,17 +7,22 @@ module CoffeeScript
def self.erb_handler
@@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
end

def self.prepare source
source ||= ""
source.gsub! /<%==(.*?)%>/, '`<%==\1%>`'
source.gsub! /<%=([^=].*?)%>/, '`<%==(\1).to_json%>`'
source.gsub! /<%([^=].*?)%>/, '`<%\1%>`'
source.gsub! /#\{==(.*?)\}/, '#{`<%==\1%>`}'
source.gsub! /#\{=([^=].*?)\}/, '#{`<%==(\1).to_json%>`}'
source
end

def self.call(template)
src = template.source
src = src.gsub(/<%=([^=].*?)%>/, '`<%==(\1).to_json%>`')
src = src.gsub(/<%([^=].*?)%>/, '`<%\1%>`')
src = src.gsub(/[^`]<%==(.*?)%>/, '`<%==\1%>`')
src = src.gsub(/#\{=(.*?)\}/, '#{`<%=(\1).to_json%>`}')
src = src.gsub(/#\{==(.*?)\}/, '#{`<%== \1 %>`}')

source = self.prepare(template.source)
source = ::CoffeeScript.compile(source)
# TODO: find how to set source back to template without instance_variable_set
template.instance_variable_set :@source, src
template.instance_variable_set :@source, source

erb_handler.call(template)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/prepare_spec.rb
@@ -0,0 +1,38 @@
require 'spec_helper'

describe CoffeeViews::Handlers::CoffeeScript do

def prepare source
subject.prepare(source)
end

describe "Without substitutions" do
it "should work with empty source" do
prepare('').should == ''
prepare(nil).should == ''
end

it "should prepare sources" do
prepare("x").should == "x"
end
end

describe "<%= code %>" do
it "should wrap with `` and call #to_json on code" do
prepare("<%=x%>").should == "`<%==(x).to_json%>`"
end
end

describe "<%%>" do
it "should wrap with ``" do
prepare("<%x%>").should == "`<%x%>`"
end
end

describe "<%==%>" do
it "should wrap with ``" do
prepare("<%==x%>").should == "`<%==x%>`"
end
end

end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,4 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))

require 'coffee-views'

0 comments on commit 84ecf28

Please sign in to comment.