eastwood 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ #= require eastwood/utils
2
+
3
+ ###*
4
+ * eastwood.js - <%= app %>
5
+ ###
6
+ app = <%= target %>.<%= app %> ||= { }
7
+
8
+ <% I18n.backend.send :init_translations %>
9
+
10
+ app.I18n = <%= I18n.backend.send( :translations ).to_json %>
11
+
12
+ app.I18n.locale = "<%= I18n.locale %>"
13
+ app.I18n.t = ( key, context={} ) ->
14
+ try
15
+ string = app.Utils.keypath( key, @[ @locale ] )
16
+ app.Utils.interpolate string, context
17
+ catch error
18
+ "translation missing: #{@locale}.#{key}"
@@ -0,0 +1,17 @@
1
+ ###*
2
+ * eastwood.js - <%= app %>
3
+ ###
4
+ app = <%= target %>.<%= app %> ||= { }
5
+
6
+ app.Utils =
7
+
8
+ reduce : ( array, initial, iterator ) ->
9
+ memo = initial
10
+ memo = iterator memo, item for item in array
11
+ memo
12
+
13
+ keypath : ( path, object ) ->
14
+ @reduce path.split( '.' ), object, ( memo, key ) -> memo[ key ]
15
+
16
+ interpolate : ( string, context={} ) ->
17
+ string.replace /%{(\w+)}/g, ( match, key ) -> context[ key ] || match
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/jeremyruppel/Git/eastwood
3
3
  specs:
4
- eastwood (0.3.8)
4
+ eastwood (0.5.0)
5
5
  coffee-rails
6
6
  rails (>= 3.1.0)
7
7
 
@@ -49,7 +49,7 @@ GEM
49
49
  coffee-script (2.2.0)
50
50
  coffee-script-source
51
51
  execjs
52
- coffee-script-source (1.2.0)
52
+ coffee-script-source (1.3.3)
53
53
  diff-lcs (1.1.3)
54
54
  erubis (2.7.0)
55
55
  execjs (1.3.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/jeremyruppel/Git/eastwood
3
3
  specs:
4
- eastwood (0.3.8)
4
+ eastwood (0.5.0)
5
5
  coffee-rails
6
6
  rails (>= 3.1.0)
7
7
 
@@ -46,7 +46,7 @@ GEM
46
46
  coffee-script (2.2.0)
47
47
  coffee-script-source
48
48
  execjs
49
- coffee-script-source (1.2.0)
49
+ coffee-script-source (1.3.3)
50
50
  diff-lcs (1.1.3)
51
51
  erubis (2.7.0)
52
52
  execjs (1.3.0)
@@ -32,6 +32,10 @@ module Eastwood
32
32
  @@exports ||= Hash.new
33
33
  end
34
34
 
35
+ def excludes
36
+ @@excludes ||= [ /eastwood_engine/ ]
37
+ end
38
+
35
39
  mattr_accessor :default_route_format
36
40
  mattr_accessor :javascript_route_style
37
41
  mattr_accessor :javascript_namespace
@@ -10,7 +10,7 @@ module Eastwood
10
10
  end
11
11
 
12
12
  def target
13
- env === 'test' ? '( @window = { } )' : 'window'
13
+ env === 'test' ? '( @window ||= { } )' : 'window'
14
14
  end
15
15
 
16
16
  def server_routes
@@ -14,6 +14,12 @@ module Eastwood
14
14
  end
15
15
 
16
16
  def named_routes
17
+ rails_routes.reject { |route| excludes.any? { |pattern| pattern.match( route ) } }
18
+ end
19
+
20
+ protected
21
+
22
+ def rails_routes
17
23
  ::Rails.application.routes.named_routes.routes
18
24
  end
19
25
  end
@@ -1,3 +1,3 @@
1
1
  module Eastwood
2
- VERSION = "0.4.0"
2
+ VERSION = '0.5.0'
3
3
  end
@@ -19,11 +19,8 @@ describe 'eastwood.js' do
19
19
  end
20
20
 
21
21
  describe 'eastwood_engine_path' do
22
- it 'should be defined' do
23
- context.eval( "typeof #{routes}.eastwood_engine_path" ).should eq( 'function' )
24
- end
25
- it 'should return the correct route' do
26
- context.call( "#{routes}.eastwood_engine_path" ).should eq( '/eastwood' )
22
+ it 'should not be defined' do
23
+ context.eval( "typeof #{routes}.eastwood_engine_path" ).should eq( 'undefined' )
27
24
  end
28
25
  end
29
26
 
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'eastwood/i18n.js' do
4
+
5
+ before do
6
+ get '/assets/eastwood/i18n.js'
7
+ end
8
+
9
+ let( :context ){ ExecJS.compile response.body }
10
+ let( :namespace ){ "window.#{Eastwood.application_name}.I18n" }
11
+
12
+ describe 'the response' do
13
+ subject { response }
14
+
15
+ its( :status ){ should eq( 200 ) }
16
+ its( :content_type ){ should eq( 'text/javascript' ) }
17
+ end
18
+
19
+ describe 'locale' do
20
+ it 'should be "en"' do
21
+ context.eval( "#{namespace}.locale" ).should eq( 'en' )
22
+ end
23
+ end
24
+
25
+ describe 'translations' do
26
+ it 'should be defined' do
27
+ context.eval( "typeof #{namespace}.en" ).should eq( 'object' )
28
+ end
29
+
30
+ it 'should provide hello world' do
31
+ context.eval( "#{namespace}.t( 'hello' )" ).should eq( 'Hello world' )
32
+ end
33
+
34
+ it 'should provide nested translations' do
35
+ context.eval( "#{namespace}.t( 'time.pm' )" ).should eq( 'pm' )
36
+ end
37
+
38
+ it 'should interpolate translations' do
39
+ key = "activerecord.errors.messages.record_invalid"
40
+ obj = "{errors:'foo!'}"
41
+
42
+ context.eval( "#{namespace}.t( '#{key}', #{obj})" ).should eq( 'Validation failed: foo!' )
43
+ end
44
+
45
+ it 'should handle missing translations' do
46
+ context.eval( "#{namespace}.t( 'foo' )" ).should eq( 'translation missing: en.foo' )
47
+ context.eval( "#{namespace}.t( 'foo.bar' )" ).should eq( 'translation missing: en.foo.bar' )
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'eastwood/utils.js' do
4
+
5
+ before do
6
+ get '/assets/eastwood/utils.js'
7
+ end
8
+
9
+ let( :context ){ ExecJS.compile response.body }
10
+ let( :namespace ){ "window.#{Eastwood.application_name}.Utils" }
11
+
12
+ describe 'the response' do
13
+ subject { response }
14
+
15
+ its( :status ){ should eq( 200 ) }
16
+ its( :content_type ){ should eq( 'text/javascript' ) }
17
+ end
18
+
19
+ describe 'Utils' do
20
+ it 'should be defined' do
21
+ context.eval( "typeof #{namespace}" ).should eq( 'object' )
22
+ end
23
+ end
24
+
25
+ describe 'reduce' do
26
+ it 'should work' do
27
+ context.eval( "#{namespace}.reduce([1,2,3], 1, function(m,n){return m+n;})" ).should eq( 7 )
28
+ end
29
+ end
30
+
31
+ describe 'keypath' do
32
+ it 'should work' do
33
+ context.eval( "#{namespace}.keypath( 'foo.bar', {foo:{bar:'baz'}})" ).should eq( 'baz' )
34
+ end
35
+ end
36
+
37
+ describe 'interpolate' do
38
+ let( :method ){ "#{namespace}.interpolate" }
39
+ let( :object ){ "{ foo : 'foo!', bar : 'bar!!' }" }
40
+
41
+ it 'should handle one key' do
42
+ context.eval( "#{method}('sup %{foo}?', #{object})" ).should eq( 'sup foo!?' )
43
+ end
44
+
45
+ it 'should handle two keys' do
46
+ context.eval( "#{method}('sup %{bar} %{foo}?', #{object})" ).should eq( 'sup bar!! foo!?' )
47
+ end
48
+
49
+ it 'should ignore missing keys' do
50
+ context.eval( "#{method}('sup %{baz}?', #{object})" ).should eq( 'sup %{baz}?' )
51
+ end
52
+ end
53
+ end
data/spec/spec_helper.rb CHANGED
@@ -47,7 +47,7 @@ RSpec::Matchers.define :delegate do |method|
47
47
  end
48
48
  chain :to do |receiver, method|
49
49
  @receiver, @method = receiver, method
50
- @receiver.should_receive( @method ){ true }
50
+ @receiver.should_receive( @method ){ double( 'delegate' ).as_null_object }
51
51
  end
52
52
  description do
53
53
  "delegate ##{method} to #{@receiver}##{@method}"
@@ -24,6 +24,12 @@ describe Eastwood do
24
24
  end
25
25
  end
26
26
 
27
+ describe '#excludes' do
28
+ it { should respond_to( :excludes ) }
29
+ its( :excludes ){ should be_an( Array ) }
30
+ its( :excludes ){ should include( /eastwood_engine/ ) }
31
+ end
32
+
27
33
  describe '#hash' do
28
34
  it { should respond_to( :hash ) }
29
35
 
@@ -53,7 +53,7 @@ describe 'the Sprockets context class' do
53
53
  before do
54
54
  subject.stub( :env ){ 'test' }
55
55
  end
56
- its( :target ){ should eq( '( @window = { } )' ) }
56
+ its( :target ){ should eq( '( @window ||= { } )' ) }
57
57
  end
58
58
  context 'when #env is production' do
59
59
  before do
@@ -74,15 +74,8 @@ describe 'sever routes' do
74
74
  let( :context ){ ::Rails.application.assets.context_class.new nil, nil, nil }
75
75
 
76
76
  describe 'eastwood_engine' do
77
- subject { context.server_routes[ :eastwood_engine ] }
78
-
79
- its( :name ){ should eq( 'eastwood_engine' ) }
80
- its( :path ){ should eq( '/eastwood' ) }
81
- its( :parts ){ should eq( [ ] ) }
82
-
83
- its( :coffee_name ){ should eq( 'eastwood_engine_path' ) }
84
- its( :coffee_path ){ should eq( '/eastwood' ) }
85
- its( :coffee_args ){ should eq( '' ) }
77
+ subject { context.server_routes }
78
+ it { should_not have_key( :eastwood_engine ) }
86
79
  end
87
80
 
88
81
  describe 'match' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eastwood
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
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: 2012-04-21 00:00:00.000000000 Z
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &120730 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *120730
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: coffee-rails
27
- requirement: &120430 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *120430
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: sqlite3
38
- requirement: &120180 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,62 +53,92 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *120180
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: appraisal
49
- requirement: &119830 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
- - - =
67
+ - - '='
53
68
  - !ruby/object:Gem::Version
54
69
  version: 0.4.1
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *119830
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.4.1
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rspec-rails
60
- requirement: &119480 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
- - - =
83
+ - - '='
64
84
  - !ruby/object:Gem::Version
65
85
  version: 2.9.0
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *119480
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '='
92
+ - !ruby/object:Gem::Version
93
+ version: 2.9.0
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: shoulda
71
- requirement: &119100 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
- - - =
99
+ - - '='
75
100
  - !ruby/object:Gem::Version
76
101
  version: 3.0.1
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *119100
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '='
108
+ - !ruby/object:Gem::Version
109
+ version: 3.0.1
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: execjs
82
- requirement: &118100 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
- - - =
115
+ - - '='
86
116
  - !ruby/object:Gem::Version
87
117
  version: 1.3.0
88
118
  type: :development
89
119
  prerelease: false
90
- version_requirements: *118100
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - '='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.0
91
126
  - !ruby/object:Gem::Dependency
92
127
  name: simplecov
93
- requirement: &116830 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
94
129
  none: false
95
130
  requirements:
96
- - - =
131
+ - - '='
97
132
  - !ruby/object:Gem::Version
98
133
  version: 0.6.1
99
134
  type: :development
100
135
  prerelease: false
101
- version_requirements: *116830
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - '='
140
+ - !ruby/object:Gem::Version
141
+ version: 0.6.1
102
142
  description: Start your client side out right
103
143
  email:
104
144
  - jeremy.ruppel@gmail.com
@@ -116,6 +156,8 @@ files:
116
156
  - README.md
117
157
  - Rakefile
118
158
  - app/assets/javascripts/eastwood.js.coffee.erb
159
+ - app/assets/javascripts/eastwood/i18n.js.coffee.erb
160
+ - app/assets/javascripts/eastwood/utils.js.coffee.erb
119
161
  - eastwood.gemspec
120
162
  - gemfiles/rails-3.1.0.gemfile
121
163
  - gemfiles/rails-3.1.0.gemfile.lock
@@ -133,6 +175,8 @@ files:
133
175
  - lib/eastwood/version.rb
134
176
  - spec/rails/.gitkeep
135
177
  - spec/requests/eastwood_spec.rb
178
+ - spec/requests/i18n_spec.rb
179
+ - spec/requests/utils_spec.rb
136
180
  - spec/spec_helper.rb
137
181
  - spec/support/rails_template.rb
138
182
  - spec/unit/config_spec.rb
@@ -160,13 +204,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
204
  version: '0'
161
205
  requirements: []
162
206
  rubyforge_project: eastwood
163
- rubygems_version: 1.8.15
207
+ rubygems_version: 1.8.19
164
208
  signing_key:
165
209
  specification_version: 3
166
210
  summary: Start your client side out right
167
211
  test_files:
168
212
  - spec/rails/.gitkeep
169
213
  - spec/requests/eastwood_spec.rb
214
+ - spec/requests/i18n_spec.rb
215
+ - spec/requests/utils_spec.rb
170
216
  - spec/spec_helper.rb
171
217
  - spec/support/rails_template.rb
172
218
  - spec/unit/config_spec.rb