strawberry_cough 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ .bundle
3
+ pkg/*
4
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'rake', '~> 0.8.7'
6
+
7
+ gem 'activesupport', '~> 3.0.0'
8
+ gem 'railties', '~> 3.0.0'
9
+
10
+ group :test do
11
+ gem 'rspec', '~> 2.0.0.beta.22'
12
+ gem 'therubyracer', '~> 0.7.5'
13
+ gem 'autotest', '~> 4.3.2'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ strawberry_cough (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionpack (3.0.0)
11
+ activemodel (= 3.0.0)
12
+ activesupport (= 3.0.0)
13
+ builder (~> 2.1.2)
14
+ erubis (~> 2.6.6)
15
+ i18n (~> 0.4.1)
16
+ rack (~> 1.2.1)
17
+ rack-mount (~> 0.6.12)
18
+ rack-test (~> 0.5.4)
19
+ tzinfo (~> 0.3.23)
20
+ activemodel (3.0.0)
21
+ activesupport (= 3.0.0)
22
+ builder (~> 2.1.2)
23
+ i18n (~> 0.4.1)
24
+ activesupport (3.0.0)
25
+ autotest (4.3.2)
26
+ builder (2.1.2)
27
+ diff-lcs (1.1.2)
28
+ erubis (2.6.6)
29
+ abstract (>= 1.0.0)
30
+ i18n (0.4.1)
31
+ rack (1.2.1)
32
+ rack-mount (0.6.13)
33
+ rack (>= 1.0.0)
34
+ rack-test (0.5.5)
35
+ rack (>= 1.0)
36
+ railties (3.0.0)
37
+ actionpack (= 3.0.0)
38
+ activesupport (= 3.0.0)
39
+ rake (>= 0.8.4)
40
+ thor (~> 0.14.0)
41
+ rake (0.8.7)
42
+ rspec (2.0.0.beta.22)
43
+ rspec-core (= 2.0.0.beta.22)
44
+ rspec-expectations (= 2.0.0.beta.22)
45
+ rspec-mocks (= 2.0.0.beta.22)
46
+ rspec-core (2.0.0.beta.22)
47
+ rspec-expectations (2.0.0.beta.22)
48
+ diff-lcs (>= 1.1.2)
49
+ rspec-mocks (2.0.0.beta.22)
50
+ rspec-core (= 2.0.0.beta.22)
51
+ rspec-expectations (= 2.0.0.beta.22)
52
+ therubyracer (0.7.5)
53
+ thor (0.14.2)
54
+ tzinfo (0.3.23)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ activesupport (~> 3.0.0)
61
+ autotest (~> 4.3.2)
62
+ bundler (>= 1.0.0)
63
+ railties (~> 3.0.0)
64
+ rake (~> 0.8.7)
65
+ rspec (~> 2.0.0.beta.22)
66
+ strawberry_cough!
67
+ therubyracer (~> 0.7.5)
data/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ License
2
+ -------
3
+
4
+ Copyright 2010, Three Wise Men Inc. All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THREE WISE MEN, INC "AS IS" AND ANY EXPRESS OR
17
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
+ EVENT SHALL THREE WISE MEN, INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ The views and conclusions contained in the software and documentation are
28
+ those of the authors and should not be interpreted as representing official
29
+ policies, either expressed or implied, of Three Wise Men Inc.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ Strawberry Cough
2
+ ================
3
+
4
+ Strawberry Cough lets you access your Rails 3 routes in your JavaScripts.
5
+
6
+ Example
7
+ -------
8
+
9
+ # In your routes.rb
10
+
11
+ resources :widgets
12
+ root :to => "widgets#index"
13
+
14
+ With Strawberry Cough, you get access to URL helpers in JavaScript via the
15
+ `Routes` object.
16
+
17
+ // In any JavaScript file
18
+
19
+ Routes.widgetsPath(); // "/widgets"
20
+ Routes.newWidgetPath(); // "/widgets/new"
21
+ Routes.newWidgetPath("xml"); // "/widgets/new.xml"
22
+ Routes.rootPath(); // "/"
23
+
24
+ Installation
25
+ ------------
26
+
27
+ To install, just add the following line to your Gemfile.
28
+
29
+ gem 'strawberry_cough'
30
+
31
+ Then include the `routes.js` JavaScript file before any script that uses it in your layout/views.
32
+
33
+ Notes
34
+ -----
35
+
36
+ Right now, Strawberry Cough is **Rails 3 only**.
37
+
38
+ A file `public/javascripts/routes.js` will be generated on each request to your app in the development environment, and on the first request only in production.
39
+
40
+ It uses the `to_prepare` hook in Railties (refer to [Rails docs][1]).
41
+
42
+ Authors
43
+ -------
44
+
45
+ * Craig Savolainen ([maedhr](http://github.com/maedhr))
46
+ * Nate Smith ([nwjsmith](http://github.com/nwjsmith))
47
+
48
+ License
49
+ -------
50
+
51
+ Copyright 2010, Three Wise Men Inc. All rights reserved.
52
+
53
+ Redistribution and use in source and binary forms, with or without
54
+ modification, are permitted provided that the following conditions are met:
55
+
56
+ 1. Redistributions of source code must retain the above copyright notice,
57
+ this list of conditions and the following disclaimer.
58
+
59
+ 2. Redistributions in binary form must reproduce the above copyright
60
+ notice, this list of conditions and the following disclaimer in the
61
+ documentation and/or other materials provided with the distribution.
62
+
63
+ THIS SOFTWARE IS PROVIDED BY THREE WISE MEN, INC "AS IS" AND ANY EXPRESS OR
64
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
65
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
66
+ EVENT SHALL THREE WISE MEN, INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
67
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
68
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
69
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
70
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
72
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73
+
74
+ The views and conclusions contained in the software and documentation are
75
+ those of the authors and should not be interpreted as representing official
76
+ policies, either expressed or implied, of Three Wise Men Inc.
77
+
78
+ [1]: http://api.rubyonrails.org/classes/Rails/Railtie.html "Railties docs"
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ Make output file configurable.
2
+ Make JS Routes object name configurable.
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,5 @@
1
+ require 'strawberry_cough/version'
2
+ require 'strawberry_cough/path_compiler'
3
+ require 'strawberry_cough/path_parser'
4
+ require 'strawberry_cough/routes_compiler'
5
+ require 'strawberry_cough/railtie'
@@ -0,0 +1,34 @@
1
+ module StrawberryCough
2
+
3
+ class PathCompiler
4
+
5
+ def self.compile(path)
6
+ params = PathParser.parse_params(path)
7
+ anchors = PathParser.parse_anchors(path)
8
+ js = <<-COMPILED
9
+ function (#{args_list(params)}) {
10
+ var url = #{url_concatenation(anchors, params)};
11
+ if (format === undefined) {
12
+ return url;
13
+ } else {
14
+ return url + "." + format;
15
+ }
16
+ }
17
+ COMPILED
18
+ js.strip
19
+ end
20
+
21
+ private
22
+ def self.url_concatenation(anchors, params)
23
+ params_without_format = params.reject { |p| p == "format" }
24
+ quoted_anchors = anchors.map { |a| "\"#{a}\"" }
25
+ anchors_paired_with_params = quoted_anchors.zip(params_without_format)
26
+ anchors_paired_with_params.flatten.compact.join(" + ")
27
+ end
28
+
29
+ def self.args_list(params)
30
+ params.join(", ")
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,18 @@
1
+ module StrawberryCough
2
+
3
+ class PathParser
4
+
5
+ PARAM_PATTERN = /:([\w_\d]+)\(*/
6
+ SPLIT_ME = "{}{}}{}{}}{}"
7
+
8
+ def self.parse_params(path)
9
+ path.scan(PARAM_PATTERN).flatten
10
+ end
11
+
12
+ def self.parse_anchors(path)
13
+ path.gsub(/\(.*\)/, '').gsub(PARAM_PATTERN, SPLIT_ME).split(SPLIT_ME)
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,19 @@
1
+ require 'strawberry_cough/routes_compiler'
2
+ require 'rails/railtie'
3
+
4
+ module StrawberryCough
5
+
6
+ class Railtie < Rails::Railtie
7
+
8
+ config.to_prepare do
9
+ app = Rails.application
10
+ routes = app.routes.routes
11
+ file = File.join(app.root, "public", "javascripts", "routes.js")
12
+ File.open(file, "w") do |io|
13
+ RoutesCompiler.compile_to_io(routes, io)
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/inflector'
2
+
3
+ module StrawberryCough
4
+
5
+ class RoutesCompiler
6
+
7
+ def self.compile(route_set)
8
+ functions = route_set.inject(Set.new) do |memo, route|
9
+ name = route.name.camelize(:lower) + "Path"
10
+ memo << "#{name} : " + PathCompiler.compile(route.path)
11
+ end.to_a.join(',')
12
+ <<-COMPILED
13
+ var Routes = {
14
+ #{functions}
15
+ };
16
+ COMPILED
17
+ end
18
+
19
+ def self.compile_to_io(route_set, io)
20
+ io << compile(route_set)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module StrawberryCough
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe StrawberryCough::PathCompiler do
4
+ let(:compiler) { StrawberryCough::PathCompiler }
5
+ let(:interpreter) { V8::Context.new }
6
+
7
+ it "compiles a resource path into a JavaScript function that returns that path" do
8
+ resource_path = "/parent/:id/edit(.:format)"
9
+ path_function = compiler.compile(resource_path)
10
+ interpreter.eval("var path = #{path_function};")
11
+ interpreter.eval("path(1, 'xml')").should == '/parent/1/edit.xml'
12
+ end
13
+
14
+ it "compiles the root path to a JS function that returns the root path" do
15
+ root_path = "/(.:format)"
16
+ path_function = compiler.compile(root_path)
17
+ interpreter.eval("var rootPath = #{path_function};")
18
+ interpreter.eval("rootPath('xml')").should == '/.xml'
19
+ end
20
+
21
+ it "compiles a path without parameters to a JS function that returns that path" do
22
+ simple_path = "/parent/new(.:format)"
23
+ path_function = compiler.compile(simple_path)
24
+ interpreter.eval("var simplePath = #{path_function};")
25
+ interpreter.eval("simplePath('xml')").should == '/parent/new.xml'
26
+ end
27
+
28
+ it "compiles a path to a function that optionally takes a format" do
29
+ path_with_format = "/parent/new(.:format)"
30
+ path_function = compiler.compile(path_with_format)
31
+ interpreter.eval("var path = #{path_function}")
32
+ interpreter.eval("path()").should == "/parent/new"
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe StrawberryCough::PathParser do
4
+ let(:parser) { StrawberryCough::PathParser }
5
+ let(:simple_resource_path) { "/parent/:id/edit(.:format)" }
6
+ let(:nested_resource_path) { "/parent/:parent_id/child/:id/edit(.:format)" }
7
+
8
+ describe ".parse_params" do
9
+ it "parses parameters from simple resource path" do
10
+ parser.parse_params(simple_resource_path).should == %w{id format}
11
+ end
12
+
13
+ it "parses parameters from nested resource path" do
14
+ parser.parse_params(nested_resource_path).should == %w{parent_id id format}
15
+ end
16
+ end
17
+
18
+ describe ".parse_anchors" do
19
+ it "parses anchors from a simple resource path" do
20
+ parser.parse_anchors(simple_resource_path).should == %w{/parent/ /edit}
21
+ end
22
+
23
+ it "parses anchors from a nested resource path" do
24
+ parser.parse_anchors(nested_resource_path).should == %w{/parent/ /child/ /edit}
25
+ end
26
+
27
+ it "combines anchors not followed by a parameter" do
28
+ parser.parse_anchors("/parent/new").should == %w{/parent/new}
29
+ end
30
+
31
+ it 'should parse root anchor' do
32
+ parser.parse_anchors("/").should == %w{/}
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe StrawberryCough::RoutesCompiler do
4
+ let(:compiler) { StrawberryCough::RoutesCompiler }
5
+ let(:interpreter) { V8::Context.new }
6
+
7
+ it "makes an empty JavaScript object when given an empty route set" do
8
+ routes_object = compiler.compile([])
9
+ interpreter.eval(routes_object)
10
+ interpreter.eval("Routes.rootPath === undefined").should be_true
11
+ end
12
+
13
+ it "makes a Routes JavaScript object when given an array of routes" do
14
+ route = double('route')
15
+ route.stub(:path) { "/parent/:id/edit(.:format)" }
16
+ route.stub(:name) { "edit_parent" }
17
+ routes_object = compiler.compile([route])
18
+ interpreter.eval(routes_object)
19
+ interpreter.eval("typeof Routes.editParentPath === 'function'").should be_true
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ lib_dir = File.dirname(__FILE__) + '/../lib'
2
+ $:.unshift lib_dir unless $:.include?(lib_dir)
3
+
4
+ require 'rspec'
5
+ require 'v8'
6
+
7
+ require 'strawberry_cough'
8
+
9
+ alias :context :describe
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/strawberry_cough/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "strawberry_cough"
6
+ s.version = StrawberryCough::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Craig Savolainen", "Nate Smith"]
9
+ s.email = ["support@threewisemen.ca"]
10
+ s.homepage = "http://github.com/ThreeWiseMen/strawberry_cough"
11
+ s.summary = "Strawberry Cough lets you access your Rails 3 routes in your JavaScripts."
12
+
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.rubyforge_project = "strawberry_cough"
15
+
16
+ s.add_development_dependency "bundler", ">= 1.0.0"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.require_path = 'lib'
20
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strawberry_cough
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Craig Savolainen
13
+ - Nate Smith
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-25 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *id001
36
+ description:
37
+ email:
38
+ - support@threewisemen.ca
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - .rspec
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - TODO
54
+ - autotest/discover.rb
55
+ - lib/strawberry_cough.rb
56
+ - lib/strawberry_cough/path_compiler.rb
57
+ - lib/strawberry_cough/path_parser.rb
58
+ - lib/strawberry_cough/railtie.rb
59
+ - lib/strawberry_cough/routes_compiler.rb
60
+ - lib/strawberry_cough/version.rb
61
+ - spec/path_compiler_spec.rb
62
+ - spec/path_parser_spec.rb
63
+ - spec/routes_compiler_spec.rb
64
+ - spec/spec_helper.rb
65
+ - strawberry_cough.gemspec
66
+ has_rdoc: true
67
+ homepage: http://github.com/ThreeWiseMen/strawberry_cough
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: -3654356472966179208
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 1
91
+ - 3
92
+ - 6
93
+ version: 1.3.6
94
+ requirements: []
95
+
96
+ rubyforge_project: strawberry_cough
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Strawberry Cough lets you access your Rails 3 routes in your JavaScripts.
101
+ test_files: []
102
+