client_variable 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +13 -12
- data/README.md +9 -0
- data/lib/client_variable.rb +62 -58
- data/lib/client_variable/global.rb +16 -16
- data/lib/client_variable/helper/view.rb +21 -13
- data/lib/client_variable/rails/engine.rb +15 -15
- data/lib/client_variable/rails/railtie.rb +3 -3
- data/lib/client_variable/version.rb +1 -1
- data/spec/client_variable_spec.rb +7 -1
- data/vendor/assets/javascripts/variable.js +3 -12
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60144bb6373f11ad296141423be6729b85b9c25b
|
4
|
+
data.tar.gz: a449a3ff9ba3355782b6d88668d41d000fb7af27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a66ba9019a28aed36c14aa08f025999b7d276437de09d41d7e1faa0804128701bbabc6000b5543ef5f5b6031b6b9e1603115a01b1e2eacd30019eb5dd284d780
|
7
|
+
data.tar.gz: 0f0d7eedaff8b858376e9a3402ebbec10ea3d53998d94d70bf788c469d4139409784253705b48b5a03a7dc8dd972caa674ad574986cba52ace5a8e67390403eb
|
data/Gemfile
CHANGED
@@ -2,21 +2,22 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
gem 'rails'
|
5
|
+
gem 'rails', '~> 3.0.0'
|
6
6
|
|
7
7
|
group :test do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
gem 'coveralls', :require => false
|
9
|
+
gem 'rack-test'
|
10
|
+
gem 'rspec'
|
11
|
+
gem 'rspec-rails'
|
12
|
+
gem 'simplecov', :require => false
|
13
|
+
gem 'mocha'
|
13
14
|
end
|
14
15
|
|
15
16
|
group :development, :test do
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
gem 'debugger'
|
18
|
+
gem 'pry'
|
19
|
+
gem 'pry-debugger'
|
20
|
+
gem 'pry-stack_explorer'
|
21
|
+
gem 'pry-rails'
|
22
|
+
gem 'pry-remote'
|
22
23
|
end
|
data/README.md
CHANGED
@@ -68,3 +68,12 @@ rails.get('user_id') // => 1
|
|
68
68
|
rails.set('a.b.c.d', '123') // rails.values => {a: {b: {c: {d: '123'}}}}
|
69
69
|
rails.get('a.b.c.d') // => '123'
|
70
70
|
```
|
71
|
+
|
72
|
+
### Options
|
73
|
+
|
74
|
+
#### :camel_case
|
75
|
+
|
76
|
+
``` erb
|
77
|
+
include_variable(:camel_case => true) // => rails.values => {appName: 'Hello World'}
|
78
|
+
include_variable(:camel_case => false) // => rails.values => {app_name: 'Hello World'}
|
79
|
+
```
|
data/lib/client_variable.rb
CHANGED
@@ -2,62 +2,66 @@ require 'client_variable/version'
|
|
2
2
|
require 'rails/engine'
|
3
3
|
|
4
4
|
module ClientVariable
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
5
|
+
def self.❨╯°□°❩╯︵┻━┻
|
6
|
+
puts "Calm down, bro"
|
7
|
+
end
|
8
|
+
|
9
|
+
class Variable
|
10
|
+
class << self
|
11
|
+
def global
|
12
|
+
Global
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_variable(name)
|
16
|
+
Request.values[name]
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_variable(name, value)
|
20
|
+
Request.values[name] = value
|
21
|
+
end
|
22
|
+
|
23
|
+
def values
|
24
|
+
Request.values
|
25
|
+
end
|
26
|
+
|
27
|
+
def clear
|
28
|
+
Request.clear
|
29
|
+
end
|
30
|
+
|
31
|
+
def push(data = {})
|
32
|
+
raise "Object must have each_pair method" unless data.respond_to? :each_pair
|
33
|
+
|
34
|
+
data.each_pair do |name, value|
|
35
|
+
set_variable(name.to_s, value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing(method, *args, &block)
|
40
|
+
if ( method.to_s =~ /=$/ )
|
41
|
+
if public_methods? method
|
42
|
+
raise "You can't use Gon public methods for storing data"
|
43
|
+
end
|
44
|
+
set_variable(method.to_s.delete('='), args[0])
|
45
|
+
else
|
46
|
+
get_variable(method.to_s)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.generate
|
53
|
+
path = "#{Rails.root}/config/client_variable.yml"
|
54
|
+
if File.exist?(path)
|
55
|
+
data = YAML.load(ERB.new(File.new(path).read).result)
|
56
|
+
config = data[Rails.env]
|
57
|
+
end
|
58
|
+
config || {}
|
59
|
+
end
|
60
|
+
|
61
|
+
class Engine < Rails::Engine; end
|
62
|
+
|
63
|
+
require 'client_variable/request'
|
64
|
+
require 'client_variable/global'
|
65
|
+
require 'client_variable/helper/view'
|
66
|
+
require 'client_variable/helper/controller'
|
63
67
|
end
|
@@ -1,21 +1,21 @@
|
|
1
1
|
module ClientVariable
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
class Global < Variable
|
3
|
+
class << self
|
4
|
+
def values
|
5
|
+
@datum ||= {}
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def clear
|
9
|
+
@datum = {}
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def get_variable(name)
|
13
|
+
values[name]
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
def set_variable(name, value)
|
17
|
+
values[name] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
21
|
end
|
@@ -12,34 +12,42 @@ module ClientVariable
|
|
12
12
|
end
|
13
13
|
|
14
14
|
module InstanceMethods
|
15
|
-
def include_variable
|
16
|
-
render_data
|
15
|
+
def include_variable(opts={})
|
16
|
+
render_data(opts)
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
-
def render_data
|
21
|
-
|
22
|
-
|
20
|
+
def render_data(opts)
|
21
|
+
camel_case = opts[:camel_case].nil? ? true : opts[:camel_case]
|
22
|
+
|
23
|
+
script = "window.rails = {};"
|
24
|
+
script << formatted_data(camel_case)
|
23
25
|
script = javascript_tag(script)
|
24
26
|
script.html_safe
|
25
27
|
end
|
26
28
|
|
27
|
-
def formatted_data
|
29
|
+
def formatted_data(camel_case)
|
28
30
|
script = ''
|
29
31
|
values.each do |key, val|
|
30
|
-
js_key = key.to_s.camelize(:lower)
|
31
|
-
script << "rails.#{js_key}=#{normalize(val).to_json};"
|
32
|
+
js_key = camel_case ? key.to_s.camelize(:lower) : key.to_s
|
33
|
+
script << "rails.#{js_key}=#{normalize(val, camel_case).to_json};"
|
32
34
|
end
|
33
35
|
script
|
34
36
|
end
|
35
37
|
|
36
|
-
def normalize(value, depth=0)
|
38
|
+
def normalize(value, camel_case, depth=0)
|
37
39
|
return value if depth > 1000
|
38
|
-
return value unless value.is_a? Hash
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
case value
|
42
|
+
when Hash
|
43
|
+
Hash[value.map { |k, v|
|
44
|
+
[ camel_case ? k.to_s.camelize(:lower) : k.to_s, normalize(v, camel_case, depth + 1) ]
|
45
|
+
}]
|
46
|
+
when Enumerable
|
47
|
+
value.map { |v| normalize(v, camel_case, depth) }
|
48
|
+
else
|
49
|
+
value
|
50
|
+
end
|
43
51
|
end
|
44
52
|
|
45
53
|
def values
|
@@ -2,31 +2,31 @@ require 'sprockets/engines'
|
|
2
2
|
require 'tilt/template'
|
3
3
|
|
4
4
|
module ClientVariable
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
class Engine < Rails::Engine
|
6
|
+
initializer "sprockets.mquy", :after => "sprockets.environment", :group => :all do |app|
|
7
|
+
app.assets.register_engine('.mquy', TiltHandlebars)
|
8
|
+
end
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
class TiltHandlebars < Tilt::Template
|
12
|
+
def self.default_mime_type
|
13
|
+
'application/javascript'
|
14
|
+
end
|
15
15
|
|
16
|
-
|
16
|
+
def initialize_engine; end
|
17
17
|
|
18
|
-
|
18
|
+
def prepare; end
|
19
19
|
|
20
20
|
# Generates Javascript code from a HandlebarsJS template.
|
21
21
|
# The SC template name is derived from the lowercase logical asset path
|
22
22
|
# by replacing non-alphanum characheters by underscores.
|
23
23
|
def evaluate(scope, locals, &block)
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
binding.pry
|
25
|
+
template = data.dup
|
26
|
+
template.gsub!(/"/, '\\"')
|
27
27
|
template.gsub!(/\r?\n/, '\\n')
|
28
28
|
template.gsub!(/\t/, '\\t')
|
29
|
-
|
29
|
+
return 'console.log(1223);'
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -10,25 +10,16 @@ rails = (function(exports, global) {
|
|
10
10
|
messages = datum;
|
11
11
|
while(messages) {
|
12
12
|
part = parts.shift();
|
13
|
-
if (parts.length
|
13
|
+
if (!parts.length) break;
|
14
14
|
if (track && !messages[parts]) messages[part] = {};
|
15
15
|
messages = messages[part];
|
16
16
|
}
|
17
17
|
return messages;
|
18
18
|
}
|
19
19
|
|
20
|
-
function deepMerge(target, source) {
|
21
|
-
for(var key in source) {
|
22
|
-
if (source.hasOwnProperty(key)) {
|
23
|
-
target[key] = source[key];
|
24
|
-
}
|
25
|
-
}
|
26
|
-
return target;
|
27
|
-
}
|
28
|
-
|
29
20
|
function separateQuery(query, n) {
|
30
21
|
var parts = query.split(defaultSeparator);
|
31
|
-
if (n < 0) n += parts.length
|
22
|
+
if (n < 0) { n += parts.length; }
|
32
23
|
return n ? parts[n] : parts;
|
33
24
|
}
|
34
25
|
|
@@ -42,5 +33,5 @@ rails = (function(exports, global) {
|
|
42
33
|
return (parent = lookup(name)) ? parent[separateQuery(name, -1)] : parent;
|
43
34
|
},
|
44
35
|
values: datum
|
45
|
-
}
|
36
|
+
};
|
46
37
|
}(typeof rails === 'undefined' ? {} : rails, this));
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: client_variable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MQuy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: export variable in rails to client-side
|
@@ -45,8 +45,8 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
- .gitignore
|
49
|
-
- .rspec
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
50
|
- Gemfile
|
51
51
|
- LICENSE.txt
|
52
52
|
- README.md
|
@@ -76,17 +76,17 @@ require_paths:
|
|
76
76
|
- lib
|
77
77
|
required_ruby_version: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- -
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
|
-
- -
|
84
|
+
- - ">="
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project: client_variable
|
89
|
-
rubygems_version: 2.
|
89
|
+
rubygems_version: 2.2.2
|
90
90
|
signing_key:
|
91
91
|
specification_version: 4
|
92
92
|
summary: variable rails in client
|