alba 1.6.0 → 2.0.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.
data/lib/alba.rb CHANGED
@@ -7,9 +7,9 @@ require_relative 'alba/deprecation'
7
7
  # Core module
8
8
  module Alba
9
9
  class << self
10
- attr_reader :backend, :encoder, :inferring, :_on_error, :_on_nil, :transforming_root_key
10
+ attr_reader :backend, :encoder, :inferring
11
11
 
12
- # Accessor for inflector, a module responsible for incflecting strings
12
+ # Accessor for inflector, a module responsible for inflecting strings
13
13
  attr_accessor :inflector
14
14
 
15
15
  # Set the backend, which actually serializes object into JSON
@@ -38,17 +38,15 @@ module Alba
38
38
  # Serialize the object with inline definitions
39
39
  #
40
40
  # @param object [Object] the object to be serialized
41
- # @param key [Symbol, nil, true] DEPRECATED, use root_key instead
42
41
  # @param root_key [Symbol, nil, true]
43
42
  # @param block [Block] resource block
44
43
  # @return [String] serialized JSON string
45
44
  # @raise [ArgumentError] if block is absent or `with` argument's type is wrong
46
- def serialize(object, key: nil, root_key: nil, &block)
47
- Alba::Deprecation.warn '`key` option to `serialize` method is deprecated, use `root_key` instead.' if key
45
+ def serialize(object, root_key: nil, &block)
48
46
  klass = block ? resource_class(&block) : infer_resource_class(object.class.name)
49
47
 
50
48
  resource = klass.new(object)
51
- resource.serialize(root_key: root_key || key)
49
+ resource.serialize(root_key: root_key)
52
50
  end
53
51
 
54
52
  # Enable inference for key and resource name
@@ -56,11 +54,7 @@ module Alba
56
54
  # @param with [Symbol, Class, Module] inflector
57
55
  # When it's a Symbol, it sets inflector with given name
58
56
  # When it's a Class or a Module, it sets given object to inflector
59
- def enable_inference!(with: :default)
60
- if with == :default
61
- Alba::Deprecation.warn 'Calling `enable_inference!` without `with` keyword argument is deprecated. Pass `:active_support` to keep current behavior.'
62
- end
63
-
57
+ def enable_inference!(with:)
64
58
  @inflector = inflector_from(with)
65
59
  @inferring = true
66
60
  end
@@ -70,48 +64,6 @@ module Alba
70
64
  @inferring = false
71
65
  end
72
66
 
73
- # Set error handler
74
- #
75
- # @param [Symbol] handler
76
- # @param [Block]
77
- # @raise [ArgumentError] if both handler and block params exist
78
- # @raise [ArgumentError] if both handler and block params don't exist
79
- # @deprecated Use `Resource.on_error` instead
80
- # @return [void]
81
- def on_error(handler = nil, &block)
82
- Alba::Deprecation.warn '`Alba.on_error` is deprecated, use `on_error` on resource class instead.'
83
- raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block
84
- raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block
85
-
86
- @_on_error = handler || block
87
- end
88
-
89
- # Set nil handler
90
- #
91
- # @param block [Block]
92
- # @return [void]
93
- # @deprecated Use `Resource.on_nil` instead
94
- def on_nil(&block)
95
- Alba::Deprecation.warn '`Alba.on_nil` is deprecated, use `on_nil` on resource class instead.'
96
- @_on_nil = block
97
- end
98
-
99
- # Enable root key transformation
100
- #
101
- # @deprecated Use `Resource.transform_keys` with `root` option instead
102
- def enable_root_key_transformation!
103
- Alba::Deprecation.warn '`Alba.enable_root_key_transformation!` is deprecated, use `transform_keys` on resource class instead.'
104
- @transforming_root_key = true
105
- end
106
-
107
- # Disable root key transformation
108
- #
109
- # @deprecated Use `Resource.transform_keys` with `root` option instead
110
- def disable_root_key_transformation!
111
- Alba::Deprecation.warn '`Alba.disable_root_key_transformation!` is deprecated, use `transform_keys` on resource class instead.'
112
- @transforming_root_key = false
113
- end
114
-
115
67
  # @param block [Block] resource body
116
68
  # @return [Class<Alba::Resource>] resource class
117
69
  def resource_class(&block)
@@ -137,7 +89,6 @@ module Alba
137
89
  @encoder = default_encoder
138
90
  @_on_error = :raise
139
91
  @_on_nil = nil
140
- @transforming_root_key = false # TODO: This will be true since 2.0
141
92
  end
142
93
 
143
94
  private
@@ -159,6 +110,7 @@ module Alba
159
110
  @encoder = case @backend
160
111
  when :oj, :oj_strict then try_oj
161
112
  when :oj_rails then try_oj(mode: :rails)
113
+ when :oj_default then try_oj(mode: :default)
162
114
  when :active_support then try_active_support
163
115
  when nil, :default, :json then default_encoder
164
116
  else
@@ -168,7 +120,12 @@ module Alba
168
120
 
169
121
  def try_oj(mode: :strict)
170
122
  require 'oj'
171
- ->(hash) { Oj.dump(hash, mode: mode) }
123
+ case mode
124
+ when :default
125
+ ->(hash) { Oj.dump(hash) }
126
+ else
127
+ ->(hash) { Oj.dump(hash, mode: mode) }
128
+ end
172
129
  rescue LoadError
173
130
  Kernel.warn '`Oj` is not installed, falling back to default JSON encoder.'
174
131
  default_encoder
@@ -184,7 +141,7 @@ module Alba
184
141
 
185
142
  def default_encoder
186
143
  lambda do |hash|
187
- JSON.dump(hash)
144
+ JSON.generate(hash)
188
145
  end
189
146
  end
190
147
 
Binary file
Binary file
Binary file
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: 1.6.0
4
+ version: 2.0.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: 2022-03-15 00:00:00.000000000 Z
11
+ date: 2022-10-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Alba is the fastest JSON serializer for Ruby. It focuses on performance,
14
14
  flexibility and usability.
@@ -18,6 +18,7 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".codeclimate.yml"
21
22
  - ".github/ISSUE_TEMPLATE/bug_report.md"
22
23
  - ".github/ISSUE_TEMPLATE/feature_request.md"
23
24
  - ".github/dependabot.yml"
@@ -29,12 +30,15 @@ files:
29
30
  - ".yardopts"
30
31
  - CHANGELOG.md
31
32
  - CODE_OF_CONDUCT.md
33
+ - CONTRIBUTING.md
32
34
  - Gemfile
35
+ - HACKING.md
33
36
  - LICENSE.txt
34
37
  - README.md
35
38
  - Rakefile
36
39
  - SECURITY.md
37
40
  - alba.gemspec
41
+ - benchmark/README.md
38
42
  - benchmark/collection.rb
39
43
  - benchmark/single_resource.rb
40
44
  - bin/console
@@ -42,19 +46,24 @@ files:
42
46
  - codecov.yml
43
47
  - docs/migrate_from_active_model_serializers.md
44
48
  - docs/migrate_from_jbuilder.md
45
- - gemfiles/all.gemfile
49
+ - docs/rails.md
46
50
  - gemfiles/without_active_support.gemfile
47
51
  - gemfiles/without_oj.gemfile
48
52
  - lib/alba.rb
49
53
  - lib/alba/association.rb
54
+ - lib/alba/conditional_attribute.rb
50
55
  - lib/alba/default_inflector.rb
51
56
  - lib/alba/deprecation.rb
52
57
  - lib/alba/errors.rb
58
+ - lib/alba/layout.rb
59
+ - lib/alba/nested_attribute.rb
53
60
  - lib/alba/resource.rb
54
61
  - lib/alba/typed_attribute.rb
55
62
  - lib/alba/version.rb
63
+ - logo/alba-card.png
64
+ - logo/alba-sign.png
65
+ - logo/alba-typography.png
56
66
  - script/perf_check.rb
57
- - sider.yml
58
67
  homepage: https://github.com/okuramasafumi/alba
59
68
  licenses:
60
69
  - MIT
@@ -72,14 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
81
  requirements:
73
82
  - - ">="
74
83
  - !ruby/object:Gem::Version
75
- version: 2.5.0
84
+ version: 2.6.0
76
85
  required_rubygems_version: !ruby/object:Gem::Requirement
77
86
  requirements:
78
87
  - - ">="
79
88
  - !ruby/object:Gem::Version
80
89
  version: '0'
81
90
  requirements: []
82
- rubygems_version: 3.3.5
91
+ rubygems_version: 3.3.21
83
92
  signing_key:
84
93
  specification_version: 4
85
94
  summary: Alba is the fastest JSON serializer for Ruby.
data/gemfiles/all.gemfile DELETED
@@ -1,20 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activesupport', require: false # For backend
4
- gem 'dry-inflector', require: false # For inflector
5
- gem 'ffaker', require: false # For testing
6
- gem 'minitest', '~> 5.14' # For test
7
- gem 'rake', '~> 13.0' # For test and automation
8
- gem 'rubocop', '>= 0.79.0', require: false # For lint
9
- gem 'rubocop-minitest', '~> 0.11.0', require: false # For lint
10
- gem 'rubocop-performance', '~> 1.10.1', require: false # For lint
11
- gem 'rubocop-rake', '>= 0.5.1', require: false # For lint
12
- gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
13
- gem 'simplecov', '~> 0.21.0', require: false # For test coverage
14
- gem 'simplecov-cobertura', require: false # For test coverage
15
- gem 'yard', require: false # For documentation
16
-
17
- platforms :ruby do
18
- gem 'oj', '~> 3.11', require: false # For backend
19
- gem 'ruby-prof', require: false # For performance profiling
20
- end
data/sider.yml DELETED
@@ -1,60 +0,0 @@
1
- linter:
2
- # # https://help.sider.review/getting-started/custom-configuration
3
-
4
- # # https://help.sider.review/tools/ruby/rubocop
5
- rubocop:
6
- gems:
7
- - "rubocop-minitest"
8
- - "rubocop-performance"
9
- - "rubocop-sensible"
10
- - "rubocop-rake"
11
- safe: false
12
-
13
- # # https://help.sider.review/tools/ruby/reek
14
- # reek:
15
- # gems:
16
- # - name: "reek"
17
- # version: "5.2.0"
18
- # target:
19
- # - lib/
20
- # - test/
21
- # config: config/.reek.yml
22
-
23
- # # https://help.sider.review/tools/ruby/querly
24
- # querly:
25
- # gems:
26
- # - "slim"
27
-
28
- # # https://help.sider.review/tools/others/misspell
29
- # misspell:
30
- # exclude:
31
- # - vendor
32
- # - "**/*.min.js"
33
- # - exclude_file.rb
34
- # targets:
35
- # - target_directory
36
- # - another_target_directory/foo.rb
37
- # - bar.rb
38
- # locale: UK
39
- # ignore: center,behavior
40
-
41
- # # https://help.sider.review/tools/shellscript/shellcheck
42
- # shellcheck:
43
- # target: "src/**/*.{sh,bash}"
44
- # include: "SC2104,SC2105"
45
- # exclude: "SC1000,SC1118"
46
- # enable: "all"
47
- # shell: "bash"
48
- # severity: "error"
49
- # norc: true
50
-
51
- # # https://help.sider.review/getting-started/custom-configuration#ignore
52
- ignore:
53
- - 'test/**/*'
54
-
55
- # # https://help.sider.review/getting-started/custom-configuration#branchesexclude
56
- # branches:
57
- # exclude:
58
- # - master
59
- # - development
60
- # - /^release-.*$/