alba 1.5.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.
@@ -1,36 +1,25 @@
1
+ begin
2
+ require 'active_support/inflector'
3
+ require 'active_support/core_ext/module/delegation'
4
+ rescue LoadError
5
+ raise ::Alba::Error, 'To use default inflector, please install `ActiveSupport` gem.'
6
+ end
7
+
1
8
  module Alba
2
- # This module represents the inflector, which is used by default
9
+ # This module has two purposes.
10
+ # One is that we require `active_support/inflector` in this module so that we don't do that all over the place.
11
+ # Another is that `ActiveSupport::Inflector` doesn't have `camelize_lower` method that we want it to have, so this module works as an adapter.
3
12
  module DefaultInflector
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
- # Camelizes a key
13
- #
14
- # @param key [String] key to be camelized
15
- # @return [String] camelized key
16
- def camelize(key)
17
- ActiveSupport::Inflector.camelize(key)
13
+ class << self
14
+ delegate :camelize, :dasherize, :underscore, :classify, :demodulize, :pluralize, to: ActiveSupport::Inflector
18
15
  end
19
16
 
20
17
  # Camelizes a key, 1st letter lowercase
21
18
  #
22
19
  # @param key [String] key to be camelized
23
20
  # @return [String] camelized key
24
- def camelize_lower(key)
21
+ def self.camelize_lower(key)
25
22
  ActiveSupport::Inflector.camelize(key, false)
26
23
  end
27
-
28
- # Dasherizes a key
29
- #
30
- # @param key [String] key to be dasherized
31
- # @return [String] dasherized key
32
- def dasherize(key)
33
- ActiveSupport::Inflector.dasherize(key)
34
- end
35
24
  end
36
25
  end
@@ -0,0 +1,10 @@
1
+ module Alba
2
+ # Base class for Errors
3
+ class Error < StandardError; end
4
+
5
+ # Error class for backend which is not supported
6
+ class UnsupportedBackend < Error; end
7
+
8
+ # Error class for type which is not supported
9
+ class UnsupportedType < Error; end
10
+ end
@@ -0,0 +1,67 @@
1
+ require 'erb'
2
+ require 'forwardable'
3
+
4
+ module Alba
5
+ # Layout serialization
6
+ class Layout
7
+ extend Forwardable
8
+
9
+ def_delegators :@resource, :object, :params, :serializable_hash, :to_h
10
+
11
+ # @params file [String] name of the layout file
12
+ # @params inline [Proc] a proc returning JSON string or a Hash representing JSON
13
+ def initialize(file:, inline:)
14
+ if file
15
+ raise ArgumentError, 'File layout must be a String representing filename' unless file.is_a?(String)
16
+
17
+ @body = file
18
+ elsif inline
19
+ raise ArgumentError, 'Inline layout must be a Proc returning a Hash or a String' unless inline.is_a?(Proc)
20
+
21
+ @body = inline
22
+ else
23
+ raise ArgumentError, 'Layout must be either String or Proc'
24
+ end
25
+ end
26
+
27
+ # Serialize within layout
28
+ #
29
+ # @param resource [Alba::Resource] the original resource calling this layout
30
+ # @param serialized_json [String] JSON string for embedding
31
+ # @param binding [Binding] context for serialization
32
+ def serialize(resource:, serialized_json:, binding:)
33
+ @resource = resource
34
+ @serialized_json = serialized_json
35
+
36
+ if @body.is_a?(String)
37
+ serialize_within_string_layout(binding)
38
+ else
39
+ serialize_within_inline_layout
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :serialized_json
46
+
47
+ def serialize_within_string_layout(bnd)
48
+ ERB.new(File.read(@body)).result(bnd)
49
+ end
50
+
51
+ def serialize_within_inline_layout
52
+ inline = instance_eval(&@body)
53
+ case inline
54
+ when Hash then encode(inline)
55
+ when String then inline
56
+ else
57
+ raise Alba::Error, 'Inline layout must be a Proc returning a Hash or a String'
58
+ end
59
+ end
60
+
61
+ # This methods exists here instead of delegation because
62
+ # `Alba::Resource#encode` is private and it prints warning if we use `def_delegators`
63
+ def encode(hash)
64
+ @resource.instance_eval { encode(hash) }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,18 @@
1
+ module Alba
2
+ # Representing nested attribute
3
+ class NestedAttribute
4
+ # @param key_transformation [Symbol] determines how to transform keys
5
+ # @param block [Proc] class body
6
+ def initialize(key_transformation: :none, &block)
7
+ @key_transformation = key_transformation
8
+ @block = block
9
+ end
10
+
11
+ # @return [Hash]
12
+ def value(object)
13
+ resource_class = Alba.resource_class(&@block)
14
+ resource_class.transform_keys(@key_transformation)
15
+ resource_class.new(object).serializable_hash
16
+ end
17
+ end
18
+ end