alba 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -5
- data/.travis.yml +3 -1
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -1
- data/README.md +15 -2
- data/alba.gemspec +14 -14
- data/lib/alba/resource.rb +9 -7
- data/lib/alba/serializer.rb +14 -1
- data/lib/alba/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 592412974accb17b359ab871e293e2c839519f7cfffb31a04485bfe261b88202
|
4
|
+
data.tar.gz: d8fbb2ec751eac8e7dbe278e8993da6641eaa88f302b3438055b2062c08bd229
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97c3fc639d1d6b7fa588c78fa84f4be7318982ebf3154b9037e1b9c6ac38d1d9a0528bcf45618a5d1b1dc009df30441589eb27d1bdfbd8afaca1e61a0708e31e
|
7
|
+
data.tar.gz: 7abd201ba80bdfff652840e948e8aede24e90d0cfc26722f08044916f239387fd0bada0a1e9a0d71cf252ced9400d1549124349a1070ea7bd8c8e5a7be0ebc38
|
data/.rubocop.yml
CHANGED
@@ -4,6 +4,7 @@ inherit_gem:
|
|
4
4
|
rubocop-sensible: 'config/rubocop.yml'
|
5
5
|
|
6
6
|
require:
|
7
|
+
- rubocop-minitest
|
7
8
|
- rubocop-performance
|
8
9
|
|
9
10
|
AllCops:
|
@@ -18,10 +19,5 @@ Layout/SpaceInsideHashLiteralBraces:
|
|
18
19
|
Metrics/MethodLength:
|
19
20
|
Max: 20
|
20
21
|
|
21
|
-
Naming/PredicateName:
|
22
|
-
AllowedMethods:
|
23
|
-
- 'has_one'
|
24
|
-
- 'has_many'
|
25
|
-
|
26
22
|
Style/FrozenStringLiteralComment:
|
27
23
|
Enabled: false
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -8,5 +8,6 @@ gem 'minitest', '~> 5.0'
|
|
8
8
|
gem 'oj', '~> 3.10'
|
9
9
|
gem 'rake', '~> 13.0'
|
10
10
|
gem 'rubocop', '>= 0.79.0', require: false
|
11
|
+
gem 'rubocop-minitest', '~> 0.10.1', require: false
|
11
12
|
gem 'rubocop-performance', '~> 1.7.1', require: false
|
12
13
|
gem 'rubocop-sensible', '~> 0.3.0', require: false
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
alba (0.
|
4
|
+
alba (0.8.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -35,6 +35,8 @@ GEM
|
|
35
35
|
unicode-display_width (>= 1.4.0, < 2.0)
|
36
36
|
rubocop-ast (0.2.0)
|
37
37
|
parser (>= 2.7.0.1)
|
38
|
+
rubocop-minitest (0.10.1)
|
39
|
+
rubocop (>= 0.87)
|
38
40
|
rubocop-performance (1.7.1)
|
39
41
|
rubocop (>= 0.82.0)
|
40
42
|
rubocop-sensible (0.3.0)
|
@@ -63,6 +65,7 @@ DEPENDENCIES
|
|
63
65
|
oj (~> 3.10)
|
64
66
|
rake (~> 13.0)
|
65
67
|
rubocop (>= 0.79.0)
|
68
|
+
rubocop-minitest (~> 0.10.1)
|
66
69
|
rubocop-performance (~> 1.7.1)
|
67
70
|
rubocop-sensible (~> 0.3.0)
|
68
71
|
|
data/README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/alba.svg)](https://badge.fury.io/rb/alba)
|
1
2
|
[![Build Status](https://travis-ci.com/okuramasafumi/alba.svg?branch=master)](https://travis-ci.com/okuramasafumi/alba)
|
2
3
|
[![Coverage Status](https://coveralls.io/repos/github/okuramasafumi/alba/badge.svg?branch=master)](https://coveralls.io/github/okuramasafumi/alba?branch=master)
|
3
4
|
[![Maintainability](https://api.codeclimate.com/v1/badges/fdab4cc0de0b9addcfe8/maintainability)](https://codeclimate.com/github/okuramasafumi/alba/maintainability)
|
4
5
|
|
5
6
|
# Alba
|
6
7
|
|
7
|
-
`Alba` is
|
8
|
+
`Alba` is the fastest JSON serializer for Ruby.
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
@@ -126,13 +127,25 @@ Although this might be useful sometimes, it's generally recommended to define a
|
|
126
127
|
|
127
128
|
## Comparison
|
128
129
|
|
129
|
-
|
130
|
+
Alba is faster than alternatives.
|
130
131
|
For a performance benchmark, see https://gist.github.com/okuramasafumi/4e375525bd3a28e4ca812d2a3b3e5829.
|
131
132
|
|
132
133
|
## Why named "Alba"?
|
133
134
|
|
134
135
|
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.
|
135
136
|
|
137
|
+
## Alba internals
|
138
|
+
|
139
|
+
Alba has three component, `Serializer`, `Resource` and `Value` (`Value` is conceptual and not implemented directly).
|
140
|
+
|
141
|
+
`Serializer` is a component responsible for rendering JSON output with `Resource`. `Serializer` can add more data to `Resource` such as `metadata`. Users can define one single `Serializer` and reuse it for all `Resource`s. The main interface is `#serialize`.
|
142
|
+
|
143
|
+
`Resource` is a component responsible for defining how an object (or a collection of objects) is converted into JSON. The difference between `Serializer` and `Resource` is that while `Serializer` can add arbitrary data into JSON, `Resource` can get data only from the object under it. The main interface is `#serializable_hash`.
|
144
|
+
|
145
|
+
`Value` is either `Attribute`, `One` or `Many`. They are responsible for fetching data from the object for `Resource`. The main interface is `#to_hash`.
|
146
|
+
|
147
|
+
The main `Alba` module holds config values and one convenience method, `.serialize`.
|
148
|
+
|
136
149
|
## Development
|
137
150
|
|
138
151
|
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/alba.gemspec
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
require_relative 'lib/alba/version'
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name =
|
4
|
+
spec.name = 'alba'
|
5
5
|
spec.version = Alba::VERSION
|
6
|
-
spec.authors = [
|
7
|
-
spec.email = [
|
6
|
+
spec.authors = ['OKURA Masafumi']
|
7
|
+
spec.email = ['masafumi.o1988@gmail.com']
|
8
8
|
|
9
|
-
spec.summary =
|
10
|
-
spec.description = "
|
11
|
-
spec.homepage =
|
12
|
-
spec.license =
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new(
|
9
|
+
spec.summary = 'Alba is the fastest JSON serializer for Ruby.'
|
10
|
+
spec.description = "Alba is designed to be a simple, easy to use and fast alternative to existing JSON serializers. Its performance is better than almost all gems which do similar things. The internal is so simple that it's easy to hack and maintain."
|
11
|
+
spec.homepage = 'https://github.com/okuramasafumi/alba'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.8')
|
14
14
|
|
15
|
-
spec.metadata[
|
16
|
-
spec.metadata[
|
17
|
-
spec.metadata[
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
spec.metadata['source_code_uri'] = 'https://github.com/okuramasafumi/alba'
|
17
|
+
spec.metadata['changelog_uri'] = 'https://github.com/okuramasafumi/alba/CHANGELOG.md'
|
18
18
|
|
19
19
|
# Specify which files should be added to the gem when it is released.
|
20
20
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
-
spec.files
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
22
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
23
|
end
|
24
|
-
spec.bindir =
|
24
|
+
spec.bindir = 'exe'
|
25
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
-
spec.require_paths = [
|
26
|
+
spec.require_paths = ['lib']
|
27
27
|
end
|
data/lib/alba/resource.rb
CHANGED
@@ -27,8 +27,10 @@ module Alba
|
|
27
27
|
|
28
28
|
# Instance methods
|
29
29
|
module InstanceMethods
|
30
|
-
|
31
|
-
|
30
|
+
attr_reader :_object
|
31
|
+
|
32
|
+
def initialize(object)
|
33
|
+
@_object = object
|
32
34
|
DSLS.each { |name| instance_variable_set("@#{name}", self.class.public_send(name)) }
|
33
35
|
end
|
34
36
|
|
@@ -53,9 +55,9 @@ module Alba
|
|
53
55
|
end
|
54
56
|
end
|
55
57
|
serializable_hash = if collection?
|
56
|
-
@
|
58
|
+
@_object.map(&get_attribute)
|
57
59
|
else
|
58
|
-
get_attribute.call(@
|
60
|
+
get_attribute.call(@_object)
|
59
61
|
end
|
60
62
|
with_key && @_key ? {@_key => serializable_hash} : serializable_hash
|
61
63
|
end
|
@@ -74,16 +76,16 @@ module Alba
|
|
74
76
|
end
|
75
77
|
|
76
78
|
def collection?
|
77
|
-
@
|
79
|
+
@_object.is_a?(Enumerable)
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
81
83
|
# Class methods
|
82
84
|
module ClassMethods
|
83
|
-
|
85
|
+
attr_reader(*DSLS)
|
84
86
|
|
85
87
|
def inherited(subclass)
|
86
|
-
DSLS.each { |name| subclass.
|
88
|
+
DSLS.each { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}")) }
|
87
89
|
end
|
88
90
|
|
89
91
|
def attributes(*attrs)
|
data/lib/alba/serializer.rb
CHANGED
@@ -10,6 +10,8 @@ module Alba
|
|
10
10
|
module InstanceMethods
|
11
11
|
def initialize(resource)
|
12
12
|
@_opts = self.class._opts || {}
|
13
|
+
@_metadata = self.class._metadata || {}
|
14
|
+
@_metadata = @_metadata.transform_values { |block| block.call(resource._object) }
|
13
15
|
key = case @_opts[:key]
|
14
16
|
when true
|
15
17
|
resource.key
|
@@ -18,6 +20,8 @@ module Alba
|
|
18
20
|
end
|
19
21
|
@hash = resource.serializable_hash(with_key: false)
|
20
22
|
@hash = {key.to_sym => @hash} if key
|
23
|
+
# @hash is either Hash or Array
|
24
|
+
@hash.is_a?(Hash) ? @hash.merge!(@_metadata.to_h) : @hash << @_metadata
|
21
25
|
end
|
22
26
|
|
23
27
|
def serialize
|
@@ -41,12 +45,21 @@ module Alba
|
|
41
45
|
|
42
46
|
# Class methods
|
43
47
|
module ClassMethods
|
44
|
-
attr_reader :_opts
|
48
|
+
attr_reader :_opts, :_metadata
|
49
|
+
|
50
|
+
def inherited(subclass)
|
51
|
+
%w[_opts _metadata].each { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}")) }
|
52
|
+
end
|
45
53
|
|
46
54
|
def set(key: false)
|
47
55
|
@_opts ||= {}
|
48
56
|
@_opts[:key] = key
|
49
57
|
end
|
58
|
+
|
59
|
+
def metadata(name, &block)
|
60
|
+
@_metadata ||= {}
|
61
|
+
@_metadata[name] = block
|
62
|
+
end
|
50
63
|
end
|
51
64
|
end
|
52
65
|
end
|
data/lib/alba/version.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.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-
|
11
|
+
date: 2020-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Alba is designed to be a simple, easy to use and fast alternative to
|
14
|
+
existing JSON serializers. Its performance is better than almost all gems which
|
15
|
+
do similar things. The internal is so simple that it's easy to hack and maintain.
|
14
16
|
email:
|
15
17
|
- masafumi.o1988@gmail.com
|
16
18
|
executables: []
|
@@ -54,7 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
56
|
requirements:
|
55
57
|
- - ">="
|
56
58
|
- !ruby/object:Gem::Version
|
57
|
-
version: 2.
|
59
|
+
version: 2.5.8
|
58
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
61
|
requirements:
|
60
62
|
- - ">="
|
@@ -64,5 +66,5 @@ requirements: []
|
|
64
66
|
rubygems_version: 3.1.4
|
65
67
|
signing_key:
|
66
68
|
specification_version: 4
|
67
|
-
summary:
|
69
|
+
summary: Alba is the fastest JSON serializer for Ruby.
|
68
70
|
test_files: []
|