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.
- checksums.yaml +4 -4
- data/.codeclimate.yml +11 -0
- data/.github/dependabot.yml +4 -18
- data/.github/workflows/codeql-analysis.yml +70 -0
- data/.github/workflows/main.yml +4 -4
- data/.github/workflows/perf.yml +2 -2
- data/.rubocop.yml +5 -1
- data/CHANGELOG.md +34 -0
- data/CONTRIBUTING.md +30 -0
- data/Gemfile +6 -2
- data/HACKING.md +41 -0
- data/README.md +599 -128
- data/Rakefile +2 -2
- data/alba.gemspec +8 -4
- data/benchmark/README.md +81 -0
- data/benchmark/collection.rb +60 -74
- data/benchmark/single_resource.rb +33 -2
- data/docs/migrate_from_jbuilder.md +18 -4
- data/docs/rails.md +44 -0
- data/lib/alba/association.rb +49 -9
- data/lib/alba/conditional_attribute.rb +54 -0
- data/lib/alba/default_inflector.rb +13 -24
- data/lib/alba/errors.rb +10 -0
- data/lib/alba/layout.rb +67 -0
- data/lib/alba/nested_attribute.rb +18 -0
- data/lib/alba/resource.rb +240 -156
- data/lib/alba/typed_attribute.rb +1 -1
- data/lib/alba/version.rb +1 -1
- data/lib/alba.rb +43 -58
- data/logo/alba-card.png +0 -0
- data/logo/alba-sign.png +0 -0
- data/logo/alba-typography.png +0 -0
- metadata +21 -11
- data/gemfiles/all.gemfile +0 -19
- data/lib/alba/key_transform_factory.rb +0 -33
- data/lib/alba/many.rb +0 -21
- data/lib/alba/one.rb +0 -21
- data/sider.yml +0 -60
@@ -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
|
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
|
-
|
5
|
-
|
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
|
data/lib/alba/errors.rb
ADDED
data/lib/alba/layout.rb
ADDED
@@ -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
|