strawberry_cough 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,8 @@
2
2
  .bundle
3
3
  pkg/*
4
4
  *.gem
5
+ tmp
6
+ measurements
7
+ doc
8
+ .yardoc
9
+
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 1.9.3@strawberry_cough
data/Gemfile CHANGED
@@ -1,14 +1,15 @@
1
- source :rubygems
1
+ source 'http://rubygems.org/'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake', '~> 0.8.7'
5
+ gem 'rake'
6
6
 
7
- gem 'activesupport', '~> 3.0.0'
8
- gem 'railties', '~> 3.0.0'
7
+ gem 'activesupport', '~> 3.1'
8
+ gem 'railties', '~> 3.1'
9
+ gem 'tilt'
10
+ gem 'sprockets-rails'
9
11
 
10
12
  group :test do
11
- gem 'rspec', '~> 2.0.0.beta.22'
12
- gem 'therubyracer', '~> 0.7.5'
13
- gem 'autotest', '~> 4.3.2'
13
+ gem 'rspec'
14
+ gem 'therubyracer'
14
15
  end
data/Gemfile.lock CHANGED
@@ -1,67 +1,82 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strawberry_cough (0.1.0)
4
+ strawberry_cough (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
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)
9
+ actionpack (3.1.1)
10
+ activemodel (= 3.1.1)
11
+ activesupport (= 3.1.1)
12
+ builder (~> 3.0.0)
13
+ erubis (~> 2.7.0)
14
+ i18n (~> 0.6)
15
+ rack (~> 1.3.2)
16
+ rack-cache (~> 1.1)
17
+ rack-mount (~> 0.8.2)
18
+ rack-test (~> 0.6.1)
19
+ sprockets (~> 2.0.2)
20
+ activemodel (3.1.1)
21
+ activesupport (= 3.1.1)
22
+ builder (~> 3.0.0)
23
+ i18n (~> 0.6)
24
+ activesupport (3.1.1)
25
+ multi_json (~> 1.0)
26
+ builder (3.0.0)
27
+ diff-lcs (1.1.3)
28
+ erubis (2.7.0)
29
+ hike (1.2.1)
30
+ i18n (0.6.0)
31
+ json (1.6.1)
32
+ multi_json (1.0.3)
33
+ rack (1.3.5)
34
+ rack-cache (1.1)
35
+ rack (>= 0.4)
36
+ rack-mount (0.8.3)
33
37
  rack (>= 1.0.0)
34
- rack-test (0.5.5)
38
+ rack-ssl (1.3.2)
39
+ rack
40
+ rack-test (0.6.1)
35
41
  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)
42
+ railties (3.1.1)
43
+ actionpack (= 3.1.1)
44
+ activesupport (= 3.1.1)
45
+ rack-ssl (~> 1.3.2)
46
+ rake (>= 0.8.7)
47
+ rdoc (~> 3.4)
48
+ thor (~> 0.14.6)
49
+ rake (0.9.2.2)
50
+ rdoc (3.11)
51
+ json (~> 1.4)
52
+ rspec (2.7.0)
53
+ rspec-core (~> 2.7.0)
54
+ rspec-expectations (~> 2.7.0)
55
+ rspec-mocks (~> 2.7.0)
56
+ rspec-core (2.7.1)
57
+ rspec-expectations (2.7.0)
58
+ diff-lcs (~> 1.1.2)
59
+ rspec-mocks (2.7.0)
60
+ sprockets (2.0.3)
61
+ hike (~> 1.2)
62
+ rack (~> 1.0)
63
+ tilt (~> 1.1, != 1.3.0)
64
+ sprockets-rails (0.0.1)
65
+ sprockets (>= 1.0.2)
66
+ therubyracer (0.8.1)
67
+ thor (0.14.6)
68
+ tilt (1.3.3)
55
69
 
56
70
  PLATFORMS
57
71
  ruby
58
72
 
59
73
  DEPENDENCIES
60
- activesupport (~> 3.0.0)
61
- autotest (~> 4.3.2)
74
+ activesupport (~> 3.1)
62
75
  bundler (>= 1.0.0)
63
- railties (~> 3.0.0)
64
- rake (~> 0.8.7)
65
- rspec (~> 2.0.0.beta.22)
76
+ railties (~> 3.1)
77
+ rake
78
+ rspec
79
+ sprockets-rails
66
80
  strawberry_cough!
67
- therubyracer (~> 0.7.5)
81
+ therubyracer
82
+ tilt
data/README.md CHANGED
@@ -28,16 +28,18 @@ To install, just add the following line to your Gemfile.
28
28
 
29
29
  gem 'strawberry_cough'
30
30
 
31
- Then include the `routes.js` JavaScript file before any script that uses it in your layout/views.
31
+ Then `//= require routes` in the JavaScript manifest file.
32
+
33
+ If you want a differently named JavaScript file add
34
+ `somefile.js.strawberry_cough` to your javascript assets.
32
35
 
33
36
  Notes
34
37
  -----
35
38
 
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
+ Right now, Strawberry Cough is **Rails 3.1 only**.
39
40
 
40
- It uses the `to_prepare` hook in Railties (refer to [Rails docs][1]).
41
+ It uses Rails' asset pipeline to generate the route file.
42
+ In development/test environments add config.assets.compile=true.
41
43
 
42
44
  Authors
43
45
  -------
@@ -48,7 +50,7 @@ Authors
48
50
  License
49
51
  -------
50
52
 
51
- Copyright 2010, Three Wise Men Inc. All rights reserved.
53
+ Copyright 2011, Three Wise Men Inc. All rights reserved.
52
54
 
53
55
  Redistribution and use in source and binary forms, with or without
54
56
  modification, are permitted provided that the following conditions are met:
@@ -75,4 +77,4 @@ The views and conclusions contained in the software and documentation are
75
77
  those of the authors and should not be interpreted as representing official
76
78
  policies, either expressed or implied, of Three Wise Men Inc.
77
79
 
78
- [1]: http://api.rubyonrails.org/classes/Rails/Railtie.html "Railties docs"
80
+ [1]: http://api.rubyonrails.org/classes/Rails/Railtie.html "Railties docs"
data/Rakefile CHANGED
@@ -1,8 +1,12 @@
1
+ require 'rake'
2
+
1
3
  require 'rspec/core/rake_task'
2
4
  require 'bundler'
3
5
 
4
6
  Bundler::GemHelper.install_tasks
5
7
 
8
+ FileList['tasks/**/*.rake'].each { |task| import task }
9
+
6
10
  RSpec::Core::RakeTask.new(:spec)
7
11
 
8
12
  task :default => :spec
data/config/flay.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ threshold: 6.0
3
+ total_score: 39.0
4
+
data/config/flog.yml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ threshold: 24.8
3
+
data/config/site.reek ADDED
@@ -0,0 +1,97 @@
1
+ ---
2
+ UncommunicativeParameterName:
3
+ accept: []
4
+ exclude: []
5
+ enabled: true
6
+ reject:
7
+ - !ruby/regexp /^.$/
8
+ - !ruby/regexp /[0-9]$/
9
+ - !ruby/regexp /[A-Z]/
10
+ LargeClass:
11
+ max_methods: 16 # TODO: decrease max_methods to 10-15 or less
12
+ exclude: []
13
+ enabled: true
14
+ max_instance_variables: 3
15
+ UncommunicativeMethodName:
16
+ accept: []
17
+ exclude: []
18
+ enabled: true
19
+ reject:
20
+ - !ruby/regexp /^[a-z]$/
21
+ - !ruby/regexp /[0-9]$/
22
+ - !ruby/regexp /[A-Z]/
23
+ LongParameterList:
24
+ max_params: 2 # TODO: decrease max_params to 2
25
+ exclude: []
26
+ enabled: true
27
+ overrides: {}
28
+ FeatureEnvy:
29
+ exclude: []
30
+ enabled: true
31
+ ClassVariable:
32
+ exclude: []
33
+ enabled: true
34
+ BooleanParameter:
35
+ exclude: []
36
+ enabled: true
37
+ IrresponsibleModule:
38
+ exclude: []
39
+ enabled: true
40
+ UncommunicativeModuleName:
41
+ accept: []
42
+ exclude: []
43
+ enabled: true
44
+ reject:
45
+ - !ruby/regexp /^.$/
46
+ - !ruby/regexp /[0-9]$/
47
+ NestedIterators:
48
+ ignore_iterators: []
49
+ exclude: []
50
+ enabled: true
51
+ max_allowed_nesting: 1
52
+ LongMethod:
53
+ max_statements: 7 # TODO: decrease max_statements to 5 or less
54
+ exclude: []
55
+ enabled: true
56
+ Duplication:
57
+ allow_calls: []
58
+ exclude: []
59
+ enabled: true
60
+ max_calls: 1
61
+ UtilityFunction:
62
+ max_helper_calls: 1
63
+ exclude: []
64
+ enabled: true
65
+ Attribute:
66
+ exclude: []
67
+ enabled: false
68
+ UncommunicativeVariableName:
69
+ accept: []
70
+ exclude: []
71
+ enabled: true
72
+ reject:
73
+ - !ruby/regexp /^.$/
74
+ - !ruby/regexp /[0-9]$/
75
+ - !ruby/regexp /[A-Z]/
76
+ SimulatedPolymorphism:
77
+ exclude: []
78
+ enabled: true
79
+ max_ifs: 1
80
+ DataClump:
81
+ exclude: [
82
+ # all of these classes have utility class methods that are
83
+ # incorrectly identified as instance methods
84
+ Veritas::Relation::Operation::Binary::ClassMethods,
85
+ Veritas::Relation::Operation::Set::ClassMethods
86
+ ]
87
+ enabled: true
88
+ max_copies: 1
89
+ min_clump_size: 2
90
+ ControlCouple:
91
+ exclude: []
92
+ enabled: true
93
+ LongYieldList:
94
+ max_params: 1
95
+ exclude: []
96
+ enabled: true
97
+
@@ -0,0 +1,3 @@
1
+ ---
2
+ threshold: 100
3
+
File without changes
@@ -1,7 +1,13 @@
1
1
  module StrawberryCough
2
2
 
3
+ # Compiles a path to a JavaScript function
3
4
  class PathCompiler
4
5
 
6
+ # Compile path to JavaScript function
7
+ #
8
+ # @return [String]
9
+ #
10
+ # @api private
5
11
  def self.compile(path)
6
12
  params = PathParser.parse_params(path)
7
13
  anchors = PathParser.parse_anchors(path)
@@ -19,16 +25,27 @@ function (#{args_list(params)}) {
19
25
  end
20
26
 
21
27
  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
28
 
29
- def self.args_list(params)
30
- params.join(", ")
31
- end
32
- end
29
+ # Generate JavaScript to concatenate anchors and params into valid URL
30
+ #
31
+ # @return [String]
32
+ #
33
+ # @api private
34
+ def self.url_concatenation(anchors, params)
35
+ params_without_format = params.reject { |param| param == "format" }
36
+ quoted_anchors = anchors.map { |anchor| "\"#{anchor}\"" }
37
+ quoted_anchors.zip(params_without_format).flatten.compact.join(" + ")
38
+ end
39
+
33
40
 
41
+ # Generate argument listing for JavaScript function
42
+ #
43
+ # @return [String]
44
+ #
45
+ # @api private
46
+ def self.args_list(params)
47
+ params.join(", ")
48
+ end
49
+ end
34
50
  end
51
+
@@ -1,18 +1,30 @@
1
1
  module StrawberryCough
2
2
 
3
+ # Parses a path into anchors and parameters
3
4
  class PathParser
5
+ PARAM_EXTRACTOR_PATTERN = /:([\w_\d]+)\(*/
6
+ ANCHOR_SPLIT_PATTERN = /:[\w_\d]+/
4
7
 
5
- PARAM_PATTERN = /:([\w_\d]+)\(*/
6
- SPLIT_ME = "{}{}}{}{}}{}"
7
-
8
+ # Get list of parameters from a path
9
+ #
10
+ # @return [Array]
11
+ #
12
+ # @api private
8
13
  def self.parse_params(path)
9
- path.scan(PARAM_PATTERN).flatten
14
+ path.scan(PARAM_EXTRACTOR_PATTERN).flatten
10
15
  end
11
16
 
17
+ # Get list of anchors from a path
18
+ #
19
+ # @return [Array]
20
+ #
21
+ # @api private
12
22
  def self.parse_anchors(path)
13
- path.gsub(/\(.*\)/, '').gsub(PARAM_PATTERN, SPLIT_ME).split(SPLIT_ME)
23
+ remove_format(path).split(ANCHOR_SPLIT_PATTERN)
14
24
  end
15
25
 
26
+ def self.remove_format(path)
27
+ path.gsub(/\(.*\)/, '')
28
+ end
16
29
  end
17
-
18
30
  end
@@ -0,0 +1,15 @@
1
+ require 'strawberry_cough/routes_compiler'
2
+ require 'rails/engine'
3
+
4
+ module StrawberryCough
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ config.before_initialize do
8
+ if defined?(Sprockets::Engines)
9
+ Sprockets::Engines
10
+ Sprockets.register_engine '.strawberry_cough', RoutesJSTemplate
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,14 +1,22 @@
1
1
  require 'active_support/inflector'
2
+ require 'active_support/core_ext'
2
3
 
3
4
  module StrawberryCough
4
5
 
6
+ # Compiles a set of routes to a JavaScript Routes object
5
7
  class RoutesCompiler
6
8
 
9
+ # Compile route set to a JavaScript object
10
+ #
11
+ # @return [String]
12
+ #
13
+ # @api private
7
14
  def self.compile(route_set)
8
- functions = route_set.inject(Set.new) do |memo, route|
15
+ functions = named_routes(route_set).inject(Set.new) do |memo, route|
9
16
  name = route.name.camelize(:lower) + "Path"
10
17
  memo << "#{name} : " + PathCompiler.compile(route.path)
11
18
  end.to_a.join(',')
19
+
12
20
  <<-COMPILED
13
21
  var Routes = {
14
22
  #{functions}
@@ -16,9 +24,18 @@ var Routes = {
16
24
  COMPILED
17
25
  end
18
26
 
27
+ # Write set to IO object
28
+ #
29
+ # @return [IO]
30
+ #
31
+ # @api private
19
32
  def self.compile_to_io(route_set, io)
20
33
  io << compile(route_set)
21
34
  end
22
35
 
36
+ def self.named_routes(routes)
37
+ routes.select {|x| x.name.present? }
38
+ end
23
39
  end
24
40
  end
41
+
@@ -0,0 +1,15 @@
1
+ require 'tilt/template'
2
+ require 'strawberry_cough/routes_compiler'
3
+
4
+ module StrawberryCough
5
+ class RoutesJSTemplate < Tilt::Template
6
+
7
+ def prepare
8
+ end
9
+
10
+ def render(scope=Object.new, locals={}, &block)
11
+ route_set = ::Rails.application.routes
12
+ RoutesCompiler.compile(route_set.routes)
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,4 @@
1
1
  module StrawberryCough
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
4
+
@@ -2,4 +2,5 @@ require 'strawberry_cough/version'
2
2
  require 'strawberry_cough/path_compiler'
3
3
  require 'strawberry_cough/path_parser'
4
4
  require 'strawberry_cough/routes_compiler'
5
- require 'strawberry_cough/railtie'
5
+ require 'strawberry_cough/rails/engine'
6
+ require 'strawberry_cough/routes_js_template'
@@ -4,10 +4,11 @@ describe StrawberryCough::RoutesCompiler do
4
4
  let(:compiler) { StrawberryCough::RoutesCompiler }
5
5
  let(:interpreter) { V8::Context.new }
6
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
7
+ it "generates an empty Routes object with an empty route set" do
8
+ empty_routes_object = compiler.compile([])
9
+ interpreter.eval(empty_routes_object)
10
+ property_check = "for (var prop in Routes) { return prop; }"
11
+ interpreter.eval(property_check).should be_nil
11
12
  end
12
13
 
13
14
  it "makes a Routes JavaScript object when given an array of routes" do
@@ -19,3 +20,4 @@ describe StrawberryCough::RoutesCompiler do
19
20
  interpreter.eval("typeof Routes.editParentPath === 'function'").should be_true
20
21
  end
21
22
  end
23
+
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
5
5
  s.name = "strawberry_cough"
6
6
  s.version = StrawberryCough::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
- s.authors = ["Craig Savolainen", "Nate Smith"]
8
+ s.authors = ["Craig Savolainen", "Nate Smith", "Shawn Lee-Kwong"]
9
9
  s.email = ["support@threewisemen.ca"]
10
10
  s.homepage = "http://github.com/ThreeWiseMen/strawberry_cough"
11
11
  s.summary = "Strawberry Cough lets you access your Rails 3 routes in your JavaScripts."