dummy-serializer 0.0.2
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/LICENSE +24 -0
- data/README.md +21 -0
- data/dummy-serializer.gemspec +17 -0
- data/lib/dummy-serializer.rb +1 -0
- data/lib/dummy/serializer.rb +28 -0
- data/lib/dummy/serializer/attributes.rb +16 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 435155d2b5ac3f506eb378de53da09ff4d660dc1c5f0836ed9cd30ebdd1da5a1
|
4
|
+
data.tar.gz: 12ea34b332143cd47cc4acc2aed58376da41bb981ae6d1d920e6540d2e21bab6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bcf07490047dea124bb07ca9724460b9b630dfdf3e468fa0c0ee256a5abd608a0cfa231282e1f75ec492c88fde0e8c433b40b3b273d59225b8b493ae74d9f1fb
|
7
|
+
data.tar.gz: 331f6cc7e61195a490bf446fd01d3de5c937634ef6d6477278868ffc8623c322619f6b648e77362c9a27b97186c1127ebbda2b311b09aef123ad7a7ce447c571
|
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Dummy::Serializer
|
2
|
+
|
3
|
+
Serializer built to practice building custom DSL.
|
4
|
+
|
5
|
+
# Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'dummy-serializer'
|
9
|
+
require 'ostruct'
|
10
|
+
|
11
|
+
object = OpenStruct.new(id: 123, name: 'Ruby', age: 27)
|
12
|
+
|
13
|
+
class ObjectSerializer < Dummy::Serializer
|
14
|
+
attribute :id
|
15
|
+
attribute :display_name do
|
16
|
+
"#{object.name} - #{object.age}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
ObjectSerializer.new(object).serialize # { id: 123, display_name: 'Ruby - 27' }
|
21
|
+
```
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'dummy-serializer'
|
3
|
+
s.summary = 'Dummy serialization'
|
4
|
+
s.version = '0.0.2'
|
5
|
+
s.date = '2020-02-03'
|
6
|
+
|
7
|
+
s.license = 'Unlicense'
|
8
|
+
s.homepage = 'https://github.com/WojciechKo/dummy-serializer'
|
9
|
+
s.authors = ['Wojciech Korzeniowski']
|
10
|
+
|
11
|
+
s.files = Dir['LICENSE', 'README.md', 'dummy-serializer.gemspec', 'lib/**/*']
|
12
|
+
s.require_paths = ['lib']
|
13
|
+
|
14
|
+
s.add_development_dependency 'bundler'
|
15
|
+
s.add_development_dependency 'rake'
|
16
|
+
s.add_development_dependency 'rspec'
|
17
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'dummy/serializer'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'dummy/serializer/attributes'
|
2
|
+
|
3
|
+
module Dummy
|
4
|
+
class Serializer
|
5
|
+
extend Attributes
|
6
|
+
|
7
|
+
def initialize(object)
|
8
|
+
@object = object
|
9
|
+
end
|
10
|
+
|
11
|
+
def serialize
|
12
|
+
self.class.ancestors
|
13
|
+
.grep(Attributes)
|
14
|
+
.reverse
|
15
|
+
.map(&:attributes)
|
16
|
+
.reduce(:merge)
|
17
|
+
.transform_values(&resolve_attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :object
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def resolve_attributes
|
25
|
+
->(resolver) { instance_exec(object, &resolver) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Dummy
|
2
|
+
class Serializer
|
3
|
+
module Attributes
|
4
|
+
DEFAULT_RESOLVER = ->(attribute, object) { object.send(attribute) }
|
5
|
+
|
6
|
+
def attribute(name, resolver = nil, &block)
|
7
|
+
attributes[name] = resolver || block || DEFAULT_RESOLVER.curry[name]
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes
|
11
|
+
@attributes ||= {}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dummy-serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wojciech Korzeniowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- LICENSE
|
62
|
+
- README.md
|
63
|
+
- dummy-serializer.gemspec
|
64
|
+
- lib/dummy-serializer.rb
|
65
|
+
- lib/dummy/serializer.rb
|
66
|
+
- lib/dummy/serializer/attributes.rb
|
67
|
+
homepage: https://github.com/WojciechKo/dummy-serializer
|
68
|
+
licenses:
|
69
|
+
- Unlicense
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubygems_version: 3.0.3
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: Dummy serialization
|
90
|
+
test_files: []
|