stitch_rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -0,0 +1,56 @@
1
+ # https://gist.github.com/1153919
2
+
3
+ modules = {}
4
+ cache = {}
5
+
6
+ require = (name, root) ->
7
+ if module = cache[name]
8
+ return module
9
+
10
+ path = expand(root, name)
11
+ if fn = modules[path] || modules[path = expand(path, './index')]
12
+ module = id: name, exports: {}
13
+ try
14
+ cache[name] = module.exports
15
+ fn(module.exports, ((name) -> require(name, dirname(path))), module)
16
+ cache[name] = module.exports
17
+ catch err
18
+ delete cache[name];
19
+ throw err
20
+ else
21
+ throw "module #{name} not found"
22
+
23
+
24
+ expand = (root, name) ->
25
+ results = []
26
+ if /^\.\.?(\/|$)/.test(name)
27
+ parts = [root, name].join('/').split('/')
28
+ else
29
+ parts = name.split('/')
30
+
31
+ for part in parts
32
+ if part is '..'
33
+ results.pop()
34
+ else if part isnt '.' && part isnt ''
35
+ results.push(part)
36
+
37
+ results.join('/')
38
+
39
+ dirname = (path) ->
40
+ path.split('/').slice(0, -1).join('/')
41
+
42
+ @require = (name) ->
43
+ require(name, '')
44
+
45
+ @requireIf = (nameAssertion, callback) ->
46
+ for name of modules when nameAssertion(name)
47
+ exports = require(name)
48
+ callback?(name, exports)
49
+ null
50
+
51
+ @require.define = (bundle) ->
52
+ for key of bundle
53
+ modules[key] = bundle[key];
54
+ bundle
55
+
56
+ module?.exports = { require, requireIf }
data/lib/stitch_rails.rb CHANGED
@@ -2,20 +2,49 @@ module Stitch
2
2
  class StitchyCoffeeScriptTemplate < Tilt::Template
3
3
  self.default_mime_type = 'application/javascript'
4
4
 
5
+ @@default_bare = false
6
+
7
+ def self.default_bare
8
+ @@default_bare
9
+ end
10
+
11
+ def self.default_bare=(value)
12
+ @@default_bare = value
13
+ end
14
+
15
+ @@excludes = []
16
+
5
17
  def self.engine_initialized?
6
18
  defined? ::CoffeeScript
7
19
  end
8
20
 
21
+ def self.excludes=(excludes)
22
+ @@excludes = excludes
23
+ end
24
+
9
25
  def initialize_engine
10
26
  require_template_library 'coffee_script'
11
27
  end
12
28
 
13
29
  def prepare
14
- options[:bare] = true
30
+ if !options.key?(:bare)
31
+ options[:bare] = self.class.default_bare
32
+ end
15
33
  end
16
34
 
17
35
  def evaluate(scope, locals, &block)
18
- @output ||= "\nrequire.define({'#{ module_name(scope) }': function(exports, require, module) {\n" + CoffeeScript.compile(data, options) + "\n}});\n"
36
+ name = module_name(scope)
37
+ if (name == 'stitch_rails') || @@excludes.include?(name)
38
+ @output ||= CoffeeScript.compile(data, options)
39
+ else
40
+ @output ||= <<JS
41
+ require.define({
42
+ '#{name}': function(exports, require, module) {
43
+ #{CoffeeScript.compile(data, options.merge(:bare => true))}
44
+ }
45
+ });
46
+ JS
47
+ end
19
48
  end
20
49
 
21
50
  private
@@ -27,6 +56,10 @@ module Stitch
27
56
  end
28
57
 
29
58
  class Railtie < ::Rails::Engine
59
+ config.before_configuration do
60
+ config.stitch = StitchyCoffeeScriptTemplate
61
+ end
62
+
30
63
  initializer 'stitch.configure_rails_initialization' do |app|
31
64
  app.assets.register_engine '.coffee', StitchyCoffeeScriptTemplate
32
65
  end
data/stitch_rails.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "stitch_rails"
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ian Yang"]
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
- "lib/assets/javascripts/stitch_rails.js",
26
+ "lib/assets/javascripts/stitch_rails.js.coffee",
27
27
  "lib/stitch_rails.rb",
28
28
  "stitch_rails.gemspec"
29
29
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stitch_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &9143400 !ruby/object:Gem::Requirement
16
+ requirement: &21144680 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *9143400
24
+ version_requirements: *21144680
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jeweler
27
- requirement: &9142900 !ruby/object:Gem::Requirement
27
+ requirement: &21144180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.8.3
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *9142900
35
+ version_requirements: *21144180
36
36
  description: coffee CommonJS
37
37
  email: me@iany.me
38
38
  executables: []
@@ -47,7 +47,7 @@ files:
47
47
  - README.rdoc
48
48
  - Rakefile
49
49
  - VERSION
50
- - lib/assets/javascripts/stitch_rails.js
50
+ - lib/assets/javascripts/stitch_rails.js.coffee
51
51
  - lib/stitch_rails.rb
52
52
  - stitch_rails.gemspec
53
53
  homepage: http://github.com/doitian/stitch_rails
@@ -65,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
65
  version: '0'
66
66
  segments:
67
67
  - 0
68
- hash: -2075132303226011823
68
+ hash: 714908353892359014
69
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  none: false
71
71
  requirements:
@@ -1,60 +0,0 @@
1
- // https://gist.github.com/1153919
2
-
3
- (function(/*! Stitch !*/) {
4
- if (!this.require) {
5
-
6
- var modules = {},
7
- cache = {},
8
- require = function(name, root) {
9
- var module = cache[name], path = expand(root, name), fn;
10
- if (module) {
11
- return module;
12
- } else if (fn = modules[path] || modules[path = expand(path, './index')]) {
13
- module = {id: name, exports: {}};
14
- try {
15
- cache[name] = module.exports;
16
-
17
- fn(module.exports, function(name) {
18
- return require(name, dirname(path));
19
- }, module);
20
-
21
- return cache[name] = module.exports;
22
- } catch (err) {
23
- delete cache[name];
24
- throw err;
25
- }
26
- } else {
27
- throw 'module "' + name + '" not found';
28
- }
29
- },
30
- expand = function(root, name) {
31
- var results = [], parts, part;
32
- if (/^\.\.?(\/|$)/.test(name)) {
33
- parts = [root, name].join('/').split('/');
34
- } else {
35
- parts = name.split('/');
36
- }
37
- for (var i = 0, length = parts.length; i < length; i++) {
38
- part = parts[i];
39
- if (part == '..') {
40
- results.pop();
41
- } else if (part != '.' && part != '') {
42
- results.push(part);
43
- }
44
- }
45
- return results.join('/');
46
- },
47
- dirname = function(path) {
48
- return path.split('/').slice(0, -1).join('/');
49
- };
50
-
51
- this.require = function(name) {
52
- return require(name, '');
53
- }
54
-
55
- this.require.define = function(bundle) {
56
- for (var key in bundle)
57
- modules[key] = bundle[key];
58
- };
59
- }
60
- }).call(this)