1
- require 'requirejs/rails'
2
- require 'requirejs/error'
1
+ require "pathname"
3
2
4
- require 'active_support/ordered_options'
5
- require 'erubis'
6
- require 'pathname'
3
+ require "active_support/ordered_options"
4
+ require "erubis"
7
5
8
- module Requirejs ::Rails
9
- class Config < ::ActiveSupport ::OrderedOptions
10
- LOADERS = [ :requirejs , :almond ]
6
+ require "requirejs/error"
7
+ require "requirejs/rails"
11
8
12
- def initialize ( application )
13
- super
14
- self . manifest = nil
9
+ module Requirejs
10
+ module Rails
11
+ class Config < ::ActiveSupport ::OrderedOptions
12
+ LOADERS = [ :requirejs , :almond ]
15
13
16
- self . logical_asset_filter = [ / \. js$/ , / \. html$/ , / \. txt$/ ]
17
- self . tmp_dir = application . root + 'tmp'
18
- self . bin_dir = Pathname . new ( __FILE__ + '/../../../../bin' ) . cleanpath
14
+ def initialize ( application )
15
+ super
16
+ self . manifest = nil
19
17
20
- self . source_dir = self . tmp_dir . join ( "requirejs/src" )
21
- self . build_dir = self . tmp_dir . join ( "requirejs/dst" )
22
- self . target_dir = application . root + 'public/assets'
23
- self . rjs_path = self . bin_dir +'r.js'
18
+ self . logical_asset_filter = [ /\. js$/ , /\. html$/ , /\. txt$/ ]
19
+ self . tmp_dir = application . root + 'tmp'
20
+ self . bin_dir = Pathname . new ( __FILE__ +'/../../../../bin' ) . cleanpath
24
21
25
- self . loader = :requirejs
22
+ self . source_dir = self . tmp_dir . join ( "requirejs/src" )
23
+ self . build_dir = self . tmp_dir . join ( "requirejs/dst" )
24
+ self . target_dir = application . root + 'public/assets'
25
+ self . rjs_path = self . bin_dir +'r.js'
26
26
27
- self . driver_template_path = Pathname . new ( __FILE__ +'/../rjs_driver.js.erb' ) . cleanpath
28
- self . driver_path = self . tmp_dir . join ( "requirejs/rjs_driver.js" )
27
+ self . loader = :requirejs
29
28
30
- self . user_config = { }
29
+ self . driver_template_path = Pathname . new ( __FILE__ +'/../rjs_driver.js.erb' ) . cleanpath
30
+ self . driver_path = self . tmp_dir . join ( "requirejs/rjs_driver.js" )
31
31
32
- self . run_config_whitelist = %w{
32
+ self . user_config = { }
33
+
34
+ self . run_config_whitelist = %w{
33
35
baseUrl
34
36
callback
35
37
catchError
@@ -49,7 +51,7 @@ def initialize(application)
49
51
xhtml
50
52
}
51
53
52
- self . build_config_whitelist = %w{
54
+ self . build_config_whitelist = %w{
53
55
appDir
54
56
baseUrl
55
57
closure
@@ -88,69 +90,70 @@ def initialize(application)
88
90
useStrict
89
91
wrap
90
92
}
91
- end
93
+ end
92
94
93
- def loader = ( sym )
94
- unless LOADERS . include? ( sym )
95
- raise Requirejs ::ConfigError , "Attempt to set unknown loader: #{ sym } "
95
+ def loader = ( sym )
96
+ unless LOADERS . include? ( sym )
97
+ raise Requirejs ::ConfigError , "Attempt to set unknown loader: #{ sym } "
98
+ end
99
+ self [ :loader ] = sym
96
100
end
97
- self [ :loader ] = sym
98
- end
99
101
100
- def build_config
101
- unless self . has_key? ( :build_config )
102
- self [ :build_config ] = self . run_config . merge "baseUrl" => source_dir . to_s ,
103
- "modules" => [ { 'name' => 'application' } ]
104
- self [ :build_config ] . merge! ( self . user_config ) . slice! ( *self . build_config_whitelist )
105
- case self . loader
106
- when :requirejs
107
- # nothing to do
108
- when :almond
109
- mods = self [ :build_config ] [ 'modules' ]
110
- unless mods . length == 1
111
- raise Requirejs ::ConfigError , "Almond build requires exactly one module, config has #{ mods . length } ."
102
+ def build_config
103
+ unless self . has_key? ( :build_config )
104
+ self [ :build_config ] = self . run_config . merge "baseUrl" => source_dir . to_s ,
105
+ "modules" => [ { 'name' => 'application' } ]
106
+ self [ :build_config ] . merge! ( self . user_config ) . slice! ( *self . build_config_whitelist )
107
+ case self . loader
108
+ when :requirejs
109
+ # nothing to do
110
+ when :almond
111
+ mods = self [ :build_config ] [ 'modules' ]
112
+ unless mods . length == 1
113
+ raise Requirejs ::ConfigError , "Almond build requires exactly one module, config has #{ mods . length } ."
114
+ end
115
+ mod = mods [ 0 ]
116
+ name = mod [ 'name' ]
117
+ mod [ 'name' ] = 'almond'
118
+ mod [ 'include' ] = name
112
119
end
113
- mod = mods [ 0 ]
114
- name = mod [ 'name' ]
115
- mod [ 'name' ] = 'almond'
116
- mod [ 'include' ] = name
117
120
end
121
+ self [ :build_config ]
118
122
end
119
- self [ :build_config ]
120
- end
121
123
122
- def run_config
123
- unless self . has_key? ( :run_config )
124
- self [ :run_config ] = { "baseUrl" => "/assets" }
125
- self [ :run_config ] . merge! ( self . user_config ) . slice! ( *self . run_config_whitelist )
124
+ def run_config
125
+ unless self . has_key? ( :run_config )
126
+ self [ :run_config ] = { "baseUrl" => "/assets" }
127
+ self [ :run_config ] . merge! ( self . user_config ) . slice! ( *self . run_config_whitelist )
128
+ end
129
+ self [ :run_config ]
126
130
end
127
- self [ :run_config ]
128
- end
129
131
130
- def user_config = ( cfg )
131
- if url = cfg . delete ( 'baseUrl' )
132
- raise Requirejs ::ConfigError , "baseUrl is not needed or permitted in the configuration"
132
+ def user_config = ( cfg )
133
+ if url = cfg . delete ( 'baseUrl' )
134
+ raise Requirejs ::ConfigError , "baseUrl is not needed or permitted in the configuration"
135
+ end
136
+ self [ :user_config ] = cfg
133
137
end
134
- self [ :user_config ] = cfg
135
- end
136
138
137
- def module_name_for ( mod )
138
- case self . loader
139
- when :almond
140
- return mod [ 'include' ]
141
- when :requirejs
142
- return mod [ 'name' ]
139
+ def module_name_for ( mod )
140
+ case self . loader
141
+ when :almond
142
+ return mod [ 'include' ]
143
+ when :requirejs
144
+ return mod [ 'name' ]
145
+ end
143
146
end
144
- end
145
147
146
- def get_binding
147
- return binding ( )
148
- end
148
+ def get_binding
149
+ return binding ( )
150
+ end
149
151
150
- def asset_allowed? ( asset )
151
- self . logical_asset_filter . reduce ( false ) do |accum , matcher |
152
- accum || ( matcher =~ asset )
153
- end ? true : false
152
+ def asset_allowed? ( asset )
153
+ self . logical_asset_filter . reduce ( false ) do |accum , matcher |
154
+ accum || ( matcher =~ asset )
155
+ end ? true : false
156
+ end
154
157
end
155
158
end
156
159
end
0 commit comments