rice_bubble 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -1
- data/Gemfile.lock +1 -1
- data/README.md +21 -6
- data/lib/rice_bubble/attributes/datetime.rb +28 -0
- data/lib/rice_bubble/serializer.rb +25 -9
- data/lib/rice_bubble/version.rb +1 -1
- data/lib/rice_bubble.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ff1a10fe0388c60a178246ee4c45a005292355ec741b9f175b56c8ba393b060
|
4
|
+
data.tar.gz: 35c94ec3a32ea7187b031eaf4341c59ad4a55649510bece671a52b480a10c2c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1eb7c799044b2d70f6f13a2066d175b4ff09965b19b11d91c36493509d579e6e9ea6ebbeb4cfe1d897faf5aded93bc74ea5e955db81fc396bda9a292689537c3
|
7
|
+
data.tar.gz: 9bd2f3aa7f5c6c70ebb5c8afc1d2ca2d4ee95e2978c5491c5a6a1e225ce31c4f3ea5e908a933fa48d74e1cea4d437e131f1718c5e0c496df7c7bf0f99196699b
|
data/CHANGELOG.md
CHANGED
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).
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RiceBubble
|
2
|
+
class Attributes
|
3
|
+
class Datetime < Base
|
4
|
+
def valid?(value)
|
5
|
+
return false unless valid_datetime?(value)
|
6
|
+
|
7
|
+
value.respond_to?(:to_datetime)
|
8
|
+
end
|
9
|
+
|
10
|
+
def coerce(value)
|
11
|
+
return nil unless valid_datetime?(value)
|
12
|
+
|
13
|
+
value.respond_to?(:to_datetime) ? value.to_datetime : value
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def valid_datetime?(value)
|
19
|
+
case value
|
20
|
+
when ::Time then true
|
21
|
+
when ::DateTime then true
|
22
|
+
when ::Date then false
|
23
|
+
else value.respond_to?(:to_datetime)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -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
data/lib/rice_bubble.rb
CHANGED
@@ -5,6 +5,7 @@ require_relative 'rice_bubble/attributes/any'
|
|
5
5
|
require_relative 'rice_bubble/attributes/array'
|
6
6
|
require_relative 'rice_bubble/attributes/boolean'
|
7
7
|
require_relative 'rice_bubble/attributes/date'
|
8
|
+
require_relative 'rice_bubble/attributes/datetime'
|
8
9
|
require_relative 'rice_bubble/attributes/enum'
|
9
10
|
require_relative 'rice_bubble/attributes/number'
|
10
11
|
require_relative 'rice_bubble/attributes/integer'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rice_bubble
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Powell
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -146,6 +146,7 @@ files:
|
|
146
146
|
- lib/rice_bubble/attributes/base.rb
|
147
147
|
- lib/rice_bubble/attributes/boolean.rb
|
148
148
|
- lib/rice_bubble/attributes/date.rb
|
149
|
+
- lib/rice_bubble/attributes/datetime.rb
|
149
150
|
- lib/rice_bubble/attributes/enum.rb
|
150
151
|
- lib/rice_bubble/attributes/integer.rb
|
151
152
|
- lib/rice_bubble/attributes/literal.rb
|