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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6c0afe73bb5006a4242503bedf3153e7308341b
4
- data.tar.gz: 01e9211a7451cc07a5a7c5fc5885ffe12ec0dfa2
3
+ metadata.gz: 60144bb6373f11ad296141423be6729b85b9c25b
4
+ data.tar.gz: a449a3ff9ba3355782b6d88668d41d000fb7af27
5
5
  SHA512:
6
- metadata.gz: 82a5bdb033ed02445c2d4dd749163c392545cbe06c18238409d9d94b00231afe5061cba60df2928cffd85eb8cf4519a7869d207db55cac5192ca1a61c0cbce3a
7
- data.tar.gz: d79011fa3e6eb08834aeb4bab2f2ef9b64ed2b240ac925f3e79b6a858e3ed251e97666fe4e174129e4f28cf4d85bcb7681ee95346bc76105765989cbfe4e1d3e
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
- gem 'coveralls', :require => false
9
- gem 'rack-test'
10
- gem 'rspec'
11
- gem 'rspec-rails'
12
- gem 'simplecov', :require => false
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
- gem 'debugger'
17
- gem 'pry'
18
- gem 'pry-debugger'
19
- gem 'pry-stack_explorer'
20
- gem 'pry-rails'
21
- gem 'pry-remote'
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
+ ```
@@ -2,62 +2,66 @@ require 'client_variable/version'
2
2
  require 'rails/engine'
3
3
 
4
4
  module ClientVariable
5
- class Variable
6
- class << self
7
- def global
8
- Global
9
- end
10
-
11
- def get_variable(name)
12
- Request.values[name]
13
- end
14
-
15
- def set_variable(name, value)
16
- Request.values[name] = value
17
- end
18
-
19
- def values
20
- Request.values
21
- end
22
-
23
- def clear
24
- Request.clear
25
- end
26
-
27
- def push(data = {})
28
- raise "Object must have each_pair method" unless data.respond_to? :each_pair
29
-
30
- data.each_pair do |name, value|
31
- set_variable(name.to_s, value)
32
- end
33
- end
34
-
35
- def method_missing(method, *args, &block)
36
- if ( method.to_s =~ /=$/ )
37
- if public_methods? method
38
- raise "You can't use Gon public methods for storing data"
39
- end
40
- set_variable(method.to_s.delete('='), args[0])
41
- else
42
- get_variable(method.to_s)
43
- end
44
- end
45
- end
46
- end
47
-
48
- def self.generate
49
- path = "#{Rails.root}/config/client_variable.yml"
50
- if File.exist?(path)
51
- data = YAML.load(ERB.new(File.new("#{Rails.root}/config/client_variable.yml").read).result)
52
- config = data[Rails.env]
53
- end
54
- config || {}
55
- end
56
-
57
- class Engine < Rails::Engine; end
58
-
59
- require 'client_variable/request'
60
- require 'client_variable/global'
61
- require 'client_variable/helper/view'
62
- require 'client_variable/helper/controller'
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
- class Global < Variable
3
- class << self
4
- def values
5
- @datum ||= {}
6
- end
2
+ class Global < Variable
3
+ class << self
4
+ def values
5
+ @datum ||= {}
6
+ end
7
7
 
8
- def clear
9
- @datum = {}
10
- end
8
+ def clear
9
+ @datum = {}
10
+ end
11
11
 
12
- def get_variable(name)
13
- values[name]
14
- end
12
+ def get_variable(name)
13
+ values[name]
14
+ end
15
15
 
16
- def set_variable(name, value)
17
- values[name] = value
18
- end
19
- end
20
- end
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
- script = "window.rails = {};"
22
- script << formatted_data
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
- Hash[value.map { |k, v|
41
- [ k.to_s.camelize(:lower), normalize(v, depth + 1) ]
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
- 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
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
- class TiltHandlebars < Tilt::Template
12
- def self.default_mime_type
13
- 'application/javascript'
14
- end
11
+ class TiltHandlebars < Tilt::Template
12
+ def self.default_mime_type
13
+ 'application/javascript'
14
+ end
15
15
 
16
- def initialize_engine; end
16
+ def initialize_engine; end
17
17
 
18
- def prepare; end
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
- binding.pry
25
- template = data.dup
26
- template.gsub!(/"/, '\\"')
24
+ binding.pry
25
+ template = data.dup
26
+ template.gsub!(/"/, '\\"')
27
27
  template.gsub!(/\r?\n/, '\\n')
28
28
  template.gsub!(/\t/, '\\t')
29
- return 'console.log(1223);'
29
+ return 'console.log(1223);'
30
30
  end
31
31
  end
32
32
  end
@@ -1,9 +1,9 @@
1
1
  require "action_controller/railtie"
2
2
 
3
3
  module ClientVariable
4
- class Railtie < ::Rails::Railtie
5
- rake_tasks do
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
6
  load "client_variable/rails/task.rake"
7
7
  end
8
- end
8
+ end
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module ClientVariable
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,4 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Client do
3
+ describe ClientVariable do
4
+ describe '.generate' do
5
+ it 'should be type of hash' do
6
+ Rails.stub(:root) { ''}
7
+ expect(ClientVariable.generate).to be_kind_of(Hash)
8
+ end
9
+ end
4
10
  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 == 0) break;
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.7
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: 2013-10-14 00:00:00.000000000 Z
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.1.3
89
+ rubygems_version: 2.2.2
90
90
  signing_key:
91
91
  specification_version: 4
92
92
  summary: variable rails in client