alba 0.9.0 → 0.10.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: 3a92c56eee7615f823efc93ed7edb87ba6c47a4bcf8e8b11e48edfc62c380545
4
- data.tar.gz: 310eca11a4a5a83287d3d87c1346c2436900bf1283e5642baa3878c69087812d
3
+ metadata.gz: e8427c3365aa48ba76f97799757e5e32ae2f746d3b63c67d58e7c7ddadd17bae
4
+ data.tar.gz: c995206962d4a041032b76113a2f053e48f50f787af12c491437ff98c55d4d2e
5
5
  SHA512:
6
- metadata.gz: 2fed0c44e90d2d876b2db635867741b3520a21c470acfb07a0218b7e0b627ce718edb1da964dfafdf0c79b3fe16bf7502a4fd873e998778c5307bd0c37d0c7c0
7
- data.tar.gz: 334edc5389f1fceba2ee880a322f894ccb145533793a885777ac29c08c273b5dda9a31cf9885b493b6ec3fabcf859a1d1643ce37b848c225db66b658b36e3e34
6
+ metadata.gz: b513dea8cfa68bf09e76114c3d6e14bd77a381f94ad9342c8692034069240c114b9eac95bf071662905de14efe269ac606a7cb2b50ec4107fa393391ed5ddf9d
7
+ data.tar.gz: e44bc0f25be1342ea9424c3919c62cd40b1917eac62f6bf612cd7979ead6db5521200eae4d7ef9a795ee373145c4fb5c846886bc1869916fd744596ac57aecbc
@@ -12,16 +12,45 @@ AllCops:
12
12
  - 'Rakefile'
13
13
  - 'alba.gemspec'
14
14
  NewCops: enable
15
+ EnabledByDefault: true
16
+
17
+ # Oneline comment is not valid so until it gets valid, we disable it
18
+ Bundler/GemComment:
19
+ Enabled: false
20
+
21
+ Layout/ClassStructure:
22
+ Enabled: true
15
23
 
16
24
  Layout/SpaceInsideHashLiteralBraces:
17
25
  EnforcedStyle: no_space
18
26
 
27
+ Layout/MultilineAssignmentLayout:
28
+ EnforcedStyle: same_line
29
+
30
+ Lint/ConstantResolution:
31
+ Enabled: false
32
+
19
33
  Metrics/ClassLength:
20
34
  Exclude:
21
35
  - 'test/alba_test.rb'
22
36
 
23
37
  Metrics/MethodLength:
24
- Max: 20
38
+ Max: 15
39
+
40
+ Style/ConstantVisibility:
41
+ Enabled: false
42
+
43
+ Style/Copyright:
44
+ Enabled: false
45
+
46
+ Style/DocumentationMethod:
47
+ Enabled: false
25
48
 
26
49
  Style/FrozenStringLiteralComment:
27
50
  Enabled: false
51
+
52
+ Style/InlineComment:
53
+ Enabled: false
54
+
55
+ Style/MethodCallWithArgsParentheses:
56
+ Enabled: false
data/Gemfile CHANGED
@@ -3,12 +3,12 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in alba.gemspec
4
4
  gemspec
5
5
 
6
- gem 'activesupport', require: false
7
- gem 'coveralls', require: false
8
- gem 'minitest', '~> 5.0'
9
- gem 'oj', '~> 3.10', platforms: :ruby
10
- gem 'rake', '~> 13.0'
11
- gem 'rubocop', '>= 0.79.0', require: false
12
- gem 'rubocop-minitest', '~> 0.10.1', require: false
13
- gem 'rubocop-performance', '~> 1.7.1', require: false
14
- gem 'rubocop-sensible', '~> 0.3.0', require: false
6
+ gem 'activesupport', require: false # For backend
7
+ gem 'coveralls', require: false # For test coverage
8
+ gem 'minitest', '~> 5.0' # For test
9
+ gem 'oj', '~> 3.10', platforms: :ruby # For backend
10
+ gem 'rake', '~> 13.0' # For test and automation
11
+ gem 'rubocop', '>= 0.79.0', require: false # For lint
12
+ gem 'rubocop-minitest', '~> 0.10.1', require: false # For lint
13
+ gem 'rubocop-performance', '~> 1.7.1', require: false # For lint
14
+ gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alba (0.9.0)
4
+ alba (0.10.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -13,6 +13,7 @@ GEM
13
13
  tzinfo (~> 1.1)
14
14
  zeitwerk (~> 2.2, >= 2.2.2)
15
15
  ast (2.4.1)
16
+ bigdecimal (2.0.0)
16
17
  concurrent-ruby (1.1.6)
17
18
  coveralls (0.8.23)
18
19
  json (>= 1.8, < 3)
@@ -25,7 +26,8 @@ GEM
25
26
  concurrent-ruby (~> 1.0)
26
27
  json (2.3.1)
27
28
  minitest (5.14.1)
28
- oj (3.10.8)
29
+ oj (3.10.12)
30
+ bigdecimal (>= 1.0, < 3)
29
31
  parallel (1.19.2)
30
32
  parser (2.7.1.4)
31
33
  ast (~> 2.4.1)
data/README.md CHANGED
@@ -7,6 +7,22 @@
7
7
 
8
8
  `Alba` is the fastest JSON serializer for Ruby.
9
9
 
10
+ # Why yet another JSON serializer?
11
+
12
+ We know that there are several other JSON serializers for Ruby around, but none of them made us satisfied.
13
+
14
+ Alba has some advantages over other JSON serializers which we've wanted to have.
15
+
16
+ ## Easy to understand
17
+
18
+ DSL is great. It makes the coding experience natural and intuitive. However, remembering lots of DSL requires us a lot of effort. Unfortunately, most of the existing libraries have implemented their features via DSL and it's not easy to understand how they behave entirely. Alba's core DSL are only four (`attributes`, `attribute`, `one` and `many`) so it's easy to understand how to use.
19
+
20
+ Alba is also understandable internally. The codebase is much smaller than the alternatives. In fact, it's less than 300 lines of code. Look at the code on [GitHub](https://github.com/okuramasafumi/alba/tree/master/lib) and you'll be surprised how simple it is!
21
+
22
+ ## Performance
23
+
24
+ Alba is faster than most of the alternatives. We have a [benchmark](https://gist.github.com/okuramasafumi/4e375525bd3a28e4ca812d2a3b3e5829).
25
+
10
26
  ## Installation
11
27
 
12
28
  Add this line to your application's Gemfile:
@@ -7,6 +7,7 @@ require 'alba/resources/default_resource'
7
7
  # Core module
8
8
  module Alba
9
9
  class Error < StandardError; end
10
+ class UnsupportedBackend < Error; end
10
11
 
11
12
  class << self
12
13
  attr_reader :backend, :encoder
@@ -31,26 +32,30 @@ module Alba
31
32
  def set_encoder
32
33
  @encoder = case @backend
33
34
  when :oj
34
- begin
35
- require 'oj'
36
- ->(hash) { Oj.dump(hash, mode: :strict) }
37
- rescue LoadError
38
- default_encoder
39
- end
35
+ try_oj
40
36
  when :active_support
41
- begin
42
- require 'active_support/json'
43
- ->(hash) { ActiveSupport::JSON.encode(hash) }
44
- rescue LoadError
45
- default_encoder
46
- end
37
+ try_active_support
47
38
  when nil, :default, :json
48
39
  default_encoder
49
40
  else
50
- raise Alba::Error, "Unsupported backend, #{backend}"
41
+ raise Alba::UnsupportedBackend, "Unsupported backend, #{backend}"
51
42
  end
52
43
  end
53
44
 
45
+ def try_oj
46
+ require 'oj'
47
+ ->(hash) { Oj.dump(hash, mode: :strict) }
48
+ rescue LoadError
49
+ default_encoder
50
+ end
51
+
52
+ def try_active_support
53
+ require 'active_support/json'
54
+ ->(hash) { ActiveSupport::JSON.encode(hash) }
55
+ rescue LoadError
56
+ default_encoder
57
+ end
58
+
54
59
  def default_encoder
55
60
  lambda do |hash|
56
61
  require 'json'
@@ -2,10 +2,11 @@ module Alba
2
2
  # Base class for `One` and `Many`
3
3
  # Child class should implement `to_hash` method
4
4
  class Association
5
- def initialize(name:, resource: nil, &block)
5
+ def initialize(name:, condition: nil, resource: nil, &block)
6
6
  @name = name
7
- @resource = resource
7
+ @condition = condition
8
8
  @block = block
9
+ @resource = resource || resource_class
9
10
  raise ArgumentError, 'resource or block is required' if @resource.nil? && @block.nil?
10
11
  end
11
12
 
@@ -3,10 +3,10 @@ require 'alba/association'
3
3
  module Alba
4
4
  # Representing many association
5
5
  class Many < Association
6
- def to_hash(target)
6
+ def to_hash(target, params: {})
7
7
  objects = target.public_send(@name)
8
- @resource ||= resource_class
9
- objects.map { |o| @resource.new(o).to_hash }
8
+ objects = @condition.call(objects) if @condition
9
+ objects.map { |o| @resource.new(o, params: params).to_hash }
10
10
  end
11
11
  end
12
12
  end
@@ -3,10 +3,10 @@ require 'alba/association'
3
3
  module Alba
4
4
  # Representing one association
5
5
  class One < Association
6
- def to_hash(target)
6
+ def to_hash(target, params: {})
7
7
  object = target.public_send(@name)
8
- @resource ||= resource_class
9
- @resource.new(object).to_hash
8
+ object = @condition.call(object) if @condition
9
+ @resource.new(object, params: params).to_hash
10
10
  end
11
11
  end
12
12
  end
@@ -63,7 +63,9 @@ module Alba
63
63
  when Proc
64
64
  instance_exec(resource, &attribute)
65
65
  when Alba::One, Alba::Many
66
- attribute.to_hash(resource)
66
+ attribute.to_hash(resource, params: params)
67
+ else
68
+ raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
67
69
  end
68
70
  end
69
71
  end
@@ -90,21 +92,21 @@ module Alba
90
92
  end
91
93
 
92
94
  def attributes(*attrs)
93
- attrs.each { |attr_name| @_attributes[attr_name] = attr_name }
95
+ attrs.each { |attr_name| @_attributes[attr_name.to_sym] = attr_name.to_sym }
94
96
  end
95
97
 
96
98
  def attribute(name, &block)
97
99
  raise ArgumentError, 'No block given in attribute method' unless block
98
100
 
99
- @_attributes[name] = block
101
+ @_attributes[name.to_sym] = block
100
102
  end
101
103
 
102
- def one(name, resource: nil, &block)
103
- @_attributes[name.to_sym] = One.new(name: name, resource: resource, &block)
104
+ def one(name, condition = nil, resource: nil, key: nil, &block)
105
+ @_attributes[key&.to_sym || name.to_sym] = One.new(name: name, condition: condition, resource: resource, &block)
104
106
  end
105
107
 
106
- def many(name, resource: nil, &block)
107
- @_attributes[name.to_sym] = Many.new(name: name, resource: resource, &block)
108
+ def many(name, condition = nil, resource: nil, key: nil, &block)
109
+ @_attributes[key&.to_sym || name.to_sym] = Many.new(name: name, condition: condition, resource: resource, &block)
108
110
  end
109
111
 
110
112
  def serializer(name)
@@ -118,7 +120,7 @@ module Alba
118
120
  # Use this DSL in child class to ignore certain attributes
119
121
  def ignoring(*attributes)
120
122
  attributes.each do |attr_name|
121
- @_attributes.delete(attr_name)
123
+ @_attributes.delete(attr_name.to_sym)
122
124
  end
123
125
  end
124
126
  end
@@ -25,12 +25,7 @@ module Alba
25
25
 
26
26
  def key
27
27
  opts = self.class._opts || {}
28
- case opts[:key]
29
- when true
30
- @resource.key
31
- else
32
- opts[:key]
33
- end
28
+ opts[:key] == true ? @resource.key : opts[:key]
34
29
  end
35
30
 
36
31
  def metadata
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '0.9.0'.freeze
2
+ VERSION = '0.10.0'.freeze
3
3
  end
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.9.0
4
+ version: 0.10.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-08-11 00:00:00.000000000 Z
11
+ date: 2020-08-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Alba is designed to be a simple, easy to use and fast alternative to
14
14
  existing JSON serializers. Its performance is better than almost all gems which