Class: Requirejs::Rails::Config

Inherits:
ActiveSupport::OrderedOptions
  • Object
show all
Defined in:
lib/requirejs/rails/config.rb

Constant Summary collapse

LOADERS =
[ :requirejs, :almond ]

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/requirejs/rails/config.rb', line 12

def initialize(application)
  super
  self.manifest = nil

  self.logical_asset_filter = [/\.js$/,/\.html$/,/\.txt$/]
  self.tmp_dir = application.root + 'tmp'
  self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath

  self.source_dir = self.tmp_dir.join("requirejs/src")
  self.build_dir = self.tmp_dir.join("requirejs/dst")
  self.target_dir = application.root + 'public/assets'
  self.rjs_path   = self.bin_dir+'r.js'

  self.loader = :requirejs

  self.driver_template_path = Pathname.new(__FILE__+'/../rjs_driver.js.erb').cleanpath
  self.driver_path = self.tmp_dir.join("requirejs/rjs_driver.js")

  self.user_config = {}

  self.run_config_whitelist = %w{
    baseUrl
    callback
    catchError
    config
    context
    deps
    jQuery
    locale
    map
    packages
    paths
    priority
    scriptType
    shim
    urlArgs
    waitSeconds
    xhtml
  }

  self.build_config_whitelist = %w{
    appDir
    baseUrl
    closure
    cssImportIgnore
    cssIn
    dir
    fileExclusionRegExp
    findNestedDependencies
    has
    hasOnSave
    include
    inlineText
    locale
    mainConfigFile
    map
    modules
    name
    namespace
    onBuildRead
    onBuildWrite
    optimize
    optimizeAllPluginResources
    optimizeCss
    out
    packagePaths
    packages
    paths
    pragmas
    pragmasOnSave
    preserveLicenseComments
    shim
    skipModuleInsertion
    skipPragmas
    uglify
    uglify2
    useStrict
    wrap
  }
end

Instance Method Details

#asset_allowed?(asset) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# File 'lib/requirejs/rails/config.rb', line 150

def asset_allowed?(asset)
  self.logical_asset_filter.reduce(false) do |accum, matcher|
    accum || (matcher =~ asset)
  end ? true : false
end

#build_configObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/requirejs/rails/config.rb', line 100

def build_config
  unless self.has_key?(:build_config)
    self[:build_config] = self.run_config.merge "baseUrl" => source_dir.to_s,
                                                "modules" => [ { 'name' => 'application' } ]
    self[:build_config].merge!(self.user_config).slice!(*self.build_config_whitelist)
    case self.loader
    when :requirejs
      # nothing to do
    when :almond
      mods = self[:build_config]['modules']
      unless mods.length == 1
        raise Requirejs::ConfigError, "Almond build requires exactly one module, config has #{mods.length}."
      end
      mod = mods[0]
      name = mod['name']
      mod['name'] = 'almond'
      mod['include'] = name
    end
  end
  self[:build_config]
end

#get_bindingObject



146
147
148
# File 'lib/requirejs/rails/config.rb', line 146

def get_binding
  return binding()
end

#loader=(sym) ⇒ Object



93
94
95
96
97
98
# File 'lib/requirejs/rails/config.rb', line 93

def loader=(sym)
  unless LOADERS.include?(sym)
    raise Requirejs::ConfigError, "Attempt to set unknown loader: #{sym}"
  end
  self[:loader] = sym
end

#module_name_for(mod) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/requirejs/rails/config.rb', line 137

def module_name_for(mod)
  case self.loader
  when :almond
    return mod['include']
  when :requirejs
    return mod['name']
  end
end

#run_configObject



122
123
124
125
126
127
128
# File 'lib/requirejs/rails/config.rb', line 122

def run_config
  unless self.has_key?(:run_config)
    self[:run_config] = { "baseUrl" => "/assets" }
    self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
  end
  self[:run_config]
end

#user_config=(cfg) ⇒ Object



130
131
132
133
134
135
# File 'lib/requirejs/rails/config.rb', line 130

def user_config=(cfg)
  if url = cfg.delete('baseUrl')
    raise Requirejs::ConfigError, "baseUrl is not needed or permitted in the configuration"
  end
  self[:user_config] = cfg
end