simple-serializer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/serializer.rb +89 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e30b7fbaaea00bdf1e3f5bbf2d92badbd7cd3ce52fa42e726de1a07808f0111
4
+ data.tar.gz: 634f28c41963af5a4185f2795ac659a8e5ac1efa1d61ef881ee11386b4b7de0d
5
+ SHA512:
6
+ metadata.gz: 5cc991cf2ad27a95c6cda224480b35d64760737eff917b54389f92ae0ce4f40a64de6dfe7bb1871ec4f4984a93e5901ece1e04b15d86ba56dfc2ce06046c6b67
7
+ data.tar.gz: 0a4cc2c36a615f33e7b3bab707f210b1a6b086faee991b9e01847958b1a6c0d62e268cdee772e22262ac9b6049950fd23631d38117e4b34d9bfd4b76a8187d60
data/lib/serializer.rb ADDED
@@ -0,0 +1,89 @@
1
+ class SimpleSerializer
2
+ VERSION = '0.1.0'
3
+
4
+ def initialize(object)
5
+ @object = object
6
+ end
7
+
8
+ def to_h
9
+ return @object.map(&method(:to_h_single)) if @object.is_a?(Enumerable)
10
+
11
+ to_h_single(@object)
12
+ end
13
+
14
+ class << self
15
+ attr_reader :fields
16
+
17
+ def helpers
18
+ serializer = self
19
+
20
+ Module.new.tap do |m|
21
+ m.define_singleton_method(:included) do |base|
22
+ base.define_method(:to_h) { serializer.to_h(self) }
23
+
24
+ base.define_method(:to_json) { serializer.to_json(self) }
25
+
26
+ base.define_method(:serializer_class) { serializer }
27
+ end
28
+ end
29
+ end
30
+
31
+ def [](object)
32
+ to_h(object)
33
+ end
34
+
35
+ def to_h(object)
36
+ new(object).to_h
37
+ end
38
+
39
+ def to_json(object)
40
+ to_h(object).to_json
41
+ end
42
+
43
+ def attributes(*fields, **named, &block)
44
+ @fields ||= {}
45
+
46
+ fields.each { |k| @fields[k] = block }
47
+
48
+ @fields.merge!(named)
49
+ end
50
+
51
+ alias attribute attributes
52
+
53
+ def serialize(field, &block)
54
+ attributes(field => Class.new(SimpleSerializer, &block))
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def to_h_single(object)
61
+ self.class.fields&.reduce({}) do |h, (k, v)|
62
+ h.merge!(serialize_key_val(object, k, v))
63
+ end
64
+ end
65
+
66
+ def serialize_key_val(obj, key, val)
67
+ return { key => nest(obj.send(key)) } if val.nil?
68
+
69
+ return { key => nest(obj.instance_eval(&val)) } if val.is_a?(Proc)
70
+
71
+ if val.is_a?(Class) && val < SimpleSerializer
72
+ return { key => val[obj.send(key)] }
73
+ end
74
+
75
+ { val => nest(obj.send(key)) }
76
+ end
77
+
78
+ def nest(result)
79
+ serializable = result.respond_to?(:serializer_class)
80
+ serializable_array = result.is_a?(Enumerable) &&
81
+ result.first.respond_to?(:serializer_class)
82
+
83
+ return result.serializer_class.to_h(result) if serializable
84
+ return result.first.serializer_class.to_h(result) if serializable_array
85
+
86
+ result
87
+ end
88
+ end
89
+
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Slyusarev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.60'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.60'
41
+ description:
42
+ email:
43
+ - slyusarevmikhail@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/serializer.rb
49
+ homepage: https://github.com/mikhailvs/simple-serializer
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.7.7
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Simple serializer. Easily serialize objects.
73
+ test_files: []