surrealist 1.3.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +1 -1
- data/.rubocop.yml +27 -25
- data/.travis.yml +4 -6
- data/CHANGELOG.md +19 -0
- data/Gemfile +3 -3
- data/README.md +6 -6
- data/Rakefile +3 -1
- data/benchmarks/surrealist_vs_ams.rb +6 -4
- data/bin/console +1 -0
- data/gemfiles/activerecord42.gemfile +2 -2
- data/lib/surrealist/builder.rb +2 -1
- data/lib/surrealist/carrier.rb +3 -3
- data/lib/surrealist/exception_raiser.rb +5 -4
- data/lib/surrealist/hash_utils.rb +10 -9
- data/lib/surrealist/helper.rb +3 -1
- data/lib/surrealist/instance_methods.rb +4 -4
- data/lib/surrealist/schema_definer.rb +1 -1
- data/lib/surrealist/serializer.rb +6 -4
- data/lib/surrealist/string_utils.rb +20 -17
- data/lib/surrealist/type_helper.rb +1 -1
- data/lib/surrealist/vars_helper.rb +8 -6
- data/lib/surrealist/version.rb +1 -1
- data/lib/surrealist/wrapper.rb +7 -5
- data/lib/surrealist.rb +6 -6
- data/surrealist.gemspec +7 -7
- metadata +19 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a95bb3a7fc605daa57ca534526272f1b07c213955a27d3e0ece8aed665feb01
|
4
|
+
data.tar.gz: 2a7a0506b60272f0c4e48fd05ccf2ec3f7ca26ae48ad02fab3228fe654b8cfa0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ead5a826a27d9947b642e4e0a1119798899df0d2fb9868b171e15219551fb7934fae2d0e628091ec22b17706ab447e9d0c90cd1e523fa859034b28f31a1b5520
|
7
|
+
data.tar.gz: 2db98f48230a322fc153f787cb33b4486c796473b9ceb653eb75a703624a35ca2e1b7ad72457bcd16533ef07f516c53c1ff984d81f0e7d22cbcb6c66909999ba
|
data/.hound.yml
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,27 +1,29 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
3
5
|
Exclude:
|
4
6
|
- './gemfiles/*gemfile'
|
5
7
|
- './tmp/*'
|
6
8
|
|
7
|
-
Documentation:
|
9
|
+
Style/Documentation:
|
8
10
|
Exclude:
|
9
11
|
- benchmarks/*rb
|
10
12
|
- spec/**/*rb
|
11
13
|
|
12
14
|
# Layout
|
13
15
|
|
14
|
-
Layout/
|
16
|
+
Layout/ParameterAlignment:
|
15
17
|
Enabled: true
|
16
18
|
|
17
19
|
Layout/EmptyLineBetweenDefs:
|
18
20
|
Exclude:
|
19
21
|
- spec/**/*rb
|
20
22
|
|
21
|
-
Layout/
|
23
|
+
Layout/FirstArrayElementIndentation:
|
22
24
|
EnforcedStyle: consistent
|
23
25
|
|
24
|
-
Layout/
|
26
|
+
Layout/FirstHashElementIndentation:
|
25
27
|
EnforcedStyle: consistent
|
26
28
|
|
27
29
|
Layout/MultilineMethodCallIndentation:
|
@@ -36,6 +38,10 @@ Layout/SpaceInLambdaLiteral:
|
|
36
38
|
Layout/ClosingParenthesisIndentation:
|
37
39
|
Enabled: true
|
38
40
|
|
41
|
+
Layout/LineLength:
|
42
|
+
Exclude: [ benchmarks/*rb ]
|
43
|
+
Max: 110
|
44
|
+
|
39
45
|
# Lint
|
40
46
|
Lint/AmbiguousBlockAssociation:
|
41
47
|
Enabled: false
|
@@ -52,6 +58,19 @@ Lint/NonLocalExitFromIterator:
|
|
52
58
|
Lint/RescueType:
|
53
59
|
Enabled: true
|
54
60
|
|
61
|
+
Lint/MissingSuper:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Lint/ConstantDefinitionInBlock:
|
65
|
+
Exclude:
|
66
|
+
- spec/**/*rb
|
67
|
+
|
68
|
+
Lint/EmptyClass:
|
69
|
+
Enabled: false
|
70
|
+
|
71
|
+
Lint/SymbolConversion:
|
72
|
+
Enabled: false
|
73
|
+
|
55
74
|
# Metrics
|
56
75
|
|
57
76
|
Metrics/BlockLength:
|
@@ -65,10 +84,6 @@ Metrics/CyclomaticComplexity:
|
|
65
84
|
Metrics/MethodLength:
|
66
85
|
Max: 15
|
67
86
|
|
68
|
-
Metrics/LineLength:
|
69
|
-
Exclude: [benchmarks/*rb]
|
70
|
-
Max: 110
|
71
|
-
|
72
87
|
Metrics/PerceivedComplexity:
|
73
88
|
Enabled: false
|
74
89
|
|
@@ -81,20 +96,9 @@ Naming/AccessorMethodName:
|
|
81
96
|
Enabled: false
|
82
97
|
|
83
98
|
Naming/PredicateName:
|
84
|
-
|
99
|
+
ForbiddenPrefixes:
|
85
100
|
- is_
|
86
101
|
|
87
|
-
# Performance
|
88
|
-
|
89
|
-
Performance/Caller:
|
90
|
-
Enabled: true
|
91
|
-
|
92
|
-
Performance/Casecmp:
|
93
|
-
Enabled: true
|
94
|
-
|
95
|
-
Performance/UnfreezeString:
|
96
|
-
Enabled: true
|
97
|
-
|
98
102
|
# Style
|
99
103
|
|
100
104
|
# ¯\_(ツ)_/¯
|
@@ -163,12 +167,10 @@ Style/TrailingCommaInHashLiteral:
|
|
163
167
|
Style/MissingRespondToMissing:
|
164
168
|
Enabled: false
|
165
169
|
|
166
|
-
Style/MethodMissingSuper:
|
167
|
-
Enabled: false
|
168
|
-
|
169
170
|
Style/EvalWithLocation:
|
170
171
|
Exclude:
|
171
172
|
- spec/**/*.rb
|
172
173
|
|
173
|
-
Style/
|
174
|
+
Style/ClassVars:
|
174
175
|
Enabled: false
|
176
|
+
|
data/.travis.yml
CHANGED
@@ -5,14 +5,12 @@ script: bundle exec rake
|
|
5
5
|
matrix:
|
6
6
|
fast_finish: true
|
7
7
|
include:
|
8
|
-
- rvm:
|
8
|
+
- rvm: 3.0
|
9
9
|
gemfile: Gemfile
|
10
|
-
- rvm: 2.
|
11
|
-
gemfile: Gemfile
|
12
|
-
- rvm: 2.4
|
10
|
+
- rvm: 2.7
|
13
11
|
gemfile: Gemfile
|
14
|
-
- rvm: 2.
|
12
|
+
- rvm: 2.6
|
15
13
|
gemfile: Gemfile
|
16
|
-
- rvm: 2.
|
14
|
+
- rvm: 2.5
|
17
15
|
gemfile: gemfiles/activerecord42.gemfile
|
18
16
|
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
# 1.3.4
|
2
|
+
|
3
|
+
## Fixed
|
4
|
+
* Performance and memory improvements ([@krzysiek1507][])[#140](https://github.com/nesaulov/surrealist/pull/140)
|
5
|
+
* Fix serializing when direct instance method call returns nil ([@past-one][]) [#141](https://github.com/nesaulov/surrealist/pull/141)
|
6
|
+
|
7
|
+
# 1.3.3
|
8
|
+
|
9
|
+
## Fixed
|
10
|
+
* Struct serialization ([@wildkain][]) [#135](https://github.com/nesaulov/surrealist/pull/135)
|
11
|
+
|
12
|
+
# 1.3.2
|
13
|
+
|
14
|
+
## Fixed
|
15
|
+
* Preserve exception backtrace in case of Surrealist::UndefinedMethodError ([@akxcv][]) [#122](https://github.com/nesaulov/surrealist/pull/122)
|
16
|
+
* Fix nil values for Boolean type ([@nesaulov][]) [#127](https://github.com/nesaulov/surrealist/pull/127)
|
17
|
+
|
1
18
|
# 1.3.1
|
2
19
|
|
3
20
|
## Fixed
|
@@ -121,3 +138,5 @@
|
|
121
138
|
[@stefkin]: https://github.com/stefkin
|
122
139
|
[@gjhenrique]: https://github.com/gjhenrique
|
123
140
|
[@kolasss]: https://github.com/kolasss
|
141
|
+
[@wildkain]: https://github.com/wildkain
|
142
|
+
[@krzysiek1507]: https://github.com/krzysiek1507
|
data/Gemfile
CHANGED
@@ -4,17 +4,17 @@ source 'https://rubygems.org'
|
|
4
4
|
gemspec
|
5
5
|
|
6
6
|
group :development, :test do
|
7
|
-
gem 'active_model_serializers', '~> 0.10
|
7
|
+
gem 'active_model_serializers', '~> 0.10'
|
8
8
|
gem 'activerecord'
|
9
9
|
gem 'benchmark-ips'
|
10
10
|
gem 'blueprinter'
|
11
11
|
gem 'coveralls', require: false
|
12
12
|
gem 'dry-struct'
|
13
13
|
gem 'dry-types'
|
14
|
-
gem 'rom', '~>
|
14
|
+
gem 'rom', '~> 5.0'
|
15
15
|
gem 'rom-repository'
|
16
16
|
gem 'rom-sql'
|
17
17
|
gem 'sequel'
|
18
|
-
gem 'sqlite3'
|
18
|
+
gem 'sqlite3', '~> 1.4'
|
19
19
|
gem 'yard', require: false unless ENV['TRAVIS']
|
20
20
|
end
|
data/README.md
CHANGED
@@ -4,11 +4,11 @@
|
|
4
4
|
[![Inline docs](http://inch-ci.org/github/nesaulov/surrealist.svg?branch=master)](http://inch-ci.org/github/nesaulov/surrealist)
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/surrealist.svg)](https://rubygems.org/gems/surrealist)
|
6
6
|
[![Open Source Helpers](https://www.codetriage.com/nesaulov/surrealist/badges/users.svg)](https://www.codetriage.com/nesaulov/surrealist)
|
7
|
+
[![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)
|
7
8
|
|
8
9
|
![Surrealist](surrealist-icon.png)
|
9
10
|
|
10
|
-
|
11
|
-
by defining a `json_schema`. It also provides a trivial type checking in the runtime before serialization.
|
11
|
+
Surrealist is a schema-driven object serialization ruby library. It also provides a trivial type checking in the runtime before serialization.
|
12
12
|
[Yard documentation](http://www.rubydoc.info/github/nesaulov/surrealist/master)
|
13
13
|
|
14
14
|
|
@@ -497,10 +497,10 @@ class Car
|
|
497
497
|
|
498
498
|
json_schema do
|
499
499
|
{
|
500
|
-
age: Types::Coercible::
|
500
|
+
age: Types::Coercible::Integer,
|
501
501
|
brand: Types::Coercible::String,
|
502
|
-
doors: Types::
|
503
|
-
horsepower: Types::Strict::
|
502
|
+
doors: Types::Integer.optional,
|
503
|
+
horsepower: Types::Strict::Integer.constrained(gteq: 20),
|
504
504
|
fuel_system: Types::Any,
|
505
505
|
previous_owner: Types::String,
|
506
506
|
}
|
@@ -781,7 +781,7 @@ Car.new.surrealize
|
|
781
781
|
### Other notes
|
782
782
|
* nil values are allowed by default, so if you have, say, `age: String`, but the actual value is nil,
|
783
783
|
type check will be passed. If you want to be strict about `nil`s consider using `Dry::Types`.
|
784
|
-
* Surrealist [officially supports](https://travis-ci.org/nesaulov/surrealist) MRI Ruby 2.
|
784
|
+
* Surrealist [officially supports](https://travis-ci.org/nesaulov/surrealist) MRI Ruby 2.3+ but should be working on other platforms as well.
|
785
785
|
|
786
786
|
## Roadmap
|
787
787
|
Here is a list of features that are not implemented yet (contributions are welcome):
|
data/Rakefile
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# rubocop:disable Metrics/MethodLength
|
2
4
|
require_relative '../lib/surrealist'
|
3
5
|
require 'benchmark/ips'
|
4
6
|
require 'active_record'
|
@@ -7,7 +9,7 @@ require 'active_model_serializers'
|
|
7
9
|
require 'blueprinter'
|
8
10
|
|
9
11
|
ActiveRecord::Base.establish_connection(
|
10
|
-
adapter:
|
12
|
+
adapter: 'sqlite3',
|
11
13
|
database: ':memory:',
|
12
14
|
)
|
13
15
|
|
@@ -34,7 +36,7 @@ end
|
|
34
36
|
ActiveModelSerializers.config.adapter = :json
|
35
37
|
|
36
38
|
def random_name
|
37
|
-
('a'..'z').to_a.shuffle.join
|
39
|
+
('a'..'z').to_a.shuffle.join.first(10).capitalize
|
38
40
|
end
|
39
41
|
|
40
42
|
class User < ActiveRecord::Base
|
@@ -302,4 +304,4 @@ benchmark_associations_collection
|
|
302
304
|
# Surrealist (associations): collection through Surrealist.surrealize_collection(): 2.4 i/s
|
303
305
|
# Surrealist (associations): collection through Surrealist::Serializer: 2.4 i/s - 1.03x slower
|
304
306
|
# AMS (associations): collection: 1.5 i/s - 1.60x slower
|
305
|
-
# rubocop:enable Metrics/
|
307
|
+
# rubocop:enable Metrics/MethodLength
|
data/bin/console
CHANGED
@@ -7,11 +7,11 @@ group :development, :test do
|
|
7
7
|
gem 'coveralls', require: false
|
8
8
|
gem 'dry-struct'
|
9
9
|
gem 'dry-types'
|
10
|
-
gem 'rom', '~>
|
10
|
+
gem 'rom', '~> 5.0'
|
11
11
|
gem 'rom-repository'
|
12
12
|
gem 'rom-sql'
|
13
13
|
gem 'sequel'
|
14
|
-
gem 'sqlite3'
|
14
|
+
gem 'sqlite3', '~> 1.3.6'
|
15
15
|
gem 'yard', require: false unless ENV['TRAVIS']
|
16
16
|
end
|
17
17
|
|
data/lib/surrealist/builder.rb
CHANGED
@@ -4,7 +4,8 @@ module Surrealist
|
|
4
4
|
# A class that builds a hash from the schema and type-checks the values.
|
5
5
|
class Builder
|
6
6
|
# Struct to carry schema along
|
7
|
-
Schema = Struct.new(:key, :value)
|
7
|
+
Schema = Struct.new(:key, :value)
|
8
|
+
Schema.freeze
|
8
9
|
|
9
10
|
# @param [Carrier] carrier instance of Surrealist::Carrier
|
10
11
|
# @param [Hash] schema the schema defined in the object's class.
|
data/lib/surrealist/carrier.rb
CHANGED
@@ -4,7 +4,7 @@ module Surrealist
|
|
4
4
|
# A data structure to carry arguments across methods.
|
5
5
|
# @api private
|
6
6
|
class Carrier
|
7
|
-
BOOLEANS = [true, false].freeze
|
7
|
+
BOOLEANS = [true, false, nil].freeze
|
8
8
|
|
9
9
|
attr_accessor :camelize, :include_root, :include_namespaces, :root, :namespaces_nesting_level
|
10
10
|
|
@@ -22,7 +22,7 @@ module Surrealist
|
|
22
22
|
#
|
23
23
|
# @return [Carrier] self if type checks were passed.
|
24
24
|
def self.call(**args)
|
25
|
-
new(args).sanitize!
|
25
|
+
new(**args).sanitize!
|
26
26
|
end
|
27
27
|
|
28
28
|
def initialize(**args)
|
@@ -64,7 +64,7 @@ module Surrealist
|
|
64
64
|
def check_booleans!
|
65
65
|
booleans_hash.each do |key, value|
|
66
66
|
unless BOOLEANS.include?(value)
|
67
|
-
raise ArgumentError, "Expected `#{key}` to be either true or
|
67
|
+
raise ArgumentError, "Expected `#{key}` to be either true, false or nil, got #{value}"
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
@@ -30,9 +30,9 @@ module Surrealist
|
|
30
30
|
|
31
31
|
# A class that raises all Surrealist exceptions
|
32
32
|
module ExceptionRaiser
|
33
|
-
CLASS_NAME_NOT_PASSED = "Can't wrap schema in root key - class name was not passed"
|
34
|
-
MUST_BEHAVE_LIKE_ENUMERABLE = "Can't serialize collection - must behave like enumerable"
|
35
|
-
CLASS_DOESNT_INCLUDE_SURREALIST = 'Class does not include Surrealist'
|
33
|
+
CLASS_NAME_NOT_PASSED = "Can't wrap schema in root key - class name was not passed"
|
34
|
+
MUST_BEHAVE_LIKE_ENUMERABLE = "Can't serialize collection - must behave like enumerable"
|
35
|
+
CLASS_DOESNT_INCLUDE_SURREALIST = 'Class does not include Surrealist'
|
36
36
|
|
37
37
|
class << self
|
38
38
|
# Raises Surrealist::InvalidSchemaDelegation if destination of delegation does not
|
@@ -90,7 +90,8 @@ module Surrealist
|
|
90
90
|
def raise_invalid_key!(err)
|
91
91
|
raise Surrealist::UndefinedMethodError,
|
92
92
|
"#{err.message}. You have probably defined a key " \
|
93
|
-
"in the schema that doesn't have a corresponding method."
|
93
|
+
"in the schema that doesn't have a corresponding method.",
|
94
|
+
err.backtrace
|
94
95
|
end
|
95
96
|
|
96
97
|
# Raises ArgumentError if a tag has no corresponding serializer
|
@@ -12,10 +12,10 @@ module Surrealist
|
|
12
12
|
#
|
13
13
|
# @return [Hash] camelized hash.
|
14
14
|
def camelize_hash(hash)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
return hash unless hash.is_a?(Hash)
|
16
|
+
|
17
|
+
hash.each_with_object({}) do |(k, v), obj|
|
18
|
+
obj[camelize_key(k, first_upper: false)] = camelize_hash(v)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -27,11 +27,12 @@ module Surrealist
|
|
27
27
|
# @param [Boolean] first_upper should the first letter be capitalized.
|
28
28
|
#
|
29
29
|
# @return [String | Symbol] camelized key of a hash.
|
30
|
-
def camelize_key(key, first_upper
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
def camelize_key(key, first_upper: true)
|
31
|
+
case key
|
32
|
+
when Symbol
|
33
|
+
Surrealist::StringUtils.camelize(key.to_s, first_upper: first_upper).to_sym
|
34
|
+
when String
|
35
|
+
Surrealist::StringUtils.camelize(key, first_upper: first_upper)
|
35
36
|
else
|
36
37
|
key
|
37
38
|
end
|
data/lib/surrealist/helper.rb
CHANGED
@@ -16,7 +16,9 @@ module Surrealist
|
|
16
16
|
# 4.2 AR relation object did not include Enumerable (it defined
|
17
17
|
# all necessary method through ActiveRecord::Delegation module),
|
18
18
|
# so we need to explicitly check for this
|
19
|
-
|
19
|
+
return false if object.is_a?(Struct)
|
20
|
+
|
21
|
+
object.is_a?(Enumerable) && !object.instance_of?(Hash) || ar_relation?(object)
|
20
22
|
end
|
21
23
|
|
22
24
|
def self.ar_relation?(object)
|
@@ -49,10 +49,10 @@ module Surrealist
|
|
49
49
|
# # => "{\"name\":\"Nikita\",\"age\":23}"
|
50
50
|
# # For more examples see README
|
51
51
|
def surrealize(**args)
|
52
|
-
return args[:serializer].new(self).surrealize(args) if args[:serializer]
|
52
|
+
return args[:serializer].new(self).surrealize(**args) if args[:serializer]
|
53
53
|
|
54
54
|
if (serializer = find_serializer(args[:for]))
|
55
|
-
return serializer.new(self).surrealize(args)
|
55
|
+
return serializer.new(self).surrealize(**args)
|
56
56
|
end
|
57
57
|
|
58
58
|
Oj.dump(Surrealist.build_schema(instance: self, **args), mode: :compat)
|
@@ -60,10 +60,10 @@ module Surrealist
|
|
60
60
|
|
61
61
|
# Invokes +Surrealist+'s class method +build_schema+
|
62
62
|
def build_schema(**args)
|
63
|
-
return args[:serializer].new(self).build_schema(args) if args[:serializer]
|
63
|
+
return args[:serializer].new(self).build_schema(**args) if args[:serializer]
|
64
64
|
|
65
65
|
if (serializer = find_serializer(args[:for]))
|
66
|
-
return serializer.new(self).build_schema(args)
|
66
|
+
return serializer.new(self).build_schema(**args)
|
67
67
|
end
|
68
68
|
|
69
69
|
Surrealist.build_schema(instance: self, **args)
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Surrealist
|
4
4
|
# A class that defines a method on the object that stores the schema.
|
5
5
|
module SchemaDefiner
|
6
|
-
SCHEMA_TYPE_ERROR = 'Schema should be defined as a hash'
|
6
|
+
SCHEMA_TYPE_ERROR = 'Schema should be defined as a hash'
|
7
7
|
|
8
8
|
class << self
|
9
9
|
# Defines an instance variable on the object that stores the schema.
|
@@ -81,7 +81,7 @@ module Surrealist
|
|
81
81
|
# Checks whether object is a collection or an instance and serializes it
|
82
82
|
def surrealize(**args)
|
83
83
|
if Helper.collection?(object)
|
84
|
-
Surrealist.surrealize_collection(object, args.merge(context: context))
|
84
|
+
Surrealist.surrealize_collection(object, **args.merge(context: context))
|
85
85
|
else
|
86
86
|
Surrealist.surrealize(instance: self, **args)
|
87
87
|
end
|
@@ -90,7 +90,7 @@ module Surrealist
|
|
90
90
|
# Passes build_schema to Surrealist
|
91
91
|
def build_schema(**args)
|
92
92
|
if Helper.collection?(object)
|
93
|
-
build_collection_schema(args)
|
93
|
+
build_collection_schema(**args)
|
94
94
|
else
|
95
95
|
Surrealist.build_schema(instance: self, **args)
|
96
96
|
end
|
@@ -102,12 +102,14 @@ module Surrealist
|
|
102
102
|
|
103
103
|
# Maps collection and builds schema for each instance.
|
104
104
|
def build_collection_schema(**args)
|
105
|
-
object.map { |object| self.class.new(object, context).build_schema(args) }
|
105
|
+
object.map { |object| self.class.new(object, **context).build_schema(**args) }
|
106
106
|
end
|
107
107
|
|
108
108
|
# Methods not found inside serializer will be invoked on the object
|
109
109
|
def method_missing(method, *args, &block)
|
110
|
-
object.
|
110
|
+
return super unless object.respond_to?(method)
|
111
|
+
|
112
|
+
object.public_send(method, *args, &block)
|
111
113
|
end
|
112
114
|
|
113
115
|
# Methods not found inside serializer will be invoked on the object
|
@@ -3,14 +3,14 @@
|
|
3
3
|
module Surrealist
|
4
4
|
# A helper class for strings transformations.
|
5
5
|
module StringUtils
|
6
|
-
DASH = '-'
|
7
|
-
UNDERSCORE = '_'
|
8
|
-
EMPTY_STRING = ''
|
9
|
-
DASH_REGEXP1 = /([A-Z]+)([A-Z][a-z])/o
|
10
|
-
DASH_REGEXP2 = /([a-z\d])([A-Z])/o
|
11
|
-
UNDERSCORE_REGEXP = /(?:^|_)([^_\s]+)/o
|
12
|
-
NAMESPACES_SEPARATOR = '::'
|
13
|
-
UNDERSCORE_SUBSTITUTE = '\1_\2'
|
6
|
+
DASH = '-'
|
7
|
+
UNDERSCORE = '_'
|
8
|
+
EMPTY_STRING = ''
|
9
|
+
DASH_REGEXP1 = /([A-Z]+)([A-Z][a-z])/o.freeze
|
10
|
+
DASH_REGEXP2 = /([a-z\d])([A-Z])/o.freeze
|
11
|
+
UNDERSCORE_REGEXP = /(?:^|_)([^_\s]+)/o.freeze
|
12
|
+
NAMESPACES_SEPARATOR = '::'
|
13
|
+
UNDERSCORE_SUBSTITUTE = '\1_\2'
|
14
14
|
|
15
15
|
class << self
|
16
16
|
# Converts a string to snake_case.
|
@@ -19,11 +19,12 @@ module Surrealist
|
|
19
19
|
#
|
20
20
|
# @return [String] new underscored string.
|
21
21
|
def underscore(string)
|
22
|
-
string.gsub(NAMESPACES_SEPARATOR, UNDERSCORE)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
dup = string.gsub(NAMESPACES_SEPARATOR, UNDERSCORE)
|
23
|
+
dup.gsub!(DASH_REGEXP1, UNDERSCORE_SUBSTITUTE)
|
24
|
+
dup.gsub!(DASH_REGEXP2, UNDERSCORE_SUBSTITUTE)
|
25
|
+
dup.tr!(DASH, UNDERSCORE)
|
26
|
+
dup.downcase!
|
27
|
+
dup
|
27
28
|
end
|
28
29
|
|
29
30
|
# Camelizes a string.
|
@@ -32,7 +33,7 @@ module Surrealist
|
|
32
33
|
# @param [Boolean] first_upper should the first letter be capitalized.
|
33
34
|
#
|
34
35
|
# @return [String] camelized string.
|
35
|
-
def camelize(snake_string, first_upper
|
36
|
+
def camelize(snake_string, first_upper: true)
|
36
37
|
if first_upper
|
37
38
|
snake_string.to_s.gsub(UNDERSCORE_REGEXP) { Regexp.last_match[1].capitalize }
|
38
39
|
else
|
@@ -69,10 +70,12 @@ module Surrealist
|
|
69
70
|
#
|
70
71
|
# @return [Hash] a nested hash.
|
71
72
|
def break_namespaces(klass, camelize, nesting_level)
|
72
|
-
Surrealist::ExceptionRaiser.raise_invalid_nesting!(nesting_level) unless nesting_level
|
73
|
+
Surrealist::ExceptionRaiser.raise_invalid_nesting!(nesting_level) unless nesting_level.positive?
|
73
74
|
|
74
|
-
klass.split(NAMESPACES_SEPARATOR).last(nesting_level).reverse
|
75
|
-
camelize ?
|
75
|
+
klass.split(NAMESPACES_SEPARATOR).last(nesting_level).reverse!.inject({}) do |a, n|
|
76
|
+
key = (camelize ? camelize(uncapitalize(n), first_upper: false) : underscore(n)).to_sym
|
77
|
+
|
78
|
+
{ key => a }
|
76
79
|
end
|
77
80
|
end
|
78
81
|
|
@@ -4,7 +4,7 @@ module Surrealist
|
|
4
4
|
# Service class for type checking
|
5
5
|
module TypeHelper
|
6
6
|
# Dry-types class matcher
|
7
|
-
DRY_TYPE_CLASS = /Dry::Types
|
7
|
+
DRY_TYPE_CLASS = /Dry::Types/.freeze
|
8
8
|
|
9
9
|
class << self
|
10
10
|
# Checks if value returned from a method is an instance of type class specified
|
@@ -1,16 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Surrealist
|
2
4
|
# Module for finding and setting hash into vars
|
3
5
|
module VarsHelper
|
4
6
|
# Instance variable name that is set by SchemaDefiner
|
5
|
-
INSTANCE_VARIABLE = '@__surrealist_schema'
|
7
|
+
INSTANCE_VARIABLE = '@__surrealist_schema'
|
6
8
|
# Instance's parent instance variable name that is set by SchemaDefiner
|
7
|
-
PARENT_VARIABLE = '@__surrealist_schema_parent'
|
9
|
+
PARENT_VARIABLE = '@__surrealist_schema_parent'
|
8
10
|
# Class variable name that is set by SchemaDefiner
|
9
|
-
CLASS_VARIABLE = '@@__surrealist_schema'
|
11
|
+
CLASS_VARIABLE = '@@__surrealist_schema'
|
10
12
|
# Regexp to resolve ROM structure
|
11
|
-
ROM_REGEXP = /ROM::Struct/o
|
13
|
+
ROM_REGEXP = /ROM::Struct/o.freeze
|
12
14
|
# Instance variable that keeps serializer classes
|
13
|
-
SERIALIZER_CLASSES = '@__surrealist_serializers'
|
15
|
+
SERIALIZER_CLASSES = '@__surrealist_serializers'
|
14
16
|
# Tag for default behaviour in multiple serializers
|
15
17
|
DEFAULT_TAG = :default
|
16
18
|
|
@@ -49,7 +51,7 @@ module Surrealist
|
|
49
51
|
def find_serializer(klass, tag: nil)
|
50
52
|
tag ||= DEFAULT_TAG
|
51
53
|
hash = klass.instance_variable_get(SERIALIZER_CLASSES)
|
52
|
-
serializer = hash
|
54
|
+
serializer = hash&.fetch(tag.to_sym, nil)
|
53
55
|
Surrealist::ExceptionRaiser.raise_unknown_tag!(tag) if serializer.nil? && tag != DEFAULT_TAG
|
54
56
|
serializer
|
55
57
|
end
|
data/lib/surrealist/version.rb
CHANGED
data/lib/surrealist/wrapper.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Surrealist
|
2
|
-
# A helper class for
|
4
|
+
# A helper class for wrapping hashes.
|
3
5
|
module Wrapper
|
4
6
|
class << self
|
5
7
|
# Wraps the schema hash into root/namespaces if there is a need to.
|
@@ -9,8 +11,8 @@ module Surrealist
|
|
9
11
|
# @param [String] klass instance's class name.
|
10
12
|
#
|
11
13
|
# @return [Hash] a wrapped hash.
|
12
|
-
def wrap(hash, carrier, klass
|
13
|
-
namespaces_condition = carrier.include_namespaces || carrier.namespaces_nesting_level != DEFAULT_NESTING_LEVEL # rubocop:disable
|
14
|
+
def wrap(hash, carrier, klass: false)
|
15
|
+
namespaces_condition = carrier.include_namespaces || carrier.namespaces_nesting_level != DEFAULT_NESTING_LEVEL # rubocop:disable Layout/LineLength
|
14
16
|
|
15
17
|
if !klass && (carrier.include_root || namespaces_condition)
|
16
18
|
Surrealist::ExceptionRaiser.raise_unknown_root!
|
@@ -54,11 +56,11 @@ module Surrealist
|
|
54
56
|
# @return [Hash] a hash with schema wrapped inside a root key.
|
55
57
|
def wrap_schema_into_root(schema, carrier, root)
|
56
58
|
root_key = if carrier.camelize
|
57
|
-
Surrealist::StringUtils.camelize(root, false).to_sym
|
59
|
+
Surrealist::StringUtils.camelize(root, first_upper: false).to_sym
|
58
60
|
else
|
59
61
|
Surrealist::StringUtils.underscore(root).to_sym
|
60
62
|
end
|
61
|
-
result =
|
63
|
+
result = { root_key => {} }
|
62
64
|
Surrealist::Copier.deep_copy(schema, result[root_key])
|
63
65
|
|
64
66
|
result
|
data/lib/surrealist.rb
CHANGED
@@ -59,7 +59,7 @@ module Surrealist
|
|
59
59
|
Surrealist::ExceptionRaiser.raise_invalid_collection! unless Helper.collection?(collection)
|
60
60
|
|
61
61
|
result = collection.map do |object|
|
62
|
-
Helper.surrealist?(object.class) ? __build_schema(object, args) : object
|
62
|
+
Helper.surrealist?(object.class) ? __build_schema(object, **args) : object
|
63
63
|
end
|
64
64
|
|
65
65
|
args[:raw] ? result : Oj.dump(result, mode: :compat)
|
@@ -128,13 +128,13 @@ module Surrealist
|
|
128
128
|
schema = Surrealist::VarsHelper.find_schema(instance.class)
|
129
129
|
Surrealist::ExceptionRaiser.raise_unknown_schema!(instance) if schema.nil?
|
130
130
|
|
131
|
-
parameters = config ? config.merge(args) : args
|
131
|
+
parameters = config ? config.merge(**args) : args
|
132
132
|
|
133
133
|
# TODO: Refactor (something pipeline-like would do here, perhaps a builder of some sort)
|
134
|
-
carrier = Surrealist::Carrier.call(parameters)
|
134
|
+
carrier = Surrealist::Carrier.call(**parameters)
|
135
135
|
copied_schema = Surrealist::Copier.deep_copy(schema)
|
136
136
|
built_schema = Builder.new(carrier, copied_schema, instance).call
|
137
|
-
wrapped_schema = Surrealist::Wrapper.wrap(built_schema, carrier, instance.class.name)
|
137
|
+
wrapped_schema = Surrealist::Wrapper.wrap(built_schema, carrier, klass: instance.class.name)
|
138
138
|
carrier.camelize ? Surrealist::HashUtils.camelize_hash(wrapped_schema) : wrapped_schema
|
139
139
|
end
|
140
140
|
# rubocop:enable Metrics/AbcSize
|
@@ -178,10 +178,10 @@ module Surrealist
|
|
178
178
|
# provided in the object's class. Values will be taken from the return values
|
179
179
|
# of appropriate methods from the object.
|
180
180
|
def __build_schema(object, **args)
|
181
|
-
return args[:serializer].new(object, args[:context].to_h).build_schema(args) if args[:serializer]
|
181
|
+
return args[:serializer].new(object, **args[:context].to_h).build_schema(**args) if args[:serializer]
|
182
182
|
|
183
183
|
if (serializer = Surrealist::VarsHelper.find_serializer(object.class, tag: args[:for]))
|
184
|
-
serializer.new(object, args[:context].to_h).build_schema(args)
|
184
|
+
serializer.new(object, **args[:context].to_h).build_schema(**args)
|
185
185
|
else
|
186
186
|
build_schema(instance: object, **args)
|
187
187
|
end
|
data/surrealist.gemspec
CHANGED
@@ -22,13 +22,13 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.bindir = 'exe'
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ['lib']
|
25
|
-
spec.required_ruby_version = '>= 2.
|
25
|
+
spec.required_ruby_version = '>= 2.5.0'
|
26
26
|
|
27
|
-
spec.add_runtime_dependency 'oj', '~> 3.
|
27
|
+
spec.add_runtime_dependency 'oj', '~> 3.11'
|
28
28
|
|
29
|
-
spec.add_development_dependency 'bundler', '~>
|
30
|
-
spec.add_development_dependency 'pry', '~> 0.
|
31
|
-
spec.add_development_dependency 'rake', '~>
|
32
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
33
|
-
spec.add_development_dependency 'rubocop', '~>
|
29
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
30
|
+
spec.add_development_dependency 'pry', '~> 0.13'
|
31
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
33
|
+
spec.add_development_dependency 'rubocop', '~> 1.9'
|
34
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: surrealist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikita Esaulov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -16,84 +16,84 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '3.
|
19
|
+
version: '3.11'
|
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: '3.
|
26
|
+
version: '3.11'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '2.0'
|
34
34
|
type: :development
|
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: '2.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pry
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0.
|
47
|
+
version: '0.13'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0.
|
54
|
+
version: '0.13'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '13.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '13.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '3.
|
75
|
+
version: '3.10'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '3.
|
82
|
+
version: '3.10'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '1.9'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '1.9'
|
97
97
|
description: A gem that provides DSL for serialization of plain old Ruby objects to
|
98
98
|
JSON in a declarative style by defining a `schema`. It also provides a trivial type
|
99
99
|
checking in the runtime before serialization.
|
@@ -143,7 +143,7 @@ homepage: https://github.com/nesaulov/surrealist
|
|
143
143
|
licenses:
|
144
144
|
- MIT
|
145
145
|
metadata: {}
|
146
|
-
post_install_message:
|
146
|
+
post_install_message:
|
147
147
|
rdoc_options: []
|
148
148
|
require_paths:
|
149
149
|
- lib
|
@@ -151,16 +151,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
151
151
|
requirements:
|
152
152
|
- - ">="
|
153
153
|
- !ruby/object:Gem::Version
|
154
|
-
version: 2.
|
154
|
+
version: 2.5.0
|
155
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
157
|
- - ">="
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
requirements: []
|
161
|
-
|
162
|
-
|
163
|
-
signing_key:
|
161
|
+
rubygems_version: 3.2.3
|
162
|
+
signing_key:
|
164
163
|
specification_version: 4
|
165
164
|
summary: A gem that provides DSL for serialization of plain old Ruby objects to JSON
|
166
165
|
in a declarative style.
|