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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 405e177e9e1fcadf7dbb164d3ac6924e1b3563547f715fedb698211b527a9604
4
- data.tar.gz: 2c534708583345ac9090c83b3ddc939b0c8bd335ed77017f554edb28578d0396
3
+ metadata.gz: 592412974accb17b359ab871e293e2c839519f7cfffb31a04485bfe261b88202
4
+ data.tar.gz: d8fbb2ec751eac8e7dbe278e8993da6641eaa88f302b3438055b2062c08bd229
5
5
  SHA512:
6
- metadata.gz: 71f4712ac28d457fe9bf997fb127288ed66d7d2231ffd6dd438b2eb5c1dd82ff9e3e5c4000415c513012a582cda3d3c3e73fdf0beb4cc96b8583ff9a658dfa75
7
- data.tar.gz: '07439ccffc5b48a0d5d4286236acadf4cc3e89ff1afa0a882a87c1fe5640015096a4cd85839cebdc4d6ef350a77043a494bfe4d7b6cff99f8e7db2ab9f8b3db0'
6
+ metadata.gz: 97c3fc639d1d6b7fa588c78fa84f4be7318982ebf3154b9037e1b9c6ac38d1d9a0528bcf45618a5d1b1dc009df30441589eb27d1bdfbd8afaca1e61a0708e31e
7
+ data.tar.gz: 7abd201ba80bdfff652840e948e8aede24e90d0cfc26722f08044916f239387fd0bada0a1e9a0d71cf252ced9400d1549124349a1070ea7bd8c8e5a7be0ebc38
@@ -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
@@ -2,5 +2,7 @@
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.6.5
5
+ - 2.5.8
6
+ - 2.6.6
7
+ - 2.7.1
6
8
  before_install: gem install bundler -v 2.1.4
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alba (0.7.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 a stupid, fast and easy to use JSON serializer.
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
- 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.
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.
@@ -1,27 +1,27 @@
1
1
  require_relative 'lib/alba/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = "alba"
4
+ spec.name = 'alba'
5
5
  spec.version = Alba::VERSION
6
- spec.authors = ["OKURA Masafumi"]
7
- spec.email = ["masafumi.o1988@gmail.com"]
6
+ spec.authors = ['OKURA Masafumi']
7
+ spec.email = ['masafumi.o1988@gmail.com']
8
8
 
9
- spec.summary = "Fast and flexible JSON serializer"
10
- spec.description = "Fast and flexible JSON serializer"
11
- spec.homepage = "https://github.com/okuramasafumi/alba"
12
- spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
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["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"
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 = Dir.chdir(File.expand_path('..', __FILE__)) do
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 = "exe"
24
+ spec.bindir = 'exe'
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
- spec.require_paths = ["lib"]
26
+ spec.require_paths = ['lib']
27
27
  end
@@ -27,8 +27,10 @@ module Alba
27
27
 
28
28
  # Instance methods
29
29
  module InstanceMethods
30
- def initialize(resource)
31
- @_resource = resource
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
- @_resource.map(&get_attribute)
58
+ @_object.map(&get_attribute)
57
59
  else
58
- get_attribute.call(@_resource)
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
- @_resource.is_a?(Enumerable)
79
+ @_object.is_a?(Enumerable)
78
80
  end
79
81
  end
80
82
 
81
83
  # Class methods
82
84
  module ClassMethods
83
- attr_accessor(*DSLS)
85
+ attr_reader(*DSLS)
84
86
 
85
87
  def inherited(subclass)
86
- DSLS.each { |name| subclass.public_send("#{name}=", instance_variable_get("@#{name}")) }
88
+ DSLS.each { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}")) }
87
89
  end
88
90
 
89
91
  def attributes(*attrs)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '0.7.0'.freeze
2
+ VERSION = '0.8.0'.freeze
3
3
  end
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.7.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-07-31 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Fast and flexible JSON serializer
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.3.0
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: Fast and flexible JSON serializer
69
+ summary: Alba is the fastest JSON serializer for Ruby.
68
70
  test_files: []