autoexec_bat 0.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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autoexec_bat.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gudleik Rasch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # AutoexecBat
2
+
3
+ AutoexecBat was written to aid organizing and running javascript code using the Rails asset pipeline.
4
+
5
+ The main idea is to organize all javascript code into separate namespaces and let a data-autoexec attribute determine which module to run.
6
+
7
+ Some examples:
8
+
9
+ define "App.Products", (exports) ->
10
+ exports.autoexec = ->
11
+ # this is the autoexec function that AutoexecBat looks for
12
+
13
+ # Module with private methods
14
+ define "App.Products.Index", (exports) ->
15
+ exports.autoexec = ->
16
+ setupEventListeners()
17
+ setupSomethingElse()
18
+ takeItAway()
19
+
20
+ setupEventListeners = ->
21
+ $('table tbody td').on 'click', -> # event handler
22
+ $('input').on 'click', -> # another event handler
23
+
24
+ takeItAway = ->
25
+ $('tag').doSomething()
26
+
27
+ setupSomethingElse = ->
28
+ $('.private').show()
29
+
30
+ # A module with dependencies
31
+ define "App.Products.Show", ["App.Products"], (exports) ->
32
+ exports.autoexec = ->
33
+ # this module has a dependency to App.Products,
34
+ # which will be executed first
35
+
36
+ # If all you want is to run the dependencies:
37
+ define "App.Gallery", ["App.UI.Fancybox", "App.UI.FileUpload"]
38
+
39
+ # A simple coffeescript class:
40
+ class namespace("App.Models").Product
41
+ # this class will be known as App.Models.Product
42
+
43
+ # Run AutoexecBat on all tags with a data-autoexec attribute (using jQuery)
44
+ jQuery ->
45
+ $('[data-autoexec]').autoexec()
46
+
47
+ # y u no got jquery?
48
+ # Run it manually or use whatever
49
+ AutoexecBat.run "App.Products.Show"
50
+ # or
51
+ App.Products.Show.autoexec()
52
+
53
+ Finally, just add the module name to a data-autoexec attribute:
54
+
55
+ <body data-autoexec="App.Products.Index">
56
+
57
+ If the given module doesn't exist, it tries to execute a parent module.
58
+ In this case, the module App.Products.autoexec() will be executed:
59
+
60
+ <div data-autoexec="App.Products.Dev.Null">..</div>
61
+
62
+
63
+ ## Installation
64
+
65
+ Add this line to your application's Gemfile:
66
+
67
+ gem 'autoexec_bat'
68
+
69
+ Require your modules and activate autoexecution:
70
+
71
+ #= require autoexec_bat
72
+ #= require_tree ./folder-containing-your-modules
73
+ jQuery ->
74
+ $('[data-autoexec]').autoexec()
75
+
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/autoexec_bat/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gudleik Rasch"]
6
+ gem.email = ["gudleik@gmail.com"]
7
+ gem.description = %q{Autoexecution of javascript based on data attribute}
8
+ gem.summary = %q{Autoexecution of javascript based on data attribute}
9
+ gem.homepage = "https://github.com/Skalar/autoexec_bat"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "autoexec_bat"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AutoexecBat::VERSION
17
+ end
data/autoexec_bat.js ADDED
@@ -0,0 +1,132 @@
1
+ // Generated by CoffeeScript 1.3.1
2
+ (function() {
3
+ var $, AutoexecBat, root,
4
+ __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
5
+
6
+ root = typeof exports !== "undefined" && exports !== null ? exports : window;
7
+
8
+ AutoexecBat = {
9
+ topLevel: "App",
10
+ debug: true,
11
+ log: function(msg) {
12
+ if (AutoexecBat.debug) {
13
+ return typeof console !== "undefined" && console !== null ? console.log(msg) : void 0;
14
+ }
15
+ },
16
+ define: function(name) {
17
+ var arg, block, dependencies, module, target, top, _i, _j, _len, _len1;
18
+ dependencies = [];
19
+ if (AutoexecBat.autoRequire && name !== AutoexecBat.autoRequire) {
20
+ dependencies.push(AutoexecBat.autoRequire);
21
+ }
22
+ for (_i = 0, _len = arguments.length; _i < _len; _i++) {
23
+ arg = arguments[_i];
24
+ if (typeof arg === "object") {
25
+ for (_j = 0, _len1 = arg.length; _j < _len1; _j++) {
26
+ module = arg[_j];
27
+ if (!(__indexOf.call(dependencies, module) >= 0 && module !== name)) {
28
+ dependencies.push(module);
29
+ }
30
+ }
31
+ } else if (typeof arg === "function") {
32
+ block = arg;
33
+ }
34
+ }
35
+ if (block == null) {
36
+ block = function() {};
37
+ }
38
+ top = AutoexecBat.topLevel;
39
+ target = AutoexecBat.namespace(name);
40
+ target.name = name;
41
+ target.loaded = false;
42
+ target.autoexec = function() {};
43
+ target.dependencies = dependencies;
44
+ target.init = function() {
45
+ if (!this.loaded) {
46
+ require(dependencies);
47
+ if (typeof this.autoexec === 'function') {
48
+ this.autoexec();
49
+ }
50
+ this.autoexec = function() {
51
+ return AutoexecBat.log("Module already initialized");
52
+ };
53
+ return this.loaded = true;
54
+ }
55
+ };
56
+ return block(target, top);
57
+ },
58
+ require: function(dependencies) {
59
+ var lib, _i, _len, _results;
60
+ if (dependencies) {
61
+ _results = [];
62
+ for (_i = 0, _len = dependencies.length; _i < _len; _i++) {
63
+ lib = dependencies[_i];
64
+ _results.push(AutoexecBat.initializeModule(lib));
65
+ }
66
+ return _results;
67
+ }
68
+ },
69
+ namespace: function(name) {
70
+ var item, target, _i, _len, _ref;
71
+ target = root;
72
+ _ref = name.split('.');
73
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
74
+ item = _ref[_i];
75
+ target = target[item] || (target[item] = {});
76
+ }
77
+ return target;
78
+ },
79
+ findModule: function(ns) {
80
+ var item, module, _i, _len;
81
+ if (!ns) {
82
+ return;
83
+ }
84
+ if (typeof ns === "string") {
85
+ ns = ns.split(".");
86
+ }
87
+ if (ns[0] !== AutoexecBat.topLevel) {
88
+ ns.unshift(AutoexecBat.topLevel);
89
+ }
90
+ module = root;
91
+ for (_i = 0, _len = ns.length; _i < _len; _i++) {
92
+ item = ns[_i];
93
+ if (typeof module[item] !== 'undefined') {
94
+ module = module[item];
95
+ }
96
+ }
97
+ return module;
98
+ },
99
+ initializeModule: function(nameOrModule) {
100
+ var module;
101
+ module = typeof nameOrModule === "string" ? AutoexecBat.findModule(nameOrModule) : nameOrModule;
102
+ if (typeof module !== 'undefined' && typeof module.init !== 'undefined') {
103
+ return module.init();
104
+ }
105
+ },
106
+ run: function(name) {
107
+ var module;
108
+ module = AutoexecBat.findModule(name);
109
+ return AutoexecBat.initializeModule(module);
110
+ }
111
+ };
112
+
113
+ root.AutoexecBat = AutoexecBat;
114
+
115
+ root.define = AutoexecBat.define;
116
+
117
+ root.require = AutoexecBat.require;
118
+
119
+ root.namespace = AutoexecBat.namespace;
120
+
121
+ if (typeof jQuery !== 'undefined') {
122
+ $ = jQuery;
123
+ $.fn.extend({
124
+ autoexec: function(options) {
125
+ return this.each(function() {
126
+ return AutoexecBat.run($(this).data('autoexec'));
127
+ });
128
+ }
129
+ });
130
+ }
131
+
132
+ }).call(this);
@@ -0,0 +1,78 @@
1
+ root = exports ? window
2
+
3
+ AutoexecBat =
4
+ topLevel: "App"
5
+ debug: true
6
+ # autoRequire: null
7
+
8
+ log: (msg) ->
9
+ console?.log msg if AutoexecBat.debug
10
+
11
+ define: (name) ->
12
+ dependencies = []
13
+ dependencies.push AutoexecBat.autoRequire if AutoexecBat.autoRequire && name != AutoexecBat.autoRequire
14
+
15
+ for arg in arguments
16
+ if typeof arg is "object"
17
+ for module in arg
18
+ dependencies.push module unless module in dependencies && module != name
19
+
20
+ else if typeof arg is "function"
21
+ block = arg
22
+
23
+ block ?= -> # empty function
24
+ top = AutoexecBat.topLevel
25
+ target = AutoexecBat.namespace name
26
+
27
+ target.name = name
28
+ target.loaded = false
29
+ target.autoexec = -> # make sure the autoexec function exists
30
+ target.dependencies = dependencies
31
+ target.init = ->
32
+ unless @loaded
33
+ require dependencies
34
+ @autoexec() if typeof @autoexec is 'function'
35
+ @autoexec = -> AutoexecBat.log "Module already initialized"
36
+ @loaded = true
37
+
38
+ block target, top
39
+
40
+ require: (dependencies) ->
41
+ (AutoexecBat.initializeModule(lib) for lib in dependencies) if dependencies
42
+
43
+ namespace: (name) ->
44
+ target = root
45
+ target = target[item] or= {} for item in name.split '.'
46
+ target
47
+
48
+ findModule: (ns) ->
49
+ return unless ns
50
+ ns = ns.split "." if typeof ns is "string"
51
+ ns.unshift AutoexecBat.topLevel unless ns[0] == AutoexecBat.topLevel
52
+
53
+ module = root
54
+ for item in ns
55
+ module = module[item] unless typeof module[item] is 'undefined'
56
+ module
57
+
58
+ initializeModule: (nameOrModule) ->
59
+ module = if typeof nameOrModule is "string" then AutoexecBat.findModule(nameOrModule) else nameOrModule
60
+ module.init() if typeof module isnt 'undefined' and typeof module.init isnt 'undefined'
61
+
62
+ run: (name) ->
63
+ module = AutoexecBat.findModule name
64
+ AutoexecBat.initializeModule module
65
+
66
+
67
+ # Globals
68
+ root.AutoexecBat = AutoexecBat
69
+ root.define = AutoexecBat.define
70
+ root.require = AutoexecBat.require
71
+ root.namespace = AutoexecBat.namespace
72
+
73
+ # Plugins
74
+ unless typeof jQuery is 'undefined'
75
+ $ = jQuery
76
+ $.fn.extend
77
+ autoexec: (options) ->
78
+ return @each -> AutoexecBat.run $(@).data('autoexec')
@@ -0,0 +1,16 @@
1
+ module AutoexecBat
2
+ module Helper
3
+
4
+ def default_autoexec_module
5
+ candidates = controller.controller_path.split('/') << controller.action_name
6
+ # candidates.unshift "shared" if candidates.length == 2
7
+ candidates.map &:camelcase
8
+ end
9
+
10
+ def autoexec_module(name=nil)
11
+ @autoexec_module = name if name
12
+ @autoexec_module || default_autoexec_module
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module AutoexecBat
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "autoexec_bat/version"
2
+ require "autoexec_bat/helper"
3
+
4
+ module AutoexecBat
5
+
6
+ module Rails
7
+ class Engine < ::Rails::Engine
8
+ ActionView::Base.send :include, AutoexecBat::Helper
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoexec_bat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gudleik Rasch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Autoexecution of javascript based on data attribute
15
+ email:
16
+ - gudleik@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - autoexec_bat.gemspec
27
+ - autoexec_bat.js
28
+ - lib/assets/javascripts/autoexec_bat.coffee
29
+ - lib/autoexec_bat.rb
30
+ - lib/autoexec_bat/helper.rb
31
+ - lib/autoexec_bat/version.rb
32
+ homepage: https://github.com/Skalar/autoexec_bat
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ segments:
45
+ - 0
46
+ hash: 1279658281685689202
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: 1279658281685689202
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.21
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Autoexecution of javascript based on data attribute
62
+ test_files: []