seri 0.1.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +6 -0
- data/.rubocop.yml +8 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +22 -4
- data/Rakefile +3 -3
- data/bin/console +4 -4
- data/lib/serializer/group_serializer.rb +9 -4
- data/lib/serializer/version.rb +1 -1
- data/lib/serializer.rb +28 -23
- data/seri.gemspec +23 -20
- metadata +24 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cfa2981681193432bc52036121c8481f8558594fb4162e5ca7938e911ade897
|
4
|
+
data.tar.gz: 005d6c9f9fa17d6aac6d1ec719cb4acd87e713782f46621899ff53c199ff9702
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b004ae3a8febfd4ba2869390f7ba91a982835f44c83813fc8423b19ff214f28804feeb960659ca9e09c7a0fb25c92eb3d9d16cff7541acc6a584eb71d4f954cf
|
7
|
+
data.tar.gz: 13dc8907b0083bbd8e251b10b23f58781d561e8f26deaefe013f56a825072bc640f08a58c3abddb9594a9074e61482a1fc324b7bb67c7c9a6e68d3f18c908edc
|
data/.circleci/config.yml
CHANGED
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in seri.gemspec
|
6
6
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,18 +1,25 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
seri (0.1.
|
4
|
+
seri (0.1.5)
|
5
5
|
appsignal (~> 2.7)
|
6
6
|
oj (~> 3.7)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
appsignal (2.
|
11
|
+
appsignal (2.9.5)
|
12
12
|
rack
|
13
|
+
ast (2.4.0)
|
13
14
|
diff-lcs (1.3)
|
14
|
-
|
15
|
-
|
15
|
+
jaro_winkler (1.5.2)
|
16
|
+
oj (3.7.11)
|
17
|
+
parallel (1.17.0)
|
18
|
+
parser (2.6.2.1)
|
19
|
+
ast (~> 2.4.0)
|
20
|
+
psych (3.1.0)
|
21
|
+
rack (2.0.7)
|
22
|
+
rainbow (3.0.0)
|
16
23
|
rake (10.5.0)
|
17
24
|
rspec (3.8.0)
|
18
25
|
rspec-core (~> 3.8.0)
|
@@ -27,6 +34,16 @@ GEM
|
|
27
34
|
diff-lcs (>= 1.2.0, < 2.0)
|
28
35
|
rspec-support (~> 3.8.0)
|
29
36
|
rspec-support (3.8.0)
|
37
|
+
rubocop (0.67.2)
|
38
|
+
jaro_winkler (~> 1.5.1)
|
39
|
+
parallel (~> 1.10)
|
40
|
+
parser (>= 2.5, != 2.5.1.1)
|
41
|
+
psych (>= 3.1.0)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
ruby-progressbar (~> 1.7)
|
44
|
+
unicode-display_width (>= 1.4.0, < 1.6)
|
45
|
+
ruby-progressbar (1.10.0)
|
46
|
+
unicode-display_width (1.5.0)
|
30
47
|
|
31
48
|
PLATFORMS
|
32
49
|
ruby
|
@@ -35,6 +52,7 @@ DEPENDENCIES
|
|
35
52
|
bundler (~> 1.16)
|
36
53
|
rake (~> 10.0)
|
37
54
|
rspec (~> 3.0)
|
55
|
+
rubocop (~> 0.67.2)
|
38
56
|
seri!
|
39
57
|
|
40
58
|
BUNDLED WITH
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'serializer'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require
|
10
|
+
# require 'pry'
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start(__FILE__)
|
@@ -1,9 +1,10 @@
|
|
1
1
|
class GroupSerializer
|
2
|
-
def initialize(objects, serializer: nil)
|
2
|
+
def initialize(objects, serializer: nil, scope: {})
|
3
3
|
raise ArgumentError, 'serializer needs to be specified' if serializer.nil?
|
4
4
|
|
5
5
|
@objects = objects
|
6
6
|
@serializer = serializer
|
7
|
+
@scope = scope
|
7
8
|
end
|
8
9
|
|
9
10
|
def to_json(*)
|
@@ -12,9 +13,13 @@ class GroupSerializer
|
|
12
13
|
'Group serializer',
|
13
14
|
@serializer.to_s
|
14
15
|
) do
|
15
|
-
|
16
|
-
|
17
|
-
Oj.dump(result, mode: :json)
|
16
|
+
Oj.dump(to_h, mode: :json)
|
18
17
|
end
|
19
18
|
end
|
19
|
+
|
20
|
+
def to_h
|
21
|
+
@objects
|
22
|
+
.map { |object| @serializer.new(object, scope: @scope) }
|
23
|
+
.map(&:to_h)
|
24
|
+
end
|
20
25
|
end
|
data/lib/serializer/version.rb
CHANGED
data/lib/serializer.rb
CHANGED
@@ -4,11 +4,11 @@ require 'serializer/group_serializer'
|
|
4
4
|
class Serializer
|
5
5
|
class SerializerError < StandardError; end
|
6
6
|
|
7
|
-
ARRAYS = %w
|
7
|
+
ARRAYS = %w[
|
8
8
|
Array
|
9
9
|
ActiveRecord_AssociationRelation
|
10
10
|
ActiveRecord_Associations_CollectionProxy
|
11
|
-
|
11
|
+
].freeze
|
12
12
|
|
13
13
|
Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
|
14
14
|
|
@@ -32,24 +32,7 @@ class Serializer
|
|
32
32
|
self.class.attributes.each_with_object({}) do |attribute, obj|
|
33
33
|
next if attribute.condition && !public_send(attribute.condition)
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
# Fetches a value from an attribute by checking if there's a ..
|
38
|
-
# .. static value set, or a ..
|
39
|
-
# .. method defined in the serializer, or a ..
|
40
|
-
# .. method/attribute defined in the object or ..
|
41
|
-
# .. it raises an error
|
42
|
-
value = if attribute.options.has_key?(:static_value)
|
43
|
-
attribute.options.fetch(:static_value)
|
44
|
-
elsif respond_to?(extraction_key)
|
45
|
-
public_send(extraction_key)
|
46
|
-
elsif object.respond_to?(extraction_key)
|
47
|
-
object.public_send(extraction_key)
|
48
|
-
else
|
49
|
-
raise SerializerError, "unknown attribute '#{extraction_key}'"
|
50
|
-
end
|
51
|
-
|
52
|
-
obj[attribute.key] = serialize_value(value, attribute.serializer)
|
35
|
+
obj[attribute.key] = serialize_value(attribute)
|
53
36
|
end
|
54
37
|
end
|
55
38
|
|
@@ -61,13 +44,35 @@ class Serializer
|
|
61
44
|
|
62
45
|
private
|
63
46
|
|
64
|
-
def serialize_value(
|
65
|
-
|
47
|
+
def serialize_value(attribute)
|
48
|
+
value = extract_value(attribute)
|
49
|
+
serializer = attribute.serializer
|
66
50
|
|
67
51
|
if ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
|
68
52
|
value.map { |v| serializer.new(v).to_h }
|
69
|
-
|
53
|
+
elsif serializer
|
70
54
|
serializer.new(value).to_h
|
55
|
+
else
|
56
|
+
value
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Fetches a value from an attribute by checking if there's a ..
|
61
|
+
# .. static value set, or a ..
|
62
|
+
# .. method defined in the serializer, or a ..
|
63
|
+
# .. method/attribute defined in the object or ..
|
64
|
+
# .. it raises an error
|
65
|
+
def extract_value(attribute)
|
66
|
+
extraction_key = attribute.from || attribute.key
|
67
|
+
|
68
|
+
if attribute.options.key?(:static_value)
|
69
|
+
attribute.options.fetch(:static_value)
|
70
|
+
elsif respond_to?(extraction_key)
|
71
|
+
public_send(extraction_key)
|
72
|
+
elsif object.respond_to?(extraction_key)
|
73
|
+
object.public_send(extraction_key)
|
74
|
+
else
|
75
|
+
raise SerializerError, "unknown attribute '#{extraction_key}'"
|
71
76
|
end
|
72
77
|
end
|
73
78
|
end
|
data/seri.gemspec
CHANGED
@@ -1,32 +1,35 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require 'serializer/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'seri'
|
8
7
|
spec.version = Serializer::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
8
|
+
spec.authors = ['grdw']
|
9
|
+
spec.email = ['gerard@wetransfer.com']
|
11
10
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
11
|
+
spec.summary = 'A basic serializer'
|
12
|
+
spec.description = 'A basic serializer'
|
13
|
+
spec.homepage = 'https://github.com/grdw/seri'
|
14
|
+
spec.license = 'MIT'
|
16
15
|
|
17
16
|
# Specify which files should be added to the gem when it is released.
|
18
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
19
|
-
|
20
|
-
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
18
|
+
# into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
21
23
|
end
|
22
|
-
spec.bindir =
|
24
|
+
spec.bindir = 'exe'
|
23
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
-
spec.require_paths = [
|
26
|
+
spec.require_paths = ['lib']
|
25
27
|
|
26
|
-
spec.add_dependency
|
27
|
-
spec.add_dependency
|
28
|
+
spec.add_dependency 'appsignal', '~> 2.7'
|
29
|
+
spec.add_dependency 'oj', '~> 3.7'
|
28
30
|
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
34
|
+
spec.add_development_dependency 'rubocop', '~> 0.67.2'
|
32
35
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seri
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- grdw
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: appsignal
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.7'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: oj
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3.7'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '3.7'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,7 +80,21 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
|
-
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.67.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.67.2
|
97
|
+
description: A basic serializer
|
84
98
|
email:
|
85
99
|
- gerard@wetransfer.com
|
86
100
|
executables: []
|
@@ -90,6 +104,7 @@ files:
|
|
90
104
|
- ".circleci/config.yml"
|
91
105
|
- ".gitignore"
|
92
106
|
- ".rspec"
|
107
|
+
- ".rubocop.yml"
|
93
108
|
- ".travis.yml"
|
94
109
|
- CODE_OF_CONDUCT.md
|
95
110
|
- Gemfile
|