alba 0.13.1 → 1.3.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.
@@ -1,31 +0,0 @@
1
- module Alba
2
- # Transform keys using `ActiveSupport::Inflector`
3
- module KeyTransformer
4
- begin
5
- require 'active_support/inflector'
6
- rescue LoadError
7
- raise ::Alba::Error, 'To use transform_keys, please install `ActiveSupport` gem.'
8
- end
9
-
10
- module_function
11
-
12
- # Transform key as given transform_type
13
- #
14
- # @params key [String] key to be transformed
15
- # @params transform_type [Symbol] transform type
16
- # @return [String] transformed key
17
- # @raise [Alba::Error] when transform_type is not supported
18
- def transform(key, transform_type)
19
- case transform_type
20
- when :camel
21
- ActiveSupport::Inflector.camelize(key)
22
- when :lower_camel
23
- ActiveSupport::Inflector.camelize(key, false)
24
- when :dash
25
- ActiveSupport::Inflector.dasherize(key)
26
- else
27
- raise ::Alba::Error, "Unknown transform_type: #{transform_type}. Supported transform_type are :camel, :lower_camel and :dash."
28
- end
29
- end
30
- end
31
- end
@@ -1,75 +0,0 @@
1
- module Alba
2
- # This module represents how a resource should be serialized.
3
- module Serializer
4
- # @!parse include InstanceMethods
5
- # @!parse extend ClassMethods
6
-
7
- # @private
8
- def self.included(base)
9
- super
10
- base.instance_variable_set('@_opts', {}) unless base.instance_variable_defined?('@_opts')
11
- base.instance_variable_set('@_metadata', {}) unless base.instance_variable_defined?('@_metadata')
12
- base.include InstanceMethods
13
- base.extend ClassMethods
14
- end
15
-
16
- # Instance methods
17
- module InstanceMethods
18
- # @param resource [Alba::Resource]
19
- def initialize(resource)
20
- @resource = resource
21
- @hash = resource.serializable_hash
22
- @hash = {key.to_sym => @hash} if key
23
- return if metadata.empty?
24
-
25
- # @hash is either Hash or Array
26
- @hash.is_a?(Hash) ? @hash.merge!(metadata.to_h) : @hash << metadata
27
- end
28
-
29
- # Use real encoder to actually serialize to JSON
30
- #
31
- # @return [String] JSON string
32
- def serialize
33
- Alba.encoder.call(@hash)
34
- end
35
-
36
- private
37
-
38
- def key
39
- opts = self.class._opts
40
- opts[:key] == true ? @resource.key : opts[:key]
41
- end
42
-
43
- def metadata
44
- metadata = self.class._metadata
45
- metadata.transform_values { |block| block.call(@resource.object) }
46
- end
47
- end
48
-
49
- # Class methods
50
- module ClassMethods
51
- attr_reader :_opts, :_metadata
52
-
53
- # @private
54
- def inherited(subclass)
55
- super
56
- %w[_opts _metadata].each { |name| subclass.instance_variable_set("@#{name}", public_send(name).clone) }
57
- end
58
-
59
- # Set options, currently key only
60
- #
61
- # @param key [Boolean, Symbol]
62
- def set(key: false)
63
- @_opts[:key] = key
64
- end
65
-
66
- # Set metadata
67
- #
68
- # @param name [String, Symbol] key for the metadata
69
- # @param block [Block] the content of the metadata
70
- def metadata(name, &block)
71
- @_metadata[name.to_sym] = block
72
- end
73
- end
74
- end
75
- end