plain_serializer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/plain_serializer.rb +15 -0
- data/lib/plain_serializer/base.rb +43 -0
- data/lib/plain_serializer/configurable.rb +49 -0
- data/lib/plain_serializer/helpers.rb +12 -0
- data/lib/plain_serializer/modifiable.rb +24 -0
- data/lib/plain_serializer/serializable.rb +57 -0
- data/lib/plain_serializer/version.rb +5 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: adbcd61a28e79cc60a085bfe6887b7896e6a7e54d2d6e856e82afb83f11a3a85
|
|
4
|
+
data.tar.gz: 54d5fd5407d810c26a7427200b55a5e82532914cfd0bdde88e1df680d64b01c5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: dfdf44a77c772609e1d2f31555110f3c7e972808503eceb7bf3adfe2a3b985230135abc8008f643b720e09692015eb0dc9db8f638ab9df52b8523377fe765ad1
|
|
7
|
+
data.tar.gz: 74d18be2fdc7cdd42167a8990ba296f14ba35af43e17e00d24b4eb9cc3bde91da6e345f97cfcdf52a9f4fddf1e98c386053c89bed088661339ddd63bb8914333
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'plain_serializer/version'
|
|
4
|
+
|
|
5
|
+
require 'plain_serializer/helpers'
|
|
6
|
+
|
|
7
|
+
require 'plain_serializer/configurable'
|
|
8
|
+
require 'plain_serializer/modifiable'
|
|
9
|
+
require 'plain_serializer/serializable'
|
|
10
|
+
|
|
11
|
+
require 'plain_serializer/base'
|
|
12
|
+
|
|
13
|
+
# The root module
|
|
14
|
+
module PlainSerializer
|
|
15
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PlainSerializer
|
|
4
|
+
# The base class
|
|
5
|
+
class Base
|
|
6
|
+
prepend Modifiable
|
|
7
|
+
|
|
8
|
+
include Serializable
|
|
9
|
+
include Configurable
|
|
10
|
+
|
|
11
|
+
def initialize(*args)
|
|
12
|
+
Helpers.extract_options!(args)
|
|
13
|
+
|
|
14
|
+
@attributes = args
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def serialize(entity)
|
|
18
|
+
return if entity.nil?
|
|
19
|
+
|
|
20
|
+
@attributes.each_with_object({}) do |attribute, result|
|
|
21
|
+
result[attribute] = send(attribute, entity)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def serialize_collection(entities)
|
|
26
|
+
return if entities.nil?
|
|
27
|
+
|
|
28
|
+
entities.each_with_object([]) do |entity, result|
|
|
29
|
+
result << serialize(entity)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class << self
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def define_attribute(name)
|
|
37
|
+
define_method(name) do |entity|
|
|
38
|
+
entity.send(name)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PlainSerializer
|
|
4
|
+
# Module which adds support of groups
|
|
5
|
+
module Configurable
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend ClassMethods
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Defines required methods for base class
|
|
11
|
+
module ClassMethods
|
|
12
|
+
def setup(*args)
|
|
13
|
+
opts = Helpers.extract_options!(args)
|
|
14
|
+
|
|
15
|
+
attributes = args.each_with_object([]) do |attribute, result|
|
|
16
|
+
next result << attribute unless group?(attribute)
|
|
17
|
+
|
|
18
|
+
group(attribute).each do |group_attribute|
|
|
19
|
+
if group_attribute.is_a?(Hash)
|
|
20
|
+
opts.merge!(group_attribute)
|
|
21
|
+
else
|
|
22
|
+
result << group_attribute
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
super(*attributes, **opts)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def define_group(name, attributes)
|
|
33
|
+
groups[name] = attributes
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def groups
|
|
37
|
+
@groups ||= {}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def group(name)
|
|
41
|
+
groups[name]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def group?(name)
|
|
45
|
+
groups.key?(name)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PlainSerializer
|
|
4
|
+
# Module which adds support of modifying output
|
|
5
|
+
module Modifiable
|
|
6
|
+
def serialize(entity, &block)
|
|
7
|
+
result = super(entity)
|
|
8
|
+
|
|
9
|
+
return if result.nil?
|
|
10
|
+
|
|
11
|
+
block&.call(result, entity)
|
|
12
|
+
|
|
13
|
+
result
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def serialize_collection(entities, &block)
|
|
17
|
+
return if entities.nil?
|
|
18
|
+
|
|
19
|
+
entities.each_with_object([]) do |entity, result|
|
|
20
|
+
result << serialize(entity, &block)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PlainSerializer
|
|
4
|
+
# Module which adds support of nested serializers
|
|
5
|
+
module Serializable
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend ClassMethods
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def setup_serializer(name, attributes)
|
|
11
|
+
serializers[name] = self.class.serializers[name].setup(*attributes)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def serializers
|
|
15
|
+
@serializers ||= {}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def serializer(name)
|
|
19
|
+
serializers[name] || self.class.serializers[name].new
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Defines required methods for base class
|
|
23
|
+
module ClassMethods
|
|
24
|
+
def setup(*args)
|
|
25
|
+
options = Helpers.extract_options!(args)
|
|
26
|
+
|
|
27
|
+
attributes = args + options.keys
|
|
28
|
+
|
|
29
|
+
new(*attributes).tap do |instance|
|
|
30
|
+
options.each do |serializer_name, opts|
|
|
31
|
+
instance.setup_serializer(serializer_name, opts)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def define_serializer(name, klass)
|
|
37
|
+
serializers[name] = klass
|
|
38
|
+
|
|
39
|
+
define_method(name) do |entity|
|
|
40
|
+
serializer(name).serialize(entity.send(name))
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def define_collection_serializer(name, klass)
|
|
45
|
+
serializers[name] = klass
|
|
46
|
+
|
|
47
|
+
define_method(name) do |entity|
|
|
48
|
+
serializer(name).serialize_collection(entity.send(name))
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def serializers
|
|
53
|
+
@serializers ||= {}
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: plain_serializer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- djezzzl
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-05-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- lawliet.djez@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/plain_serializer.rb
|
|
21
|
+
- lib/plain_serializer/base.rb
|
|
22
|
+
- lib/plain_serializer/configurable.rb
|
|
23
|
+
- lib/plain_serializer/helpers.rb
|
|
24
|
+
- lib/plain_serializer/modifiable.rb
|
|
25
|
+
- lib/plain_serializer/serializable.rb
|
|
26
|
+
- lib/plain_serializer/version.rb
|
|
27
|
+
homepage: https://github.com/djezzzl/plain_serializer
|
|
28
|
+
licenses:
|
|
29
|
+
- MIT
|
|
30
|
+
metadata:
|
|
31
|
+
homepage_uri: https://github.com/djezzzl/plain_serializer
|
|
32
|
+
source_code_uri: https://github.com/djezzzl/plain_serializer
|
|
33
|
+
changelog_uri: https://github.com/djezzzl/plain_serializer/CHANGELOG.md
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 2.3.0
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 3.0.3
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 4
|
|
52
|
+
summary: Plain Serializer DSL
|
|
53
|
+
test_files: []
|