json_array_serializer 0.0.3 → 0.0.4

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: 759b6374e0d2f2f6b141dc720a08e358445a31ca
4
- data.tar.gz: e129f2d357544ff607187baf39b43855665b6dd9
3
+ metadata.gz: 9e7a4e281f8c0cd7f9375760778ee6f8e6caaf76
4
+ data.tar.gz: 7db5e8490e317f3a57c95c8b787aa6d31aa07cdf
5
5
  SHA512:
6
- metadata.gz: b521b7344f0f00622f628a3f293640daffbf3fae68508aabe7cc7be34cf3c26ceaf2a23422cb6ad71ef4fce5a18a6811e5af7908fca6473b571f24be90b80033
7
- data.tar.gz: ece9d5e2a0c7905cb41a32f0931518d10ef9db77bbc023918b352dd989d6f04eda4afda9cc3a5c7abbb8716c4c1d462b980c4a7283e7d1c266ec6e2e419325be
6
+ metadata.gz: fa1c7b226056e7812621d8becbab7dc302117f054cef04ed77ce3cce04b2264b3a011169c135cfb963d6cb112241313925b9b5a5d8406a003c34504b82cc15f4
7
+ data.tar.gz: 10c856a3be1bc47e44d4359cb25584f6037643b94395b578887fb72143419e531bb124d9c645036f68b47789e65e02e3a61602856abcf49da08133483ab5ef32
data/README.md CHANGED
@@ -1,24 +1,18 @@
1
1
  # JSONArraySerializer
2
2
 
3
- A class to serialize and deserialize arrays of JSON strings. This is useful when doing things like saving arrays of objects to a database or file, or sending them over the wire.
3
+ A class to serialize and deserialize arrays of JSON strings. This is useful when doing things like saving arrays of objects to a database or file, or sending them over the wire. The main focus of this gem is aimed at Rails's `ActiveRecord::Base#serialize`.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'json_array_serializer'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install json_array_serializer
9
+ ```ruby
10
+ gem 'json_array_serializer'
11
+ ```
18
12
 
19
13
  ## Usage
20
14
 
21
- You can use the `JSONArraySerizalizer` directly on a attribute of a Rails model as follows.
15
+ You can use the `JSONArraySerizalizer` directly on a attribute of a Rails model as follows. This will save the `bar` attribute as a string of JSON in the database, where `bar` is an array of hashes.
22
16
 
23
17
  ```ruby
24
18
  class Foo < ActiveRecord::Base
@@ -71,7 +65,7 @@ The `array_class` you pass to `JSONArraySerializer.new` __must__ have the follow
71
65
 
72
66
  ```
73
67
  # A.new : Array -> A
74
- # a.to_a : -> Array (where a is an instance of A) ?????
68
+ # a.to_a : -> Array (where a is an instance of A)
75
69
  ```
76
70
 
77
71
  ### Specifying the Database Type
@@ -102,4 +96,4 @@ end
102
96
  class Company < ActiveRecord::Base
103
97
  serialize :developers, JSONArraySerializer.new(array_class: DeveloperArray, element_class: Developer)
104
98
  end
105
- ```
99
+ ```
@@ -1,3 +1,3 @@
1
1
  class JSONArraySerializer < Array
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
@@ -9,45 +9,67 @@ class JSONArraySerializer
9
9
  # class. The element_class is what will be used to represent
10
10
  # each element of the stored JSON.
11
11
  #
12
- # The element class MUST implement two methods:
12
+ # The option :array_class MUST implement two methods:
13
+ #
14
+ # A.new : Array -> A
15
+ # a.to_a : -> Array (where a is an instance of A)
16
+ #
17
+ # The option :element_class MUST implement two methods:
13
18
  #
14
19
  # A.new : Hash -> A
15
20
  # a.to_h : -> Hash (where a is an instance of A)
16
21
  #
17
- def initialize(array_class: Array, element_class: Hash, column_type: :text)
18
- @array_class = array_class
19
- @element_class = element_class
20
- @column_type = column_type
22
+ # The option :column_type MUST be one of :string | :text | :array.
23
+ #
24
+ # The option :allow_nil is a boolean which determines whether or not
25
+ # to load/dump nil values. If set to true (the default) it will load/dump
26
+ # nil when given nil. If set to false it will treat nil as an empty array.
27
+ #
28
+ def initialize(options = {})
29
+ options = {
30
+ array_class: Array,
31
+ element_class: Hash,
32
+ column_type: :text,
33
+ allow_nil: true
34
+ }.merge(options)
35
+
36
+ @array_class = options[:array_class]
37
+ @element_class = options[:element_class]
38
+ @column_type = options[:column_type]
39
+ @allow_nil = options[:allow_nil]
21
40
  end
22
41
 
23
42
  # [JSON String] || JSON String -> array_class<element_class>
24
- # Takes an array of JSON strings and loads them
25
- # into an array of element_classes.
43
+ # Takes the data from the database, and loads them into an
44
+ # instance of `array_class` with elements of `element_class`.
26
45
  #
27
46
  def load(data)
28
- return data if data.nil?
47
+ return (@allow_nil ? nil : []) if data.nil?
29
48
 
30
- array = case @column_type
31
- when :array
32
- data.map do |json|
33
- hash = JSON.load(json)
34
- (element_class == Hash) ? hash : element_class.new(hash)
35
- end
36
- when :string, :text
37
- JSON.load(data).map do |hash|
38
- (element_class == Hash) ? hash : element_class.new(hash)
49
+ array =
50
+ case @column_type
51
+ when :array
52
+ data.map do |json|
53
+ hash = JSON.load(json)
54
+ (element_class == Hash) ? hash : element_class.new(hash)
55
+ end
56
+ when :string, :text
57
+ if element_class == Hash
58
+ JSON.load(data)
59
+ else
60
+ JSON.load(data).map { |hash| element_class.new(hash) }
61
+ end
39
62
  end
40
- end
41
63
 
42
64
  (array_class == Array) ? array : array_class.new(array)
43
65
  end
44
66
 
45
67
  # array_class<element_class> -> [JSON String] || JSON String
46
- # Takes an array of element_classes and dumps them
47
- # into JSON Strings, and returns the array of them.
68
+ # Takes an instance of `array_class` with `element_class` elements
69
+ # and dumps them either a array of JSON or JSON itself for the database.
48
70
  #
49
71
  def dump(data)
50
- return data if data.nil?
72
+ return (@allow_nil ? nil : []) if data.nil?
51
73
 
52
74
  case @column_type
53
75
  when :array
@@ -7,14 +7,15 @@ class Hash
7
7
  #
8
8
  def stringify
9
9
  inject({}) do |acc, (key, value)|
10
- acc[key.to_s] = case value
11
- when Symbol
12
- value.to_s
13
- when Hash
14
- value.stringify
15
- else
16
- value
17
- end
10
+ acc[key.to_s] =
11
+ case value
12
+ when Symbol
13
+ value.to_s
14
+ when Hash
15
+ value.stringify
16
+ else
17
+ value
18
+ end
18
19
  acc
19
20
  end
20
21
  end
File without changes
@@ -125,6 +125,10 @@ describe JSONArraySerializer do
125
125
  expect(serializer.dump(array)).to be_a(String)
126
126
  end
127
127
 
128
+ it 'returns a string given an array of another class' do
129
+ expect(serializer.dump([OpenStruct.new(foo: 'bar')])).to be_a(String)
130
+ end
131
+
128
132
  it 'JSON can load it' do
129
133
  expect { JSON.load(serializer.dump(array)) }.not_to raise_error
130
134
  end
@@ -160,6 +164,10 @@ describe JSONArraySerializer do
160
164
  expect(serializer.dump(array)).to be_a(String)
161
165
  end
162
166
 
167
+ it 'returns a string given an array of hashes' do
168
+ expect(serializer.dump([{foo: 'bar'}])).to be_a(String)
169
+ end
170
+
163
171
  it 'JSON can load it' do
164
172
  expect { JSON.load(serializer.dump(array)) }.not_to raise_error
165
173
  end
@@ -215,4 +223,36 @@ describe JSONArraySerializer do
215
223
  end
216
224
  end
217
225
  end
226
+
227
+ context 'with allow_nil set to false' do
228
+ let(:serializer) { JSONArraySerializer.new(allow_nil: false) }
229
+
230
+ describe '#load' do
231
+ it 'loads nil as []' do
232
+ expect(serializer.load(nil)).to eq([])
233
+ end
234
+ end
235
+
236
+ describe '#dump' do
237
+ it 'dumps nil as []' do
238
+ expect(['[]', []]).to include(serializer.dump(nil))
239
+ end
240
+ end
241
+ end
242
+
243
+ context 'with allow_nil set to true' do
244
+ let(:serializer) { JSONArraySerializer.new(allow_nil: true) }
245
+
246
+ describe '#load' do
247
+ it 'loads nil as nil' do
248
+ expect(serializer.load(nil)).to eq(nil)
249
+ end
250
+ end
251
+
252
+ describe '#dump' do
253
+ it 'dumps nil as nil' do
254
+ expect(serializer.dump(nil)).to eq(nil)
255
+ end
256
+ end
257
+ end
218
258
  end
data/spec/spec_helper.rb CHANGED
@@ -3,8 +3,8 @@ require 'ostruct'
3
3
  require 'json_array_serializer'
4
4
 
5
5
  # Connivance extensions.
6
- require_relative '../ext/hash'
7
- require_relative '../ext/open_struct'
6
+ require_relative 'ext/hash'
7
+ require_relative 'ext/open_struct'
8
8
 
9
9
  RSpec.configure do |config|
10
10
  config.color_enabled = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_array_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Lilienthal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-28 00:00:00.000000000 Z
11
+ date: 2014-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,11 +65,11 @@ files:
65
65
  - LICENSE.txt
66
66
  - README.md
67
67
  - Rakefile
68
- - ext/hash.rb
69
- - ext/open_struct.rb
70
68
  - json_array_serializer.gemspec
71
69
  - lib/json_array_serializer.rb
72
70
  - lib/json_array_serializer/version.rb
71
+ - spec/ext/hash.rb
72
+ - spec/ext/open_struct.rb
73
73
  - spec/json_array_serializer_spec.rb
74
74
  - spec/spec_helper.rb
75
75
  homepage: https://github.com/Americastestkitchen/json_array_serializer
@@ -97,5 +97,7 @@ signing_key:
97
97
  specification_version: 4
98
98
  summary: Simple serialization for arrays of things to arrays of JSON.
99
99
  test_files:
100
+ - spec/ext/hash.rb
101
+ - spec/ext/open_struct.rb
100
102
  - spec/json_array_serializer_spec.rb
101
103
  - spec/spec_helper.rb