alba 0.0.1 → 0.1.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +30 -6
- data/lib/alba.rb +5 -4
- data/lib/alba/attribute.rb +19 -0
- data/lib/alba/resource.rb +67 -0
- data/lib/alba/serializer.rb +45 -0
- data/lib/alba/serializers/default_serializer.rb +9 -0
- data/lib/alba/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62d87c709e80629fa4f9373e113a141da7489703d309fd1ed6b9b411e427a101
|
4
|
+
data.tar.gz: 3aacb3b018632b50cd2c71052c783e6aae97d6d79c2d1f0bb107ba07cc0d5bbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3eb9360c0f0a608252d521b89911308b45584c4456040579d844cc7cf09fc839f12f8a908cd20c9f6fd421123f17ca6248948d80e7d36e82adb5a6a42e82b460
|
7
|
+
data.tar.gz: d5ec4be5ede9d7cd359ed5f50af51d37c6926b99d5130f679655fec3949962c57d4c215dc212413e76f85ef20b74aa036e2b1955837ff113bcf41d3dc6f180bc
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -21,12 +21,36 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
class User
|
25
|
+
attr_accessor :id, :name, :email, :created_at, :updated_at
|
26
|
+
def initialize(id, name, email)
|
27
|
+
@id = id
|
28
|
+
@name = name
|
29
|
+
@email = email
|
30
|
+
@created_at = Time.now
|
31
|
+
@updated_at = Time.now
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class UserResource
|
36
|
+
include Alba::Resource
|
37
|
+
|
38
|
+
attributes :id, :name
|
39
|
+
|
40
|
+
attribute :name_with_email do |resource|
|
41
|
+
"#{resource.name}: #{resource.email}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class SerializerWithKey
|
46
|
+
include Alba::Serializer
|
47
|
+
|
48
|
+
set key: :user
|
49
|
+
end
|
50
|
+
|
51
|
+
user = User.new(1, 'Masafumi OKURA', 'masafumi@example.com')
|
52
|
+
UserResource.new(user).serialize
|
53
|
+
# => "{\"id\":1,\"name\":\"Masafumi OKURA\",\"name_with_email\":\"Masafumi OKURA: masafumi@example.com\"}"
|
30
54
|
```
|
31
55
|
|
32
56
|
## Development
|
data/lib/alba.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
require 'alba/version'
|
2
|
+
require 'alba/resource'
|
2
3
|
require 'json'
|
3
4
|
|
4
5
|
# Core module
|
5
6
|
module Alba
|
6
7
|
class Error < StandardError; end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
class << self
|
10
|
+
attr_reader :backend
|
10
11
|
end
|
11
12
|
|
12
|
-
def self.backend
|
13
|
-
@backend
|
13
|
+
def self.backend=(backend)
|
14
|
+
@backend = backend&.to_sym
|
14
15
|
end
|
15
16
|
|
16
17
|
def self.serialize(object)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Alba
|
2
|
+
# This class represents an attribute, which is serialized
|
3
|
+
# by either sending message or calling a Proc.
|
4
|
+
class Attribute
|
5
|
+
def initialize(name:, method:)
|
6
|
+
@name = name
|
7
|
+
@method = method
|
8
|
+
end
|
9
|
+
|
10
|
+
def serialize(target)
|
11
|
+
case @method
|
12
|
+
when Symbol, String
|
13
|
+
target.public_send(@method)
|
14
|
+
when Proc
|
15
|
+
@method.call(target)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'alba/serializer'
|
2
|
+
require 'alba/attribute'
|
3
|
+
require 'alba/serializers/default_serializer'
|
4
|
+
|
5
|
+
module Alba
|
6
|
+
# This module represents what should be serialized
|
7
|
+
module Resource
|
8
|
+
def self.included(base)
|
9
|
+
base.include InstanceMethods
|
10
|
+
base.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
# Instance methods
|
14
|
+
module InstanceMethods
|
15
|
+
def initialize(resource)
|
16
|
+
@_resource = resource
|
17
|
+
@_attributes = self.class._attributes
|
18
|
+
@_serializer_class = self.class._serializer_class
|
19
|
+
end
|
20
|
+
|
21
|
+
def serialize(with: nil)
|
22
|
+
serializer_class = case with
|
23
|
+
when ->(obj) { obj.is_a?(Class) && obj.ancestors.include?(Alba::Serializer) }
|
24
|
+
with
|
25
|
+
when Symbol
|
26
|
+
const_get(with.to_s.capitalize)
|
27
|
+
when String
|
28
|
+
const_get(with)
|
29
|
+
when nil
|
30
|
+
@_serializer_class || Alba::Serializers::DefaultSerializer
|
31
|
+
end
|
32
|
+
# opts = serializer.opts
|
33
|
+
serialiable_hash = @_attributes.map do |name, attribute|
|
34
|
+
[name, attribute.serialize(@_resource)]
|
35
|
+
end.to_h
|
36
|
+
serializer_class.new(serialiable_hash).serialize
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Class methods
|
41
|
+
module ClassMethods
|
42
|
+
attr_accessor :_attributes, :_serializer_class
|
43
|
+
|
44
|
+
def inherited(subclass)
|
45
|
+
@_attributes = {} unless defined?(@_attributes)
|
46
|
+
@_serializer_class = nil unless defined?(@_serializer_class)
|
47
|
+
subclass._attributes = @_attributes
|
48
|
+
subclass._serializer_class = @_serializer_class
|
49
|
+
end
|
50
|
+
|
51
|
+
def attributes(*attrs)
|
52
|
+
@_attributes = {} unless defined? @_attributes
|
53
|
+
attrs.each { |attr_name| @_attributes[attr_name] = Attribute.new(name: attr_name, method: attr_name) }
|
54
|
+
end
|
55
|
+
|
56
|
+
def attribute(name, &block)
|
57
|
+
raise ArgumentError, 'No block given in attribute method' unless block
|
58
|
+
|
59
|
+
@_attributes[name] = Attribute.new(name: name, method: block)
|
60
|
+
end
|
61
|
+
|
62
|
+
def serializer(name)
|
63
|
+
@_serializer_class = name.ancestors.include?(Alba::Serializer) ? name : nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Alba
|
2
|
+
# This module represents how a resource should be serialized.
|
3
|
+
#
|
4
|
+
module Serializer
|
5
|
+
def self.included(base)
|
6
|
+
base.include InstanceMethods
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
# Instance methods
|
11
|
+
module InstanceMethods
|
12
|
+
def initialize(resource)
|
13
|
+
@_resource = resource
|
14
|
+
@_opts = self.class._opts || {}
|
15
|
+
key = @_opts[:key]
|
16
|
+
@_resource = {key.to_sym => @_resource} if key
|
17
|
+
end
|
18
|
+
|
19
|
+
def serialize
|
20
|
+
fallback = -> { @_resource.to_json }
|
21
|
+
case Alba.backend
|
22
|
+
when :oj
|
23
|
+
begin
|
24
|
+
require 'oj'
|
25
|
+
-> { Oj.dump(@_resource) }
|
26
|
+
rescue LoadError
|
27
|
+
fallback
|
28
|
+
end
|
29
|
+
else
|
30
|
+
fallback
|
31
|
+
end.call
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Class methods
|
36
|
+
module ClassMethods
|
37
|
+
attr_reader :_opts
|
38
|
+
|
39
|
+
def set(key: false)
|
40
|
+
@_opts ||= {}
|
41
|
+
@_opts[:key] = key
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Alba
|
2
|
+
module Serializers
|
3
|
+
# DefaultSerializer class is used when a user doesn't specify serializer opt.
|
4
|
+
# It's basically an alias of Alba::Serializer, but since it's a module this class simply include it.
|
5
|
+
class DefaultSerializer
|
6
|
+
include Alba::Serializer
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/lib/alba/version.rb
CHANGED
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.0
|
4
|
+
version: 0.1.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-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Fast and flexible JSON serializer
|
14
14
|
email:
|
@@ -30,6 +30,10 @@ files:
|
|
30
30
|
- bin/console
|
31
31
|
- bin/setup
|
32
32
|
- lib/alba.rb
|
33
|
+
- lib/alba/attribute.rb
|
34
|
+
- lib/alba/resource.rb
|
35
|
+
- lib/alba/serializer.rb
|
36
|
+
- lib/alba/serializers/default_serializer.rb
|
33
37
|
- lib/alba/version.rb
|
34
38
|
homepage: https://github.com/okuramasafumi/alba
|
35
39
|
licenses:
|