alba 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -0
- data/lib/alba.rb +17 -6
- data/lib/alba/resource.rb +12 -4
- data/lib/alba/resources/default_resource.rb +9 -0
- data/lib/alba/version.rb +1 -1
- 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: 89b4df7ce18a63e20cee9e87bd2855ef9c4735c4f6e4e1ef55433c5ba0a2fbd0
|
4
|
+
data.tar.gz: 63fa94fa17cdf3f5bfeef5f2e7b0d6efeec1fd06ad8d5386fb389fea3db0343a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 063d913715faa3917df5658d798ceb1619b9a2aef1dc0875c92c1592b3d3f9025bec60a364ebe0d54fb0769e18ba78f5786caa288159d304dfeca7ab00149d2b
|
7
|
+
data.tar.gz: 66aed62d355cd6fd8527ad4da27c53c9c42bf98606586a562b84dc377e6041125653d9669cf77c14427b4a1796b6c7abd7e6b2861323bac51fde28bfe05a7e7a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -108,6 +108,22 @@ UserResource1.new(user).serialize
|
|
108
108
|
# => '{"id":1,"articles":[{"title":"Hello World!"},{"title":"Super nice"}]}'
|
109
109
|
```
|
110
110
|
|
111
|
+
### Inline definition with `Alba.serialize`
|
112
|
+
|
113
|
+
`Alba.serialize` method is a shortcut to define everything inline.
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
Alba.serialize(user, with: proc { set key: :foo }) do
|
117
|
+
attributes :id
|
118
|
+
many :articles do
|
119
|
+
attributes :title, :body
|
120
|
+
end
|
121
|
+
end
|
122
|
+
# => '{"foo":{"id":1,"articles":[{"title":"Hello World!","body":"Hello World!!!"},{"title":"Super nice","body":"Really nice!"}]}}'
|
123
|
+
```
|
124
|
+
|
125
|
+
Although this might be useful sometimes, it's generally recommended to define a class for both Resource and Serializer.
|
126
|
+
|
111
127
|
## Comparison
|
112
128
|
|
113
129
|
Since Alba is intended to be stupid, there are many things Alba can't do while other gems can. However, from the same reason, it's extremely faster than alternatives.
|
data/lib/alba.rb
CHANGED
@@ -2,6 +2,7 @@ require 'alba/version'
|
|
2
2
|
require 'alba/serializers/default_serializer'
|
3
3
|
require 'alba/serializer'
|
4
4
|
require 'alba/resource'
|
5
|
+
require 'alba/resources/default_resource'
|
5
6
|
|
6
7
|
# Core module
|
7
8
|
module Alba
|
@@ -9,13 +10,23 @@ module Alba
|
|
9
10
|
|
10
11
|
class << self
|
11
12
|
attr_reader :backend
|
12
|
-
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def backend=(backend)
|
15
|
+
@backend = backend&.to_sym
|
16
|
+
end
|
17
|
+
|
18
|
+
def serialize(object, with: nil, &block)
|
19
|
+
raise ArgumentError, 'Block required' unless block
|
20
|
+
|
21
|
+
resource_class.class_eval(&block)
|
22
|
+
resource = resource_class.new(object)
|
23
|
+
resource.serialize(with: with)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
17
27
|
|
18
|
-
|
19
|
-
|
28
|
+
def resource_class
|
29
|
+
::Alba::Resources::DefaultResource.clone
|
30
|
+
end
|
20
31
|
end
|
21
32
|
end
|
data/lib/alba/resource.rb
CHANGED
@@ -33,10 +33,10 @@ module Alba
|
|
33
33
|
@_serializer || Alba::Serializers::DefaultSerializer
|
34
34
|
when ->(obj) { obj.is_a?(Class) && obj <= Alba::Serializer }
|
35
35
|
with
|
36
|
-
when
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
when Proc
|
37
|
+
inline_extended_serializer(with)
|
38
|
+
else
|
39
|
+
raise ArgumentError, 'Unexpected type for with, possible types are Class or Proc'
|
40
40
|
end
|
41
41
|
serializer.new(serializable_hash).serialize
|
42
42
|
end
|
@@ -47,6 +47,14 @@ module Alba
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
alias to_hash serializable_hash
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def inline_extended_serializer(with)
|
54
|
+
klass = ::Alba::Serializers::DefaultSerializer.clone
|
55
|
+
klass.class_eval(&with)
|
56
|
+
klass
|
57
|
+
end
|
50
58
|
end
|
51
59
|
|
52
60
|
# Class methods
|
data/lib/alba/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Fast and flexible JSON serializer
|
14
14
|
email:
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/alba/many.rb
|
35
35
|
- lib/alba/one.rb
|
36
36
|
- lib/alba/resource.rb
|
37
|
+
- lib/alba/resources/default_resource.rb
|
37
38
|
- lib/alba/serializer.rb
|
38
39
|
- lib/alba/serializers/default_serializer.rb
|
39
40
|
- lib/alba/version.rb
|