simple_serializer 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGU3YmExMWM5OGZjZjc0ZGZjMDM5YzQxZTc0MDdmZWZkNDI1NmExYQ==
4
+ OTM2MDJkNDRiNjk0YmQ5Y2M3Mzk1YzQ2MTAzZjJkNWFhNWUwZTU0Ng==
5
5
  data.tar.gz: !binary |-
6
- MjcyZmFhZTUzZjkxZjcwYjZlOGYwM2M0NTMzNDRlMWU1ZmI2Njg2NQ==
6
+ YmE2MmYzZWFlMGRjZWE0MDdmYjYyNGRjYjA3NWUxNzNlZTNhMDU4NA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTE5MmQ4NmVjMWMzYjg1MjVlNDVkZGYzZjIyMmNhYTdjMTViMGViZTlkNzE5
10
- MDE1NjBjMTk3MDMyMDAyZjdjYmE5NzFmMTY5NDU2YmIyYThhNTc2ODAxMzE1
11
- MzM3ZmE1NmQ0OTYwYjBhMjE0MzMxNzE0Y2VmYzFhZmNmN2U5ZmY=
9
+ MzMyNjg5ZDc1YjcxYzg5YjQ2MTQzMTk4NWFiYjc3ZTU0YmIyYjYyM2FmOTFi
10
+ YjdkOTg2MDUxYTI2MmNkYzNmMDEwNjVhYTMwNzU1OGQzZjRkYmNmZWViYmEw
11
+ YTJiODMzMDhjMzVlOTQ2NGVlMjVhOWY4MDdhZmEwMDkxOTJiOGU=
12
12
  data.tar.gz: !binary |-
13
- NzU5YzE0YzFiZTkyOWM2NzU3YzFmNDg4NDEyMDVkOWMzZDRhMjllZTllODM4
14
- NDY2MTdkYWI1ZmQ1MWMzMzRmZGYyMDgxOGMzMDFhOGRlYmE5YjgzMTRlNjQz
15
- MWVjMTBkMGRlMWY3Y2NiMzA5NjRmNWIxZGIxNjQ4YjQxNmEyM2Q=
13
+ ZmMyZDllMDA5YWM5ZWY0MGEzMDY5OTUyMmNhMmYwMmViZjNmOGI4YWE4ZmNl
14
+ MmIwZGYxMDIzZWE3MGY0MTMxNjJhNzI4NjNmMTIyNzg4OWViNGNhOGQwNjk1
15
+ Yzg2ZjJlZGE3Nzk1NjE5ZjFlZDZiYjJmNTQzMGVjOTYwMTk3NmY=
@@ -1,14 +1,15 @@
1
1
  # Simple framelet for deserialization
2
2
  #
3
3
  # class SomeDeserializer < SimpleSerializer::Deserializer
4
- # data_attributes :site_id, :name, :category_id, :integration_key
4
+ # object_attributes :site_id, :name, :category, :integration_key
5
5
  #
6
- # def integration_key(old_integration_key)
7
- # "XX#{@data[:other_attr]}XX#{old_integration_key}XX"
6
+ # def integration_key
7
+ # "XX#{data[:other_attr]}XX#{data[:integration_key]}XX"
8
8
  # end
9
9
  #
10
- # def set_category_id(category_id)
11
- # object.category = InventoryStatusCategory.from_id(category_id)
10
+ # # Set a field regardless of presence in data hash
11
+ # def set_site_id
12
+ # object.site_id = 99
12
13
  # end
13
14
  # end
14
15
  #
@@ -22,22 +23,22 @@
22
23
  module SimpleSerializer
23
24
  class Deserializer
24
25
  class << self
25
- attr_accessor :_attributes
26
+ attr_accessor :_object_attributes
26
27
 
27
28
  def inherited(base)
28
- base._attributes = []
29
+ base._object_attributes = []
29
30
  end
30
31
 
31
- def data_attributes(*attrs)
32
- @_attributes.concat attrs
32
+ def object_attributes(*attrs)
33
+ @_object_attributes.concat attrs
33
34
 
34
35
  attrs.each do |attr|
35
- define_method attr do |datum|
36
- datum
36
+ define_method attr do
37
+ @data[attr]
37
38
  end unless method_defined?(attr)
38
39
 
39
- define_method "set_#{attr}" do |datum|
40
- object.send("#{attr}=", send(attr, datum))
40
+ define_method "set_#{attr}" do
41
+ object.send("#{attr}=", send(attr)) if @data.has_key?(attr)
41
42
  end unless method_defined?("set_#{attr}")
42
43
  end
43
44
  end
@@ -51,7 +52,7 @@ class Deserializer
51
52
  end
52
53
  end
53
54
 
54
- attr_accessor :object
55
+ attr_reader :object, :data
55
56
 
56
57
  def initialize(object, data)
57
58
  @object = object
@@ -59,8 +60,8 @@ class Deserializer
59
60
  end
60
61
 
61
62
  def deserialize
62
- self.class._attributes.dup.each do |name|
63
- send("set_#{name}", @data[name])
63
+ self.class._object_attributes.dup.each do |name|
64
+ send("set_#{name}")
64
65
  end
65
66
  object
66
67
  end
@@ -4,7 +4,7 @@
4
4
  # and dependence on ActiveModel
5
5
  #
6
6
  # class SomeSerializer < SimpleSerializer::Serializer
7
- # attributes :id, :name, :category_id, :errors
7
+ # hash_attributes :id, :name, :category_id, :errors
8
8
  #
9
9
  # def category_id
10
10
  # object.category.try(:id)
@@ -25,14 +25,14 @@
25
25
  module SimpleSerializer
26
26
  class Serializer
27
27
  class << self
28
- attr_accessor :_attributes
28
+ attr_accessor :_hash_attributes
29
29
 
30
30
  def inherited(base)
31
- base._attributes = []
31
+ base._hash_attributes = []
32
32
  end
33
33
 
34
- def attributes(*attrs)
35
- @_attributes.concat attrs
34
+ def hash_attributes(*attrs)
35
+ @_hash_attributes.concat attrs
36
36
 
37
37
  attrs.each do |attr|
38
38
  define_method attr do
@@ -58,15 +58,15 @@ module SimpleSerializer
58
58
  @object = object
59
59
  end
60
60
 
61
- def attributes
62
- self.class._attributes.dup.each_with_object({}) do |name, hash|
61
+ def extract_attributes
62
+ self.class._hash_attributes.dup.each_with_object({}) do |name, hash|
63
63
  hash[name] = send(name)
64
64
  end
65
65
  end
66
66
 
67
67
  def serialize(_={})
68
68
  return nil if object.nil?
69
- attributes
69
+ extract_attributes
70
70
  end
71
71
  alias :as_json :serialize
72
72
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleSerializer
2
- VERSION = "0.0.1"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["seank@nulogy.com"]
11
11
  spec.summary = %q{Very simple framelet for serializing/deserializing objects to hashes.}
12
12
  spec.description = %q{Very simple framelet for serializing/deserializing objects to hashes.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/nulogy/simple_serializer"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -6,7 +6,7 @@ describe SimpleSerializer::Deserializer do
6
6
  let(:object) { double }
7
7
 
8
8
  class TestDeserializer < SimpleSerializer::Deserializer
9
- data_attributes :field1
9
+ object_attributes :field1
10
10
  end
11
11
 
12
12
  describe 'deserialize' do
@@ -18,12 +18,18 @@ describe SimpleSerializer::Deserializer do
18
18
  expect(result).to be object
19
19
  end
20
20
 
21
+ it 'does not set attributes when the hash key is not set' do
22
+ expect(object).to_not receive(:field1=)
23
+
24
+ TestDeserializer.deserialize(object, {})
25
+ end
26
+
21
27
  describe 'attribute value transformation' do
22
28
  class TestTransformingDeserializer < SimpleSerializer::Deserializer
23
- data_attributes :field1
29
+ object_attributes :field1
24
30
 
25
- def field1(datum)
26
- datum + 1
31
+ def field1
32
+ data[:field1] + 1
27
33
  end
28
34
  end
29
35
 
@@ -35,20 +41,19 @@ describe SimpleSerializer::Deserializer do
35
41
  end
36
42
  end
37
43
 
38
- describe "attribute key transformation" do
44
+ describe 'attribute setter override' do
39
45
  class TestMappingDeserializer < SimpleSerializer::Deserializer
40
- data_attributes :association_id
46
+ object_attributes :association
41
47
 
42
- def set_association_id(datum)
43
- object.association = datum
48
+ def set_association
49
+ object.association = 'static'
44
50
  end
45
51
  end
46
52
 
47
- it 'maps keys' do
48
- attrs = {association_id: 1}
49
- expect(object).to receive(:association=).with(1)
53
+ it 'maps keys regardless of presence in data hash' do
54
+ expect(object).to receive(:association=).with('static')
50
55
 
51
- TestMappingDeserializer.deserialize(object, attrs)
56
+ TestMappingDeserializer.deserialize(object, {})
52
57
  end
53
58
  end
54
59
  end
@@ -6,7 +6,7 @@ describe SimpleSerializer::Serializer do
6
6
  let(:object) { double }
7
7
 
8
8
  class TestSerializer < SimpleSerializer::Serializer
9
- attributes :field1, :field2
9
+ hash_attributes :field1, :field2
10
10
  end
11
11
 
12
12
  describe 'serialize' do
@@ -20,7 +20,7 @@ describe SimpleSerializer::Serializer do
20
20
 
21
21
  describe 'value transformation' do
22
22
  class TestTransformingSerializer < SimpleSerializer::Serializer
23
- attributes :field1
23
+ hash_attributes :field1
24
24
 
25
25
  def field1
26
26
  object.field1 + 1
@@ -35,6 +35,18 @@ describe SimpleSerializer::Serializer do
35
35
  expect(result).to eq({field1: 2})
36
36
  end
37
37
  end
38
+
39
+ # This should work in a Rails controller when the ActiveModelSerializers gem is loaded:
40
+ #
41
+ # render json: object, serializer: TestSerializer
42
+ it 'is api-compatible with ActiveModel::Serializer' do
43
+ object = double(field1: 1, field2: 2)
44
+ options = {}
45
+
46
+ result = TestSerializer.new(object, options).serialize(options)
47
+
48
+ expect(result).to eq({field1: 1, field2: 2})
49
+ end
38
50
  end
39
51
 
40
52
  describe 'serialize_array' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Kirby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-06 00:00:00.000000000 Z
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,7 +72,7 @@ files:
72
72
  - spec/unit/simple_serializer/deserializer_spec.rb
73
73
  - spec/unit/simple_serializer/serializer_spec.rb
74
74
  - spec/unit_spec_helper.rb
75
- homepage: ''
75
+ homepage: https://github.com/nulogy/simple_serializer
76
76
  licenses:
77
77
  - MIT
78
78
  metadata: {}