js_vars 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: 5f71b2c15cc0201d4ffcbdc17171abc365ee5c6d
4
+ data.tar.gz: 5aff28da3fa50d61a2fae6b3ea44861af14039cc
5
+ SHA512:
6
+ metadata.gz: 0f3868ce7bbb482d4a5cd18c07230b6051953cb7e5f941c448cecc09adad450adf87b94f58ac4fd3356ca724f298666f076348b8ed7af872e26c04a853559341
7
+ data.tar.gz: 133c2d5be7efa9cfbf4c6f09fb6202d580f82cd99364c305350cddb17801ca69e3643dcc18dd7e2b0aaffbdf22e265814e29ed8dad71e0d8377e9dadbefbec68
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 jsvars.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jiren Patel
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,78 @@
1
+ # JsVars
2
+
3
+ It is view helper to render javascript variables in one place with namespaces.
4
+
5
+ How to add js variables. `@user.to_jsvar(:user)` this will add `@user` json value to variable map and it is render by `js_vars_tag` in view
6
+
7
+ i.e
8
+ APP = { user: {name: 'jiren', id: 1}, address: {city: 'pune'} }
9
+
10
+ ### Why?
11
+
12
+ Many times we are using `javascript_tag` or `:javascript` to render javascript variables or json data in view.
13
+
14
+ i.e
15
+
16
+ :javascript
17
+ var users = #{@users.to_json}
18
+
19
+ OR
20
+
21
+ javascript_tag do
22
+ var users = #{@users.to_json}
23
+ end
24
+
25
+ So here there are no namespace for javascript variables i.e `var APP = {users: [....]}` and they are global. May be there are chances to conflict with other variables with same name. Also in debugging we find difficulties to find variable because variables are not define in namespace. If there are namespace so we can view all variables in namespace from debugging console.
26
+
27
+ To solve this problems I have written gem `jsvars`.
28
+
29
+ ## Usage
30
+
31
+ - Add any object to js var map
32
+
33
+ @users.to_jsvars(:users)
34
+ @address.to_jsvars(:address)
35
+
36
+ - For rendering all added variables in html. Add following tag to end of the layout.
37
+
38
+ = js_vars_tag
39
+
40
+ OR
41
+
42
+ <%= js_vars_tag %>
43
+
44
+ Output in html
45
+
46
+ <script jsvars="jsvars">//<![CDATA[ if(!window.APP){ window.APP = {}};APP.users = [1,2,3]; window.address = 'pune'//]]></script>
47
+
48
+
49
+ Here `APP` is a namespace which hold `users` and `address` variables.
50
+
51
+ - `to_jsvars` options
52
+
53
+ @users.to_jsvars(:users, namespace: 'User', json: { only: [:id]})
54
+
55
+ namespace: To change particular variable namespace
56
+ json: To change rendering object fields.
57
+
58
+ ## Installation
59
+
60
+ Add this line to your application's Gemfile:
61
+
62
+ gem 'jsvars'
63
+
64
+ And then execute:
65
+
66
+ $ bundle
67
+
68
+ Or install it yourself as:
69
+
70
+ $ gem install jsvars
71
+
72
+ ### Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/js_vars.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'js_vars/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "js_vars"
8
+ spec.version = JsVars::VERSION
9
+ spec.authors = ["Jiren Patel"]
10
+ spec.email = ["jirenpatel@gmail.com"]
11
+ spec.description = %q{Manage javascript variables in html templates}
12
+ spec.summary = %q{Manage javascript variables in html templates}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,14 @@
1
+ module JsVars
2
+ module ObjExt
3
+
4
+ def to_jsvar(var_name, options = {})
5
+ JsVars.add(
6
+ {
7
+ var_name => self.to_json(options[:json] || {} )
8
+ },
9
+ options[:namespace]
10
+ )
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module JsVars
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ module JsVars
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ initializer 'js_vars.view_helpers' do
5
+ ActionView::Base.send(:include, JsVars::TagHelper)
6
+ end
7
+
8
+ =begin
9
+ config.before_configuration do
10
+ if config.action_view.javascript_expansions
11
+ config.action_view.javascript_expansions[:defaults] << ['js_vars']
12
+ end
13
+ end
14
+ =end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ require 'js_vars/rails/engine' if ::Rails.version >= '3.1'
2
+ require 'js_vars/rails/railtie'
@@ -0,0 +1,23 @@
1
+ module JsVars
2
+ module TagHelper
3
+
4
+ def js_vars_tag(default_namespace = 'APP')
5
+ script_data = ''
6
+
7
+ JsVars::VarMap.get.each do |namespace, vars|
8
+ namespace ||= default_namespace
9
+ script_data += "if(!window.#{namespace}){ window.#{namespace} = {}};"
10
+ script_data = vars.inject(script_data) {|s, (k, v)| s += "#{namespace}.#{k} = #{v.is_a?(String) ? v : v.to_json};" } if vars
11
+ end
12
+
13
+ return '' if JsVars::VarMap.empty?
14
+
15
+ JsVars::VarMap.clear
16
+
17
+ return script_data.html_safe if request.xhr?
18
+
19
+ raw javascript_tag(jsvars: :jsvars){ script_data.html_safe }
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ module JsVars
2
+ class VarMap < Hash
3
+
4
+ class << self
5
+
6
+ def get
7
+ Thread.current['[jsvar]:map'] ||= JsVars::VarMap.new
8
+ end
9
+
10
+ def add(vars, namepsace)
11
+ var_map = self.get
12
+ var_map[namepsace] ||= {}
13
+ var_map[namepsace].merge!(vars)
14
+ end
15
+
16
+ def clear
17
+ self.get.clear
18
+ end
19
+
20
+ def empty?
21
+ self.get.empty?
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module JsVars
2
+ VERSION = "0.0.1"
3
+ end
data/lib/js_vars.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+ require 'js_vars/version'
3
+ require 'js_vars/tag_helper'
4
+ require 'js_vars/var_map'
5
+ require 'js_vars/obj_ext'
6
+ require 'js_vars/rails'
7
+
8
+ module JsVars
9
+
10
+ class << self
11
+
12
+ def add(vars, namepsace = 'APP')
13
+ VarMap.add(vars, namepsace) if vars.is_a?(Hash)
14
+ end
15
+
16
+ def clear
17
+ VarMap.clear
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ Object.send(:include, JsVars::ObjExt) unless Object.respond_to?(:to_jsvar)
@@ -0,0 +1,17 @@
1
+
2
+ (function(w) {
3
+
4
+ if ( w.jsVars !== undefined ) {
5
+ return false;
6
+ }
7
+
8
+ var jsVars;
9
+ jsVars = {
10
+ add: function(vars, options){
11
+
12
+ }
13
+ }
14
+
15
+ w.jsVars = jsVars;
16
+
17
+ })(window);
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js_vars
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jiren Patel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-17 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Manage javascript variables in html templates
42
+ email:
43
+ - jirenpatel@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - js_vars.gemspec
54
+ - lib/js_vars.rb
55
+ - lib/js_vars/obj_ext.rb
56
+ - lib/js_vars/rails.rb
57
+ - lib/js_vars/rails/engine.rb
58
+ - lib/js_vars/rails/railtie.rb
59
+ - lib/js_vars/tag_helper.rb
60
+ - lib/js_vars/var_map.rb
61
+ - lib/js_vars/version.rb
62
+ - vendor/assets/javascripts/js_vars.js
63
+ homepage: ''
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.0
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Manage javascript variables in html templates
87
+ test_files: []