alba 0.5.0 → 0.6.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: 89b4df7ce18a63e20cee9e87bd2855ef9c4735c4f6e4e1ef55433c5ba0a2fbd0
4
- data.tar.gz: 63fa94fa17cdf3f5bfeef5f2e7b0d6efeec1fd06ad8d5386fb389fea3db0343a
3
+ metadata.gz: efa21967c65ad6880f19fbb2ec2fd795ed1b71f477084056068b23984df60834
4
+ data.tar.gz: 556f95a3ad12a7fdc591f8b7e336a7bc2d6c4b99710198f9bdc4e36b1389e0d2
5
5
  SHA512:
6
- metadata.gz: 063d913715faa3917df5658d798ceb1619b9a2aef1dc0875c92c1592b3d3f9025bec60a364ebe0d54fb0769e18ba78f5786caa288159d304dfeca7ab00149d2b
7
- data.tar.gz: 66aed62d355cd6fd8527ad4da27c53c9c42bf98606586a562b84dc377e6041125653d9669cf77c14427b4a1796b6c7abd7e6b2861323bac51fde28bfe05a7e7a
6
+ metadata.gz: 9f2bc512373e64a1ecda15c5797a5e9a1944f9fdc2a81f34f4af063d54c288fa53710bba9645fdbdec922b2642491bf0983830b7b806c3e6706099d48ed31c29
7
+ data.tar.gz: 99c355f1ad8d2c26786b79d3f7889ac00b08444f72352a14f300eb25049efbe81792242d48c0c9bf760e5bce3e93d61f408a99ac38a93a252ca55954c0880162
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gemspec
5
5
 
6
6
  gem 'coveralls', require: false
7
7
  gem 'minitest', '~> 5.0'
8
+ gem 'oj', '~> 3.10'
8
9
  gem 'rake', '~> 13.0'
9
10
  gem 'rubocop', '>= 0.79.0', require: false
10
11
  gem 'rubocop-performance', '~> 1.7.1', require: false
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alba (0.5.0)
4
+ alba (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -16,6 +16,7 @@ GEM
16
16
  docile (1.3.2)
17
17
  json (2.3.1)
18
18
  minitest (5.14.1)
19
+ oj (3.10.8)
19
20
  parallel (1.19.2)
20
21
  parser (2.7.1.4)
21
22
  ast (~> 2.4.1)
@@ -59,6 +60,7 @@ DEPENDENCIES
59
60
  alba!
60
61
  coveralls
61
62
  minitest (~> 5.0)
63
+ oj (~> 3.10)
62
64
  rake (~> 13.0)
63
65
  rubocop (>= 0.79.0)
64
66
  rubocop-performance (~> 1.7.1)
@@ -10,6 +10,7 @@ module Alba
10
10
 
11
11
  class << self
12
12
  attr_reader :backend
13
+ attr_accessor :default_serializer
13
14
 
14
15
  def backend=(backend)
15
16
  @backend = backend&.to_sym
@@ -20,6 +21,7 @@ module Alba
20
21
 
21
22
  resource_class.class_eval(&block)
22
23
  resource = resource_class.new(object)
24
+ with ||= @default_serializer
23
25
  resource.serialize(with: with)
24
26
  end
25
27
 
@@ -7,12 +7,17 @@ require 'alba/serializers/default_serializer'
7
7
  module Alba
8
8
  # This module represents what should be serialized
9
9
  module Resource
10
- DSLS = [:_attributes, :_serializer].freeze
10
+ DSLS = [:_attributes, :_serializer, :_key].freeze
11
11
  def self.included(base)
12
12
  base.class_eval do
13
13
  # Initialize
14
14
  DSLS.each do |name|
15
- initial = name == :_serializer ? nil : {}
15
+ initial = case name
16
+ when :_attributes
17
+ {}
18
+ when :_serializer, :_name
19
+ nil
20
+ end
16
21
  instance_variable_set("@#{name}", initial) unless instance_variable_defined?("@#{name}")
17
22
  end
18
23
  end
@@ -38,16 +43,21 @@ module Alba
38
43
  else
39
44
  raise ArgumentError, 'Unexpected type for with, possible types are Class or Proc'
40
45
  end
41
- serializer.new(serializable_hash).serialize
46
+ serializer.new(self).serialize
42
47
  end
43
48
 
44
- def serializable_hash
45
- @_attributes.transform_values do |attribute|
49
+ def serializable_hash(with_key: true)
50
+ serializable_hash = @_attributes.transform_values do |attribute|
46
51
  attribute.to_hash(@_resource)
47
52
  end
53
+ with_key && @_key ? {@_key => serializable_hash} : serializable_hash
48
54
  end
49
55
  alias to_hash serializable_hash
50
56
 
57
+ def key
58
+ @_key || self.class.name.delete_suffix('Resource').downcase.gsub(/:{2}/, '_').to_sym
59
+ end
60
+
51
61
  private
52
62
 
53
63
  def inline_extended_serializer(with)
@@ -86,6 +96,10 @@ module Alba
86
96
  def serializer(name)
87
97
  @_serializer = name <= Alba::Serializer ? name : nil
88
98
  end
99
+
100
+ def key(key)
101
+ @_key = key.to_sym
102
+ end
89
103
  end
90
104
  end
91
105
  end
@@ -1,6 +1,5 @@
1
1
  module Alba
2
2
  # This module represents how a resource should be serialized.
3
- #
4
3
  module Serializer
5
4
  def self.included(base)
6
5
  base.include InstanceMethods
@@ -10,22 +9,27 @@ module Alba
10
9
  # Instance methods
11
10
  module InstanceMethods
12
11
  def initialize(resource)
13
- @_resource = resource
14
12
  @_opts = self.class._opts || {}
15
- key = @_opts[:key]
16
- @_resource = {key.to_sym => @_resource} if key
13
+ key = case @_opts[:key]
14
+ when true
15
+ resource.key
16
+ else
17
+ @_opts[:key]
18
+ end
19
+ @hash = resource.serializable_hash(with_key: false)
20
+ @hash = {key.to_sym => @hash} if key
17
21
  end
18
22
 
19
23
  def serialize
20
24
  fallback = lambda do
21
25
  require 'json'
22
- JSON.dump(@_resource)
26
+ JSON.dump(@hash)
23
27
  end
24
28
  case Alba.backend
25
29
  when :oj
26
30
  begin
27
31
  require 'oj'
28
- -> { Oj.dump(@_resource, mode: :strict) }
32
+ -> { Oj.dump(@hash, mode: :strict) }
29
33
  rescue LoadError
30
34
  fallback
31
35
  end
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.6.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.5.0
4
+ version: 0.6.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-30 00:00:00.000000000 Z
11
+ date: 2020-07-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast and flexible JSON serializer
14
14
  email: