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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a430c1d28309d9ef1c1626e64fa3e0f482abef0215307a8ac8ea19083a44eb8b
4
- data.tar.gz: dc45cc49c8b1af7676a9ad6738c42c83e10397587d7d21908d5345ee74248ff4
3
+ metadata.gz: 6470fa2a8f31edfdb08c858e7f74bb92add85aa887bf5a3e968521b88585adec
4
+ data.tar.gz: 703ce5280e32c246d5c51bd6be1d4a0e0688d01dcaaeb195aa1a5867e762e86e
5
5
  SHA512:
6
- metadata.gz: f2ae0819199d9c5ef9d0e221f3a743612fb18c1b392922788c9741eb6ed0172c2ba9aeef566d408b30bfd4a19df2a468edf98cef025923c70e0e2a30fb17d39c
7
- data.tar.gz: e958a7d97360166bf852d8cb69b294b452176b0f981537df80478e1c5daaa817964a3d877746307d86d0018dc808ec89d58635eee594a7180c5af7a8b6070ba2
6
+ metadata.gz: 3f95d432d163188e66ae51cea051db55cf9fbfb05ae43b0e8096c0275a3d78f317a1a716fb2dc6900b3dc2deae82216ef2b9f09d062db0c00301c0cd2f2459a6
7
+ data.tar.gz: 71575656f64a1d6110f56623ecaa04d186478702c46f777967a17478fc180c4b6a326e4221a1c48f0def25036d68f307fd6eba2c62881120444b042808c32514
data/.rubocop.yml CHANGED
@@ -9,7 +9,7 @@ Documentation:
9
9
  - benchmarks/*rb
10
10
  - spec/**/*rb
11
11
 
12
- # Layout
12
+ # Layout
13
13
 
14
14
  Layout/AlignParameters:
15
15
  Enabled: true
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
@@ -10,7 +10,7 @@ group :development, :test do
10
10
  gem 'blueprinter'
11
11
  gem 'coveralls', require: false
12
12
  gem 'dry-struct'
13
- gem 'dry-types'
13
+ gem 'dry-types', '= 0.12.2'
14
14
  gem 'rom', '~> 4.0'
15
15
  gem 'rom-repository'
16
16
  gem 'rom-sql'
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.send(schema.key)
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Surrealist
4
4
  # Defines the version of Surrealist
5
- VERSION = '1.1.2'.freeze
5
+ VERSION = '1.2.0'.freeze
6
6
  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: 1.1.2
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-04-18 00:00:00.000000000 Z
11
+ date: 2018-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj