alba 0.3.0 → 0.4.0
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 +61 -1
- data/lib/alba.rb +3 -13
- data/lib/alba/serializer.rb +5 -2
- data/lib/alba/serializers/default_serializer.rb +2 -0
- data/lib/alba/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: 115b71c658fcda000dd3e4d4ccc0306c3cf94033f42fd96eb8e26223cba1ed3a
|
4
|
+
data.tar.gz: 8c9f023c9d60c905c35b6f285120f6de536b4a21258fdf42b867a5601c2162d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66bc3c85b71f412c761629a6eae760c038115edd826e1d2cc34d204e2cdb4164ababdc31fc3f9569eb89ea51d364176cfd2a53b9ffbfb995fb3daeada1c110e3
|
7
|
+
data.tar.gz: 6f8b53f011829b941ea6ebb6237f96f52dbc7cd36042db6cab996c7e4a4a3ead931f3801243c64be6f7c60f8d144cb85d5beea62f020f95e6d1bc3585bbadd1b
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
# Alba
|
6
6
|
|
7
|
-
`Alba` is a fast and
|
7
|
+
`Alba` is a stupid, fast and easy to use JSON serializer.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -24,6 +24,8 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
+
### Simple serialization with key
|
28
|
+
|
27
29
|
```ruby
|
28
30
|
class User
|
29
31
|
attr_accessor :id, :name, :email, :created_at, :updated_at
|
@@ -57,6 +59,64 @@ UserResource.new(user).serialize
|
|
57
59
|
# => "{\"id\":1,\"name\":\"Masafumi OKURA\",\"name_with_email\":\"Masafumi OKURA: masafumi@example.com\"}"
|
58
60
|
```
|
59
61
|
|
62
|
+
### Serialization with associations
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class User
|
66
|
+
attr_reader :id, :created_at, :updated_at
|
67
|
+
attr_accessor :articles
|
68
|
+
|
69
|
+
def initialize(id)
|
70
|
+
@id = id
|
71
|
+
@created_at = Time.now
|
72
|
+
@updated_at = Time.now
|
73
|
+
@articles = []
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Article
|
78
|
+
attr_accessor :user_id, :title, :body
|
79
|
+
|
80
|
+
def initialize(user_id, title, body)
|
81
|
+
@user_id = user_id
|
82
|
+
@title = title
|
83
|
+
@body = body
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class ArticleResource
|
88
|
+
include Alba::Resource
|
89
|
+
|
90
|
+
attributes :title
|
91
|
+
end
|
92
|
+
|
93
|
+
class UserResource1
|
94
|
+
include Alba::Resource
|
95
|
+
|
96
|
+
attributes :id
|
97
|
+
|
98
|
+
many :articles, resource: ArticleResource
|
99
|
+
end
|
100
|
+
|
101
|
+
user = User.new(1)
|
102
|
+
article1 = Article.new(1, 'Hello World!', 'Hello World!!!')
|
103
|
+
user.articles << article1
|
104
|
+
article2 = Article.new(2, 'Super nice', 'Really nice!')
|
105
|
+
user.articles << article2
|
106
|
+
|
107
|
+
UserResource1.new(user).serialize
|
108
|
+
# => '{"id":1,"articles":[{"title":"Hello World!"},{"title":"Super nice"}]}'
|
109
|
+
```
|
110
|
+
|
111
|
+
## Comparison
|
112
|
+
|
113
|
+
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.
|
114
|
+
For a performance benchmark, see https://gist.github.com/okuramasafumi/4e375525bd3a28e4ca812d2a3b3e5829.
|
115
|
+
|
116
|
+
## Why named "Alba"?
|
117
|
+
|
118
|
+
The name "Alba" comes from "albatross", a kind of birds. In Japanese, this bird is called "Aho-dori", which means "stupid bird". I find it funny because in fact albatrosses fly really fast. I hope Alba looks stupid but in fact it does its job quick.
|
119
|
+
|
60
120
|
## Development
|
61
121
|
|
62
122
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/alba.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'alba/version'
|
2
|
+
require 'alba/serializers/default_serializer'
|
3
|
+
require 'alba/serializer'
|
2
4
|
require 'alba/resource'
|
3
|
-
require 'json'
|
4
5
|
|
5
6
|
# Core module
|
6
7
|
module Alba
|
@@ -15,17 +16,6 @@ module Alba
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def self.serialize(object)
|
18
|
-
|
19
|
-
case backend
|
20
|
-
when :oj
|
21
|
-
begin
|
22
|
-
require 'oj'
|
23
|
-
->(resource) { Oj.dump(resource) }
|
24
|
-
rescue LoadError
|
25
|
-
fallback
|
26
|
-
end
|
27
|
-
else
|
28
|
-
fallback
|
29
|
-
end.call(object)
|
19
|
+
Serializers::DefaultSerializer.new(object).serialize
|
30
20
|
end
|
31
21
|
end
|
data/lib/alba/serializer.rb
CHANGED
@@ -17,12 +17,15 @@ module Alba
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def serialize
|
20
|
-
fallback =
|
20
|
+
fallback = lambda do
|
21
|
+
require 'json'
|
22
|
+
JSON.dump(@_resource)
|
23
|
+
end
|
21
24
|
case Alba.backend
|
22
25
|
when :oj
|
23
26
|
begin
|
24
27
|
require 'oj'
|
25
|
-
-> { Oj.dump(@_resource) }
|
28
|
+
-> { Oj.dump(@_resource, mode: :strict) }
|
26
29
|
rescue LoadError
|
27
30
|
fallback
|
28
31
|
end
|
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.4.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-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Fast and flexible JSON serializer
|
14
14
|
email:
|