surrealist 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -1
- data/README.md +12 -3
- data/lib/surrealist/class_methods.rb +30 -0
- data/lib/surrealist/value_assigner.rb +14 -1
- data/lib/surrealist/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6470fa2a8f31edfdb08c858e7f74bb92add85aa887bf5a3e968521b88585adec
|
4
|
+
data.tar.gz: 703ce5280e32c246d5c51bd6be1d4a0e0688d01dcaaeb195aa1a5867e762e86e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f95d432d163188e66ae51cea051db55cf9fbfb05ae43b0e8096c0275a3d78f317a1a716fb2dc6900b3dc2deae82216ef2b9f09d062db0c00301c0cd2f2459a6
|
7
|
+
data.tar.gz: 71575656f64a1d6110f56623ecaa04d186478702c46f777967a17478fc180c4b6a326e4221a1c48f0def25036d68f307fd6eba2c62881120444b042808c32514
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# 1.2.0
|
2
|
+
|
3
|
+
## Added
|
4
|
+
* `.defined_schema` to return the schema that has been defined with `json_schema` ([@glaucocustodio][]) [#98](https://github.com/nesaulov/surrealist/pull/98)
|
5
|
+
|
6
|
+
## Fixed
|
7
|
+
* Incorrect method delegation ([@nesaulov][]) [#103](https://github.com/nesaulov/surrealist/pull/103)
|
8
|
+
|
1
9
|
# 1.1.2
|
2
10
|
|
3
11
|
## Fixed
|
@@ -87,6 +95,7 @@
|
|
87
95
|
* Allow nil values by default.
|
88
96
|
* Allow nested objects.
|
89
97
|
|
98
|
+
[@glaucocustodio]: https://github.com/glaucocustodio
|
90
99
|
[@nesaulov]: https://github.com/nesaulov
|
91
100
|
[@AlessandroMinali]: https://github.com/AlessandroMinali
|
92
101
|
[@nulldef]: https://github.com/nulldef
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -27,6 +27,7 @@ to serialize nested objects and structures. [Introductory blogpost.](https://med
|
|
27
27
|
* [Defining custom serializers](#defining-custom-serializers)
|
28
28
|
* [Multiple serializers](#multiple-serializers)
|
29
29
|
* [Build schema](#build-schema)
|
30
|
+
* [Defined schema](#defined-schema)
|
30
31
|
* [Working with ORMs](#working-with-orms)
|
31
32
|
* [ActiveRecord](#activerecord)
|
32
33
|
* [ROM](#rom)
|
@@ -260,19 +261,19 @@ you have to define yourself. DSL looks as follows
|
|
260
261
|
class IncomeSerializer < Surrealist::Serializer
|
261
262
|
serializer_context :current_user
|
262
263
|
json_schema { { amount: Integer } }
|
263
|
-
|
264
|
+
|
264
265
|
def amount
|
265
266
|
current_user.guest? ? 100000000 : object.amount
|
266
267
|
end
|
267
268
|
end
|
268
|
-
```
|
269
|
+
```
|
269
270
|
`.serializer_context` takes an array of symbols and dynamically defines instance methods
|
270
271
|
that read values from the context hash. So `.serializer_context :current_user` will become
|
271
272
|
``` ruby
|
272
273
|
def current_user
|
273
274
|
context[:current_user]
|
274
275
|
end
|
275
|
-
```
|
276
|
+
```
|
276
277
|
There is also an alias in the plural form: `.serializer_contexts`.
|
277
278
|
### Multiple serializers
|
278
279
|
|
@@ -321,6 +322,14 @@ Car.new.build_schema
|
|
321
322
|
# => { age: 7, brand: "Toyota", doors: nil, horsepower: 140, fuel_system: "Direct injection", previous_owner: "John Doe" }
|
322
323
|
```
|
323
324
|
|
325
|
+
### Defined schema
|
326
|
+
Use the `.defined_schema` method to get the schema that has been defined with `json_schema`:
|
327
|
+
|
328
|
+
``` ruby
|
329
|
+
User.defined_schema
|
330
|
+
# => { name: String, age: Integer }
|
331
|
+
```
|
332
|
+
|
324
333
|
### Working with ORMs
|
325
334
|
|
326
335
|
There are two kinds of return values of ORM methods: some return collections of objects, while others return instances.
|
@@ -55,6 +55,28 @@ module Surrealist
|
|
55
55
|
SchemaDefiner.call(self, yield)
|
56
56
|
end
|
57
57
|
|
58
|
+
# A DSL method to return the defined schema.
|
59
|
+
# @example DSL usage example
|
60
|
+
# class Person
|
61
|
+
# include Surrealist
|
62
|
+
#
|
63
|
+
# json_schema do
|
64
|
+
# { name: String }
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# def name
|
68
|
+
# 'Parent'
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# Person.defined_schema
|
73
|
+
# # => { name: String }
|
74
|
+
def defined_schema
|
75
|
+
read_schema.tap do |schema|
|
76
|
+
raise UnknownSchemaError if schema.nil?
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
58
80
|
# A DSL method to delegate schema in a declarative style. Must reference a valid
|
59
81
|
# class that includes Surrealist
|
60
82
|
#
|
@@ -101,9 +123,17 @@ module Surrealist
|
|
101
123
|
def surrealize_with(klass, tag: Surrealist::VarsHelper::DEFAULT_TAG)
|
102
124
|
if klass < Surrealist::Serializer
|
103
125
|
Surrealist::VarsHelper.add_serializer(self, klass, tag: tag)
|
126
|
+
instance_variable_set(VarsHelper::PARENT_VARIABLE, klass.defined_schema)
|
104
127
|
else
|
105
128
|
raise ArgumentError, "#{klass} should be inherited from Surrealist::Serializer"
|
106
129
|
end
|
107
130
|
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def read_schema
|
135
|
+
instance_variable_get(VarsHelper::INSTANCE_VARIABLE) ||
|
136
|
+
instance_variable_get(VarsHelper::PARENT_VARIABLE)
|
137
|
+
end
|
108
138
|
end
|
109
139
|
end
|
@@ -34,10 +34,23 @@ module Surrealist
|
|
34
34
|
#
|
35
35
|
# @return [Object] value to be further processed
|
36
36
|
def raw_value(instance, schema)
|
37
|
-
value = instance.is_a?(Hash) ? instance[schema.key] : instance
|
37
|
+
value = instance.is_a?(Hash) ? instance[schema.key] : invoke_method(instance, schema.key)
|
38
38
|
coerce_value(value, schema)
|
39
39
|
end
|
40
40
|
|
41
|
+
# Checks if there is a custom serializer defined for the object and invokes the method
|
42
|
+
# on it first.
|
43
|
+
#
|
44
|
+
# @param [Object] instance an instance of a model or a serializer
|
45
|
+
# @param [Symbol] method the schema key that represents the method to be invoked
|
46
|
+
#
|
47
|
+
# @return [Object] the return value of the method
|
48
|
+
def invoke_method(instance, method)
|
49
|
+
object = instance.instance_variable_get(:@object)
|
50
|
+
|
51
|
+
object && object.respond_to?(method) ? object.send(method) : instance.send(method)
|
52
|
+
end
|
53
|
+
|
41
54
|
# Coerces value if type check is passed
|
42
55
|
#
|
43
56
|
# @param [Object] value the value to be checked and coerced
|
data/lib/surrealist/version.rb
CHANGED
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikita Esaulov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|