1- require 'requirejs/rails'
2- require 'requirejs/error'
1+ require "pathname"
32
4- require 'active_support/ordered_options'
5- require 'erubis'
6- require 'pathname'
3+ require "active_support/ordered_options"
4+ require "erubis"
75
8- module Requirejs ::Rails
9- class Config < ::ActiveSupport ::OrderedOptions
10- LOADERS = [ :requirejs , :almond ]
6+ require "requirejs/error"
7+ require "requirejs/rails"
118
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 ]
1513
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
1917
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
2421
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'
2626
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
2928
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" )
3131
32- self . run_config_whitelist = %w{
32+ self . user_config = { }
33+
34+ self . run_config_whitelist = %w{
3335 baseUrl
3436 callback
3537 catchError
@@ -49,7 +51,7 @@ def initialize(application)
4951 xhtml
5052 }
5153
52- self . build_config_whitelist = %w{
54+ self . build_config_whitelist = %w{
5355 appDir
5456 baseUrl
5557 closure
@@ -88,69 +90,70 @@ def initialize(application)
8890 useStrict
8991 wrap
9092 }
91- end
93+ end
9294
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
96100 end
97- self [ :loader ] = sym
98- end
99101
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
112119 end
113- mod = mods [ 0 ]
114- name = mod [ 'name' ]
115- mod [ 'name' ] = 'almond'
116- mod [ 'include' ] = name
117120 end
121+ self [ :build_config ]
118122 end
119- self [ :build_config ]
120- end
121123
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 ]
126130 end
127- self [ :run_config ]
128- end
129131
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
133137 end
134- self [ :user_config ] = cfg
135- end
136138
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
143146 end
144- end
145147
146- def get_binding
147- return binding ( )
148- end
148+ def get_binding
149+ return binding ( )
150+ end
149151
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
154157 end
155158 end
156159end
0 commit comments