js-initializers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d98734161907a8ef44b4ba6e34e4f9022226520
4
+ data.tar.gz: 4c97c67aa9c450b82bd0beb410b6a39d58f8d0d0
5
+ SHA512:
6
+ metadata.gz: 5fe199e0251cd868b655a3ceeb62cb1cb2a5e196d0657b3195daf0e6c187f3d2cd60eeadcff53a9acf240ce7d6097ec1c23547bfb13b89eca5e2f5d5e37c2b52
7
+ data.tar.gz: b31cacbdc7a833c3a69b3c5f0aaeae8fb4e0011228e2d7c4fd942996e33a3c7deb8668cc111f667cb8ece3266e1287e905d76905776e51924dc4d45faaf85df0
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in js-initializers.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nahuel Cuesta Luengo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # js-initializers
2
+
3
+ Rails engine that provides a micro-framework for organizing in a modular way your client-side scripts, as well as it enables you to only run the *necessary* modules for each page.
4
+
5
+ (Better readme coming soon!)
6
+
7
+ ## Usage
8
+
9
+ Add `js-initializers` to your Gemfile
10
+
11
+ ```ruby
12
+ # Gemfile
13
+ gem 'js-initializers'
14
+ ```
15
+
16
+ Add `js-initializers` to your Sprockets manifest:
17
+
18
+ ```javascript
19
+ //= require js-initializers
20
+ ```
21
+
22
+ Et voilà! You're ready to add your initializers:
23
+
24
+ ```javascript
25
+ // app/assets/javascripts/initializers/rickroll.js
26
+ (function($) {
27
+ function isRickrollable() {
28
+ return window.user !== 'admin';
29
+ }
30
+
31
+ function rickroll() {
32
+ $('a').on('click', function() { window.location = 'www.youtube.com/watch?v=dQw4w9WgXcQ'; return false; });
33
+ }
34
+
35
+ Initializers.register('rickroll', rickroll, isRickrollable);
36
+ }(jQuery));
37
+ ```
38
+
39
+ For further instructions on how to use this library and defining your own initializers, you may refer to the source code of [js-initializers.js](https://github.com/ncuesta/js-initializers/blob/master/app/assets/javascripts/js-initializers/initializers.js).
40
+
41
+ # Contributing
42
+
43
+ Contributions are welcome! Just fork, commit and send a pull request :)
44
+
45
+ # Author
46
+
47
+ This skinny gem is brought to you by Nahuel Cuesta Luengo. You may reach him at [@ncuestal](https://twitter.com/ncuestal) on Twitter or by email at nahuelcuestaluengo@gmail.com.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,7 @@
1
+ // js-initializers manifest
2
+ //
3
+ // This file is provided for convenience purposes so you can simply
4
+ // //= require js-initializers
5
+ // in your application's manifest
6
+ //
7
+ //= require_tree .
@@ -0,0 +1,168 @@
1
+ // Copyright (c) 2015 Nahuel Cuesta Luengo <nahuelcuestaluengo@gmail.com>
2
+ //
3
+ // Injectable initializers micro-framework
4
+ //
5
+ // This allows for decoupled addition of new javascript initializers into your
6
+ // regular workflow and unobstrusively running the code, both with the winning
7
+ // of modularity -you can organize every piece of JS code in different files-
8
+ // and clarity. You can either have your scripts run on every page load, or
9
+ // only when a certain condition is met, which even will boost the performance
10
+ // of your application on the client side by only running the necessary JS for
11
+ // each situation.
12
+ //
13
+ // Initializers may provide the minimum necessary logic to get up and running
14
+ // with a third-party JS library, preparing target elements for usage and/or
15
+ // add complex logic to the page to initialize a super cool UX bloated with
16
+ // in-house JS code.
17
+ //
18
+ // Usage:
19
+ // Add an initializer with the initialization logic:
20
+ //
21
+ // // app/assets/javascripts/initializers/alert.js
22
+ // (function($) {
23
+ // Initializers.register('alert', function() {
24
+ // $('a').on('click', function() { alert('Hey there!'); });
25
+ // });
26
+ // }(jQuery))
27
+ //
28
+ // Once the initializer is registered, it will be automatically run
29
+ // on every page load (or page:change event, if using Turbolinks),
30
+ // so go refresh that page and see those alerts pop up!
31
+ //
32
+ // Using conditions:
33
+ // An initializer with a condition guard will only get run when its guard
34
+ // evaluates to a truthy value:
35
+ //
36
+ // // app/assets/javascripts/initializers/fridays.js
37
+ // (function() {
38
+ // function party() {
39
+ // alert('Today is friday! w00t!');
40
+ // }
41
+ //
42
+ // function isFriday() {
43
+ // return (new Date()).getDay() === 5;
44
+ // }
45
+ //
46
+ // Initializers.register('fridays', party, isFriday);
47
+ // }())
48
+ //
49
+ // Once the initializer is registered, it will run only when its condition
50
+ // returns true.
51
+ //
52
+ // Running the initializers without jQuery:
53
+ // If the page where js-initializers is included has jQuery loaded, all
54
+ // registerd initializers will be automatically run on page load. But what
55
+ // about non-jQuery pages? Those pages only ned to have the following snippet
56
+ // after the page has been loaded:
57
+ //
58
+ // Initializers.run()
59
+ //
60
+ // And that's it! Pretty straight-forward, huh?
61
+ window.Initializers = (function($, doc, undefined) {
62
+ var I = {}, initializers = {};
63
+
64
+ /**
65
+ * Default condition function: always returns true.
66
+ * By default any initializer will be run.
67
+ */
68
+ function defaultCondition() {
69
+ return true;
70
+ }
71
+
72
+ /**
73
+ * Error reporting helper.
74
+ */
75
+ function error(message) {
76
+ if (window.console && window.console.error) {
77
+ console.error(message);
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Adds a new element to the list of registered initializers.
83
+ * Once an initializer is registered, it becomes instantly available to the
84
+ * `Initializers.run()` method.
85
+ *
86
+ * Trying to overwrite an existing initializer won't affect the state of the
87
+ * existing one, instead will yield an error on the console -if possible-.
88
+ *
89
+ * // Register an initializer that alerts 'meow' on every page load
90
+ * // only if the user is 'catlover492'
91
+ * Initializer.register(
92
+ * 'meow',
93
+ * function() { alert('meow'); },
94
+ * function() { return window.user === 'catlover492' }
95
+ * )
96
+ */
97
+ I.register = function(name, initializer, condition) {
98
+ if (initializers[name] !== undefined) {
99
+ error('Cowardly refusing to overwrite existing initializer: ' + name);
100
+ } else {
101
+ initializers[name] = { condition: condition || defaultCondition,
102
+ logic: initializer };
103
+ }
104
+ };
105
+
106
+ /**
107
+ * Removes an initializer from the registered ones so it's no longer
108
+ * avaiable to the `Initializers.run()` function.
109
+ *
110
+ * Initializers.unregister('alert')
111
+ */
112
+ I.unregister = function(name) {
113
+ if (initializers[name] === undefined) {
114
+ delete initializers[name];
115
+ } else {
116
+ error('Trying to unregister unknown initializer: ' + name);
117
+ }
118
+ };
119
+
120
+ /**
121
+ * Goes through all the registered initializers, checking the condition block
122
+ * provided for each of them and running the logic of every initializer whose
123
+ * condition block returns a truthy value.
124
+ *
125
+ * Returns an array with the names of the initializers that were run.
126
+ *
127
+ * Initializers.run()
128
+ * //=> ['alert', 'snowPage', 'marquee']
129
+ *
130
+ * This function may be called any number of times, but bear in mind that any
131
+ * non-idempotent initializer may result in an unexpected/undesired behavior.
132
+ * It's entirely up to the implementer of the initializer to provide with a
133
+ * safe logic
134
+ */
135
+ I.run = function() {
136
+ var run = [];
137
+ for (var name in initializers) {
138
+ if (initializers[name].condition.apply()) {
139
+ initializers[name].logic.apply(this);
140
+ run.push(name);
141
+ }
142
+ }
143
+ return run;
144
+ };
145
+
146
+ /**
147
+ * Returns all registered initializers at the moment of invocation,
148
+ * as known by the microframework:
149
+ *
150
+ * Initializers.registered()
151
+ * //=> {alert: {condition: function() {...}, logic: function() {...}}}
152
+ */
153
+ I.registered = function() {
154
+ return initializers;
155
+ };
156
+
157
+ // If jQuery is available, automatically run registed initializers
158
+ if ($ !== undefined) {
159
+ // Trigger initializers on page load (either with Turbolinks or without 'em)
160
+ if (window.Turbolinks === undefined) {
161
+ $($.runInitializers);
162
+ } else {
163
+ $(doc).on('page:change', $.runInitializers);
164
+ }
165
+ }
166
+
167
+ return I;
168
+ }(jQuery, document));
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'js-initializers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'js-initializers'
8
+ spec.version = JsInitializers::VERSION
9
+ spec.authors = ['Nahuel Cuesta Luengo']
10
+ spec.email = ['nahuelcuestaluengo@gmail.com']
11
+ spec.summary = %q{Avoid JS mayhem on your Rails app! js-initializers is an engine that adds a micro-framework for initializing and organizing client-side logic in a modular way}
12
+ spec.description = %q{Avoid JS mayhem on your Rails app! js-initializers is an engine that adds a micro-framework for initializing and organizing client-side logic in a modular way}
13
+
14
+ spec.homepage = 'https://github.com/ncuesta/js-initializers'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_dependency 'rails', '> 3.1'
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'js-initializers/version'
2
+ require 'js-initializers/engine'
3
+
4
+ module JsInitializers
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ module JsInitializers
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module JsInitializers
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-initializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nahuel Cuesta Luengo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: Avoid JS mayhem on your Rails app! js-initializers is an engine that
56
+ adds a micro-framework for initializing and organizing client-side logic in a modular
57
+ way
58
+ email:
59
+ - nahuelcuestaluengo@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - app/assets/javascripts/js-initializers/index.js
70
+ - app/assets/javascripts/js-initializers/initializers.js
71
+ - js-initializers.gemspec
72
+ - lib/js-initializers.rb
73
+ - lib/js-initializers/engine.rb
74
+ - lib/js-initializers/version.rb
75
+ homepage: https://github.com/ncuesta/js-initializers
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.5
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Avoid JS mayhem on your Rails app! js-initializers is an engine that adds
99
+ a micro-framework for initializing and organizing client-side logic in a modular
100
+ way
101
+ test_files: []