rice_bubble 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +21 -6
- data/lib/rice_bubble/serializer.rb +25 -9
- data/lib/rice_bubble/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a6ac1880406c9b129d4d9f0fa1029acc21a46cb1a3c9be1feaece000fc99e5c
|
4
|
+
data.tar.gz: 6f684b2691b37a6ccb30cd73aa4d45ab689546377f184e3fb33d8e5eab8e22e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4936fab7ed2f4a4d5ba5e044606f11ce6311e51b287f4bcdc4024113b90d2ae0e2323eb63af42b7a860bc9ccba9b4c1bc785ebfd555d8e49fd7e7af57862a261
|
7
|
+
data.tar.gz: 04fb49f5fd9e425b2df7311ef7301e6613a279301de442047b171a90a2ee1b063d4de117255c4f75a2e01e96de458220be06ba2704b5eed3f85fa5313fb19b6a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -37,9 +37,11 @@ end
|
|
37
37
|
Then you can use the new serializer to convert your data to a JSON-friendly representation:
|
38
38
|
|
39
39
|
```ruby
|
40
|
-
json = CharacterSerializer.
|
40
|
+
json = CharacterSerializer.new(my_character).call # => { name: "...", ... }
|
41
41
|
```
|
42
42
|
|
43
|
+
**Note:** you can also use `CharacterSerializer.call(my_character)`.
|
44
|
+
|
43
45
|
### Attribute types
|
44
46
|
|
45
47
|
| Name | Expects | Options | Example |
|
@@ -100,12 +102,25 @@ WrappingSerializer.call({ name: 'Spicy Beef' }) # => { name: '{Spicy Beef}' }
|
|
100
102
|
### Fetching values from an object
|
101
103
|
|
102
104
|
By default, attributes know how to fetch their values from the object being serialized
|
103
|
-
by either:
|
105
|
+
by either (in this order):
|
106
|
+
|
107
|
+
- invoking an instance method with the same name on the serializer; or
|
108
|
+
- invoking an instance method with the same name on the object; or
|
109
|
+
- looking up a hash key on the object
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class FullNameSerializer
|
113
|
+
attributes(
|
114
|
+
full_name: string
|
115
|
+
)
|
104
116
|
|
105
|
-
|
106
|
-
|
117
|
+
def full_name
|
118
|
+
[object.first_name, object.last_name].join(' ')
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
107
122
|
|
108
|
-
You can change how a value is retrieved by passing a block to the attribute:
|
123
|
+
You can also change how a value is retrieved by passing a block to the attribute:
|
109
124
|
|
110
125
|
```ruby
|
111
126
|
class FullNameSerializer
|
@@ -134,4 +149,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
134
149
|
|
135
150
|
## Code of Conduct
|
136
151
|
|
137
|
-
Everyone interacting in the RiceBubble project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://
|
152
|
+
Everyone interacting in the RiceBubble project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://gitlab.com/fauxparse/rice_bubble/blob/main/CODE_OF_CONDUCT.md).
|
@@ -1,17 +1,33 @@
|
|
1
1
|
module RiceBubble
|
2
2
|
class Serializer
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
attr_reader :object
|
4
|
+
|
5
|
+
def initialize(object = nil)
|
6
|
+
@object = object
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(object_to_serialize = nil, path: nil)
|
10
|
+
if object_to_serialize
|
11
|
+
self.class.new(object_to_serialize).call(path:)
|
12
|
+
elsif object
|
13
|
+
self.class.attributes.map do |name, attr|
|
14
|
+
attr.call(fetch(name), path: path || self.class.name)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
raise ArgumentError, 'no object to serialize'
|
6
18
|
end
|
7
19
|
end
|
8
20
|
|
9
|
-
def
|
10
|
-
|
21
|
+
def fetch(name)
|
22
|
+
if respond_to?(name)
|
23
|
+
public_send(name)
|
24
|
+
else
|
25
|
+
self.class.attributes[name].fetch(object, name)
|
26
|
+
end
|
11
27
|
end
|
12
28
|
|
13
29
|
def valid?(object)
|
14
|
-
attributes.all? do |name, attr|
|
30
|
+
self.class.attributes.all? do |name, attr|
|
15
31
|
attr.valid?(attr.fetch(object, name))
|
16
32
|
end
|
17
33
|
end
|
@@ -19,7 +35,7 @@ module RiceBubble
|
|
19
35
|
def validate!(object, path: '', **)
|
20
36
|
path = self.class.name if path.empty?
|
21
37
|
|
22
|
-
attributes.each do |name, attr|
|
38
|
+
self.class.attributes.each do |name, attr|
|
23
39
|
value = attr.fetch(object, name)
|
24
40
|
attr.validate!(
|
25
41
|
value,
|
@@ -34,8 +50,8 @@ module RiceBubble
|
|
34
50
|
end
|
35
51
|
|
36
52
|
class << self
|
37
|
-
def call(...)
|
38
|
-
new.call(...)
|
53
|
+
def call(object = nil, ...)
|
54
|
+
new(object).call(...)
|
39
55
|
end
|
40
56
|
|
41
57
|
def attributes(attrs = nil, &)
|
data/lib/rice_bubble/version.rb
CHANGED