gon 5.2.0 → 5.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gon might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6490edc958229669a41401931a59c79cbb3826f
4
- data.tar.gz: 9e0337987644e7f2bd4c80456a1a69da673ad640
3
+ metadata.gz: df53612edac96aa34a023ad579922c66556c733f
4
+ data.tar.gz: d9edd380ed9e0f5f8175b8e0e1512b299f672d59
5
5
  SHA512:
6
- metadata.gz: ba65303d31b34f7d2b9bad0d775efb210fb0eba67f30e80190639736773e4a773ed072a4276795c3271507b7f08079f99ad8c5f9b0159eb46c496c59eb768a34
7
- data.tar.gz: f543c36fea096b81de1c089fa9af7c1f8845eced67410485b9a345569aa9d181c8551e5a24f6c91866e1a16022b240e24cb1a8ced93b089c2840c2920161eeb8
6
+ metadata.gz: c64f6170b9cd2b31e78cf140de34b9c73b8a2762d5d5f025b7649c2195254552806269b6b4682ec78594c39811ab38355304c09f4007a2fa337cf87b922ddc5d
7
+ data.tar.gz: 15e23d39beaa852fd98ce17ac020e6e1bbbd80943369b8f49b3804f053e5bc2630226def87972a91fff06f647f03536b62ab1d274a335f24d164a50c11293f94
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 5.2.1
4
+
5
+ * fix for jbuilder module. Thanks to @jankovy
6
+ * merge variable feature (for merge hash-like variables instead of overriding them). Thanks to @jalkoby
7
+
3
8
  ## 5.2.0
4
9
 
5
10
  * fix issue where include_gon would raise exception if the controller did not assign any gon variables. Thanks to @asalme
data/lib/gon.rb CHANGED
@@ -54,11 +54,25 @@ class Gon
54
54
  current_gon.gon[name] = value
55
55
  end
56
56
 
57
- def push(data = {})
57
+ def merge_variable(name, value)
58
+ old_value = all_variables[name]
59
+ if value.is_a?(Hash) && old_value.is_a?(Hash)
60
+ value = old_value.deep_merge(value)
61
+ end
62
+ set_variable(name, value)
63
+ end
64
+
65
+ def push(data = {}, merge = false)
58
66
  raise 'Object must have each_pair method' unless data.respond_to? :each_pair
59
67
 
60
- data.each_pair do |name, value|
61
- set_variable(name.to_s, value)
68
+ if merge
69
+ data.each_pair do |name, value|
70
+ merge_variable(name.to_s, value)
71
+ end
72
+ else
73
+ data.each_pair do |name, value|
74
+ set_variable(name.to_s, value)
75
+ end
62
76
  end
63
77
  end
64
78
 
@@ -97,27 +97,20 @@ class Gon
97
97
  end
98
98
 
99
99
  def parse_path(path)
100
- return path if File.exists?(path)
101
-
102
- splitted = path.split('/')
103
- if splitted.size == 2
104
- tmp_path = construct_path(splitted[0], splitted[1])
105
- return tmp_path if tmp_path
106
- elsif splitted.size == 1
107
- tmp_path = construct_path(@_controller_name, splitted[0])
108
- return tmp_path if tmp_path
109
- end
110
-
111
- raise 'Something wrong with partial path in your jbuilder templates'
100
+ return path if File.exists?(path)
101
+ if (splitted = path.split('/')).blank?
102
+ raise 'Something wrong with partial path in your jbuilder templates'
103
+ elsif splitted.size == 1
104
+ splitted.shift(@_controller_name)
105
+ end
106
+ construct_path(splitted)
112
107
  end
113
108
 
114
- def construct_path(part1, part2)
115
- tmp_path = "app/views/#{part1}/_#{part2}"
116
- tmp_path = path_with_ext(tmp_path)
117
- return tmp_path if tmp_path
118
- tmp_path = "app/views/#{part1}/#{part2}"
119
- tmp_path = path_with_ext(tmp_path)
120
- return tmp_path if tmp_path
109
+ def construct_path(args)
110
+ last_arg = args.pop
111
+ tmp_path = 'app/views/' + args.join('/')
112
+ path = path_with_ext(tmp_path + "/_#{last_arg}")
113
+ path || path_with_ext(tmp_path + "/#{last_arg}")
121
114
  end
122
115
 
123
116
  def path_with_ext(path)
@@ -1,3 +1,3 @@
1
1
  class Gon
2
- VERSION = '5.2.0'
2
+ VERSION = '5.2.1'
3
3
  end
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe Gon do
4
4
 
5
5
  before(:each) do
6
+ Gon.clear
6
7
  end
7
8
 
8
9
  describe '#all_variables' do
@@ -16,7 +17,6 @@ describe Gon do
16
17
  end
17
18
 
18
19
  it 'supports all data types' do
19
- Gon.clear
20
20
  Gon.int = 1
21
21
  Gon.float = 1.1
22
22
  Gon.string = 'string'
@@ -27,8 +27,6 @@ describe Gon do
27
27
  end
28
28
 
29
29
  it 'can be filled with dynamic named variables' do
30
- Gon.clear
31
-
32
30
  check = {}
33
31
  3.times do |i|
34
32
  Gon.set_variable("variable#{i}", i)
@@ -39,7 +37,6 @@ describe Gon do
39
37
  end
40
38
 
41
39
  it 'can set and get variable with dynamic name' do
42
- Gon.clear
43
40
  var_name = "variable#{rand}"
44
41
 
45
42
  Gon.set_variable(var_name, 1)
@@ -47,25 +44,49 @@ describe Gon do
47
44
  end
48
45
 
49
46
  it 'can be support new push syntax' do
50
- Gon.clear
51
-
52
47
  Gon.push({ :int => 1, :string => 'string' })
53
48
  expect(Gon.all_variables).to eq({ 'int' => 1, 'string' => 'string' })
54
49
  end
55
50
 
56
51
  it 'push with wrong object' do
57
52
  expect {
58
- Gon.clear
59
53
  Gon.push(String.new('string object'))
60
54
  }.to raise_error('Object must have each_pair method')
61
55
  end
62
56
 
57
+ describe "#merge_variable" do
58
+ it 'deep merges the same key' do
59
+ Gon.merge_variable(:foo, { bar: { tar: 12 }, car: 23 })
60
+ Gon.merge_variable(:foo, { bar: { dar: 21 }, car: 12 })
61
+ expect(Gon.get_variable(:foo)).to eq(bar: { tar: 12, dar: 21 }, car: 12)
62
+ end
63
+
64
+ it 'merges on push with a flag' do
65
+ Gon.push(foo: { bar: 1 })
66
+ Gon.push({ foo: { tar: 1 } }, :merge)
67
+ expect(Gon.get_variable("foo")).to eq(bar: 1, tar: 1)
68
+ end
69
+
70
+ context 'overrides key' do
71
+ specify "the previous value wasn't hash" do
72
+ Gon.merge_variable(:foo, 2)
73
+ Gon.merge_variable(:foo, { a: 1 })
74
+ expect(Gon.get_variable(:foo)).to eq(a: 1)
75
+ end
76
+
77
+ specify "the new value isn't a hash" do
78
+ Gon.merge_variable(:foo, { a: 1 })
79
+ Gon.merge_variable(:foo, 2)
80
+ expect(Gon.get_variable(:foo)).to eq(2)
81
+ end
82
+ end
83
+ end
84
+
63
85
  end
64
86
 
65
87
  describe '#include_gon' do
66
88
 
67
89
  before(:each) do
68
- Gon.clear
69
90
  Gon::Request.
70
91
  instance_variable_set(:@request_id, request.object_id)
71
92
  expect(ActionView::Base.
@@ -261,7 +282,6 @@ describe Gon do
261
282
  describe '#include_gon_amd' do
262
283
 
263
284
  before(:each) do
264
- Gon.clear
265
285
  Gon::Request.
266
286
  instance_variable_set(:@request_id, request.object_id)
267
287
  @base = ActionView::Base.new
@@ -286,7 +306,7 @@ describe Gon do
286
306
  it 'outputs correct js with an integer' do
287
307
  Gon.int = 1
288
308
 
289
- expect(@base.include_gon_amd).to eq( wrap_script(
309
+ expect(@base.include_gon_amd).to eq( wrap_script(
290
310
  'define(\'gon\',[],function(){'+
291
311
  'var gon={};gon[\'int\']=1;return gon;'+
292
312
  '});')
@@ -294,7 +314,7 @@ describe Gon do
294
314
  end
295
315
 
296
316
  it 'outputs correct module name when given a namespace' do
297
- expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script(
317
+ expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script(
298
318
  'define(\'data\',[],function(){'+
299
319
  'var gon={};return gon;'+
300
320
  '});')
@@ -303,7 +323,6 @@ describe Gon do
303
323
  end
304
324
 
305
325
  it 'returns exception if try to set public method as variable' do
306
- Gon.clear
307
326
  expect { Gon.all_variables = 123 }.to raise_error
308
327
  expect { Gon.rabl = 123 }.to raise_error
309
328
  end
@@ -1,4 +1,13 @@
1
1
  require 'gon'
2
+
3
+ # We don't require rails for specs, but jbuilder works only in rails.
4
+ # And it checks version of rails. I've decided to configure jbuilder for rails v4
5
+ module Rails
6
+ module VERSION
7
+ MAJOR = 4
8
+ end
9
+ end
10
+
2
11
  require 'jbuilder'
3
12
  require 'rabl'
4
13
  require 'rabl-rails'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gon
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.0
4
+ version: 5.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gazay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-26 00:00:00.000000000 Z
11
+ date: 2014-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -233,3 +233,4 @@ test_files:
233
233
  - spec/test_data/sample_with_helpers_rabl_rails.rabl
234
234
  - spec/test_data/sample_with_locals.json.jbuilder
235
235
  - spec/test_data/sample_with_partial.json.jbuilder
236
+ has_rdoc: