client_variable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/client_variable.gemspec +24 -0
- data/lib/client_variable/global.rb +21 -0
- data/lib/client_variable/helper/controller.rb +26 -0
- data/lib/client_variable/helper/view.rb +50 -0
- data/lib/client_variable/request.rb +30 -0
- data/lib/client_variable/version.rb +3 -0
- data/lib/client_variable.rb +50 -0
- data/lib/generators/client_variable/install/install_generator.rb +13 -0
- data/lib/generators/client_variable/install/templates/config.yml +5 -0
- data/vendor/assets/javascripts/variable.js.erb +44 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04b108517497bd64e9c3e19151c98b4f428bb5b5
|
4
|
+
data.tar.gz: 7b976422e928a650392a40606537885bcb7e09b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 323eb3e245c79a985a685130070a930434e230c5360c1d0f32792028b73b09fff468c737df84d0faf7a218b14e7359bffc8307909fdaab79cdb79ce9b93bda94
|
7
|
+
data.tar.gz: 9e7b71aa97e6382596517f341d351188a615675747c69ce939bd274c4fd0ce6525039be2c81b80338571caa54e4953b63940a39a113f265710706cd477bc9790
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 MQuy
|
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,29 @@
|
|
1
|
+
# ClientVariable
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'client_variable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install client_variable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'client_variable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "client_variable"
|
8
|
+
spec.version = ClientVariable::VERSION
|
9
|
+
spec.authors = ["MQuy"]
|
10
|
+
spec.email = ["sugiacupit@gmail.com"]
|
11
|
+
spec.description = %q{export variable from rails}
|
12
|
+
spec.summary = %q{variable rails in client}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.rubyforge_project = "client_variable"
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Client
|
2
|
+
class Global < Variable
|
3
|
+
class << self
|
4
|
+
def values
|
5
|
+
@datum ||= {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def clear
|
9
|
+
@datum = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_variable(name)
|
13
|
+
values[name]
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_variable(name, value)
|
17
|
+
values[name] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Client
|
2
|
+
module Helpers
|
3
|
+
module Instance
|
4
|
+
def self.included base
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
def client
|
10
|
+
if new_request?
|
11
|
+
Request.id = request.object_id
|
12
|
+
Request.env = request.env
|
13
|
+
end
|
14
|
+
Variable
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def new_request?
|
19
|
+
Request.env.blank? || Request.id != request.object_id
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ActionController::Base.send :include, Client::Helpers::Instance
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Client
|
2
|
+
module Helpers
|
3
|
+
module View
|
4
|
+
extend ActionView::Helpers::JavaScriptHelper
|
5
|
+
extend ActionView::Helpers::TagHelper
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.send(:include, InstanceMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def include_variable
|
13
|
+
render_data
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def render_data
|
18
|
+
script = "window.rails = {};"
|
19
|
+
script << formatted_data
|
20
|
+
script = javascript_tag(script)
|
21
|
+
script.html_safe
|
22
|
+
end
|
23
|
+
|
24
|
+
def formatted_data
|
25
|
+
script = ''
|
26
|
+
values.each do |key, val|
|
27
|
+
js_key = key.to_s.camelize(:lower)
|
28
|
+
script << "rails.#{js_key}=#{normalize(val).to_json};"
|
29
|
+
end
|
30
|
+
script
|
31
|
+
end
|
32
|
+
|
33
|
+
def normalize(value, depth=0)
|
34
|
+
return value if depth > 1000
|
35
|
+
return value unless value.is_a? Hash
|
36
|
+
|
37
|
+
Hash[value.map { |k, v|
|
38
|
+
[ k.to_s.camelize(:lower), normalize(v, depth + 1) ]
|
39
|
+
}]
|
40
|
+
end
|
41
|
+
|
42
|
+
def values
|
43
|
+
[Global.values, Request.values].inject(:merge)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
ActionView::Base.send :include, Client::Helpers::View
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Client
|
2
|
+
class Request
|
3
|
+
class << self
|
4
|
+
def env
|
5
|
+
@request_env if defined? @request_env
|
6
|
+
end
|
7
|
+
|
8
|
+
def env=(environment)
|
9
|
+
@request_env = environment
|
10
|
+
@request_env['client'] ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def id
|
14
|
+
@request_id if defined? @request_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def id=(request_id)
|
18
|
+
@request_id = request_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def values
|
22
|
+
env['client'] if env
|
23
|
+
end
|
24
|
+
|
25
|
+
def clear
|
26
|
+
env && (env['client'] = {})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "client_variable/version"
|
2
|
+
|
3
|
+
module Client
|
4
|
+
class Variable
|
5
|
+
class << self
|
6
|
+
def global
|
7
|
+
Global
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_variable(name)
|
11
|
+
Request.values[name]
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_variable(name, value)
|
15
|
+
Request.values[name] = value
|
16
|
+
end
|
17
|
+
|
18
|
+
def values
|
19
|
+
Request.values
|
20
|
+
end
|
21
|
+
|
22
|
+
def clear
|
23
|
+
Request.clear
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(method, *args, &block)
|
27
|
+
if ( method.to_s =~ /=$/ )
|
28
|
+
if public_methods? method
|
29
|
+
raise "You can't use Gon public methods for storing data"
|
30
|
+
end
|
31
|
+
set_variable(method.to_s.delete('='), args[0])
|
32
|
+
else
|
33
|
+
get_variable(method.to_s)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.generate
|
40
|
+
data = YAML.load(ERB.new(File.new("#{Rails.root}/config/client_variable.yml").read).result)[Rails.env]
|
41
|
+
data.merge([Global.values, Request.values].inject(:merge)).to_json
|
42
|
+
end
|
43
|
+
|
44
|
+
class Engine < ::Rails::Engine; end
|
45
|
+
|
46
|
+
require 'client_variable/request'
|
47
|
+
require 'client_variable/global'
|
48
|
+
require 'client_variable/helper/view'
|
49
|
+
require 'client_variable/helper/controller'
|
50
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ClientVariable
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
desc "Generates a variable config"
|
7
|
+
|
8
|
+
def create_config_file
|
9
|
+
template "config.yml", "config/client_variable.yml"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
rails = (function(exports, global) {
|
2
|
+
var datum = deepMerge(exports, <%= Client.generate%>);
|
3
|
+
var defaultSeparator = '.';
|
4
|
+
|
5
|
+
function lookup(query, track) {
|
6
|
+
var parts = separateQuery(query),
|
7
|
+
part = null,
|
8
|
+
messages = datum;
|
9
|
+
while(messages) {
|
10
|
+
part = parts.shift();
|
11
|
+
if (parts.length == 0) break;
|
12
|
+
if (track && !messages[parts]) messages[part] = {};
|
13
|
+
messages = messages[part];
|
14
|
+
}
|
15
|
+
return messages;
|
16
|
+
}
|
17
|
+
|
18
|
+
function deepMerge(target, source) {
|
19
|
+
for(var key in source) {
|
20
|
+
if (source.hasOwnProperty(key)) {
|
21
|
+
target[key] = source[key];
|
22
|
+
}
|
23
|
+
}
|
24
|
+
return target;
|
25
|
+
}
|
26
|
+
|
27
|
+
function separateQuery(query, n) {
|
28
|
+
var parts = query.split(defaultSeparator);
|
29
|
+
if (n < 0) n += parts.length
|
30
|
+
return n ? parts[n] : parts;
|
31
|
+
}
|
32
|
+
|
33
|
+
return {
|
34
|
+
set: function(name, value) {
|
35
|
+
var parent = lookup(name, true);
|
36
|
+
if (parent) parent[separateQuery(name, -1)] = value;
|
37
|
+
},
|
38
|
+
get: function(name) {
|
39
|
+
var parent = lookup(name);
|
40
|
+
return (parent = lookup(name)) ? parent[separateQuery(name, -1)] : parent;
|
41
|
+
},
|
42
|
+
values: datum
|
43
|
+
}
|
44
|
+
}(rails || {}, this));
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: client_variable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MQuy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-14 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: export variable from rails
|
42
|
+
email:
|
43
|
+
- sugiacupit@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
|
+
- client_variable.gemspec
|
54
|
+
- lib/client_variable.rb
|
55
|
+
- lib/client_variable/global.rb
|
56
|
+
- lib/client_variable/helper/controller.rb
|
57
|
+
- lib/client_variable/helper/view.rb
|
58
|
+
- lib/client_variable/request.rb
|
59
|
+
- lib/client_variable/version.rb
|
60
|
+
- lib/generators/client_variable/install/install_generator.rb
|
61
|
+
- lib/generators/client_variable/install/templates/config.yml
|
62
|
+
- vendor/assets/javascripts/variable.js.erb
|
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: client_variable
|
83
|
+
rubygems_version: 2.1.3
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: variable rails in client
|
87
|
+
test_files: []
|