includejs 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module Includejs
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -14,6 +14,11 @@ describe "include", ->
14
14
  included: (klass) ->
15
15
  klass.baz = "Baz!!"
16
16
 
17
+ FuBarModule =
18
+ sayHi: ->
19
+ "Hello!"
20
+ fu: 'Bar!'
21
+
17
22
  it "includes functions from the module into a class", ->
18
23
  class Foo
19
24
  constructor: ->
@@ -57,7 +62,7 @@ describe "include", ->
57
62
  it "works with plain objects as well", ->
58
63
  foo = {}
59
64
  include(foo, BazModule)
60
-
65
+
61
66
  expect(foo.sayHi()).toEqual("Hello undefined!")
62
67
 
63
68
  foo.name = "Mark"
@@ -65,4 +70,13 @@ describe "include", ->
65
70
 
66
71
  x = (fn) -> fn()
67
72
 
68
- expect(x(foo.sayHi)).toEqual("Hello Mark!")
73
+ expect(x(foo.sayHi)).toEqual("Hello Mark!")
74
+
75
+ it "handles variables just fine", ->
76
+ class Foo
77
+ constructor: ->
78
+ include(@, FuBarModule)
79
+
80
+ foo = new Foo()
81
+ expect(foo.sayHi()).toEqual("Hello!")
82
+ expect(foo.fu).toEqual("Bar!")
@@ -4,15 +4,54 @@ Includejs
4
4
  https://github.com/markbates/includejs
5
5
  ###
6
6
  @include = (klass, modules...) =>
7
+ ###
8
+ Since we can't assume there are any other
9
+ libraries available to us, let's create our
10
+ own simple binding function to bind the
11
+ module's functions to the original object.
12
+ ###
7
13
  __binder = (fn, me) ->
8
14
  return ->
9
15
  return fn.apply(me, arguments)
16
+
17
+ ###
18
+ Since we can't assume there are any other
19
+ libraries available to us, let's create our
20
+ own function to check whether an object is a
21
+ function or not.
22
+ ###
23
+ __isFunction = (fn) ->
24
+ !!(fn and fn.constructor and fn.call and fn.apply)
10
25
 
26
+ ###
27
+ Loop through all of the modules that are passed in.
28
+ ###
11
29
  for mod in modules
12
-
30
+ ###
31
+ If the module has a 'included' function
32
+ then let's call it and pass it the object
33
+ that's including the module.
34
+ ###
13
35
  if mod.included?
14
36
  mod.included(klass)
15
37
 
16
38
  for key, value of mod
39
+ ###
40
+ As long as the 'key' is not the 'included' or
41
+ 'include' function, then let's include it in the
42
+ original object and bind it together.
43
+ ###
17
44
  unless key in ["included", "include"]
18
- klass[key] = __binder(value, klass)
45
+ klass[key] =
46
+ if __isFunction(value)
47
+ ###
48
+ If the value is a function then let's
49
+ bind it to the current object.
50
+ ###
51
+ __binder(value, klass)
52
+ else
53
+ ###
54
+ The value isn't a function, so let's just
55
+ set it and forget it.
56
+ ###
57
+ value
@@ -11,16 +11,43 @@ https://github.com/markbates/includejs
11
11
  __slice = [].slice;
12
12
 
13
13
  this.include = function() {
14
- var key, klass, mod, modules, value, __binder, _i, _len, _results;
14
+ var key, klass, mod, modules, value, __binder, __isFunction, _i, _len, _results;
15
15
  klass = arguments[0], modules = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
16
+ /*
17
+ Since we can't assume there are any other
18
+ libraries available to us, let's create our
19
+ own simple binding function to bind the
20
+ module's functions to the original object.
21
+ */
22
+
16
23
  __binder = function(fn, me) {
17
24
  return function() {
18
25
  return fn.apply(me, arguments);
19
26
  };
20
27
  };
28
+ /*
29
+ Since we can't assume there are any other
30
+ libraries available to us, let's create our
31
+ own function to check whether an object is a
32
+ function or not.
33
+ */
34
+
35
+ __isFunction = function(fn) {
36
+ return !!(fn && fn.constructor && fn.call && fn.apply);
37
+ };
38
+ /*
39
+ Loop through all of the modules that are passed in.
40
+ */
41
+
21
42
  _results = [];
22
43
  for (_i = 0, _len = modules.length; _i < _len; _i++) {
23
44
  mod = modules[_i];
45
+ /*
46
+ If the module has a 'included' function
47
+ then let's call it and pass it the object
48
+ that's including the module.
49
+ */
50
+
24
51
  if (mod.included != null) {
25
52
  mod.included(klass);
26
53
  }
@@ -29,8 +56,30 @@ https://github.com/markbates/includejs
29
56
  _results1 = [];
30
57
  for (key in mod) {
31
58
  value = mod[key];
59
+ /*
60
+ As long as the 'key' is not the 'included' or
61
+ 'include' function, then let's include it in the
62
+ original object and bind it together.
63
+ */
64
+
32
65
  if (key !== "included" && key !== "include") {
33
- _results1.push(klass[key] = __binder(value, klass));
66
+ _results1.push(klass[key] = (function() {
67
+ if (__isFunction(value)) {
68
+ /*
69
+ If the value is a function then let's
70
+ bind it to the current object.
71
+ */
72
+
73
+ return __binder(value, klass);
74
+ } else {
75
+ /*
76
+ The value isn't a function, so let's just
77
+ set it and forget it.
78
+ */
79
+
80
+ return value;
81
+ }
82
+ })());
34
83
  } else {
35
84
  _results1.push(void 0);
36
85
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: includejs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70170748893700 !ruby/object:Gem::Requirement
16
+ requirement: &70206983152540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70170748893700
24
+ version_requirements: *70206983152540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: growl
27
- requirement: &70170748892260 !ruby/object:Gem::Requirement
27
+ requirement: &70206983150860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70170748892260
35
+ version_requirements: *70206983150860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard
38
- requirement: &70170748891440 !ruby/object:Gem::Requirement
38
+ requirement: &70206983148580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70170748891440
46
+ version_requirements: *70206983148580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-coffeescript
49
- requirement: &70170748888760 !ruby/object:Gem::Requirement
49
+ requirement: &70206983163960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70170748888760
57
+ version_requirements: *70206983163960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard-jasmine-headless-webkit
60
- requirement: &70170748902560 !ruby/object:Gem::Requirement
60
+ requirement: &70206983163240 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70170748902560
68
+ version_requirements: *70206983163240
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: jasmine
71
- requirement: &70170748899440 !ruby/object:Gem::Requirement
71
+ requirement: &70206983162020 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70170748899440
79
+ version_requirements: *70206983162020
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jasmine-headless-webkit
82
- requirement: &70170748896620 !ruby/object:Gem::Requirement
82
+ requirement: &70206983160040 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70170748896620
90
+ version_requirements: *70206983160040
91
91
  description: Adds the ability to include Modules into JavaScript objects
92
92
  email:
93
93
  - mark@markbates.com