Skip to content

Commit

Permalink
[Chore] Move transform_key method to Alba module
Browse files Browse the repository at this point in the history
The main purpose of this commit is to prepare for type export feature.
Also this method is generic enough, so it's safe to extract it for
smaller `Alba::Resource`.
  • Loading branch information
okuramasafumi committed Aug 5, 2024
1 parent 2a43f3d commit 3466aff
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
20 changes: 20 additions & 0 deletions lib/alba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ def regularize_key(key)
@symbolize_keys ? key.to_sym : key.to_s
end

# Transform a key with given transform_type
#
# @param key [String] a target key
# @param transform_type [Symbol] a transform type, either one of `camel`, `lower_camel`, `dash` or `snake`
# @return [String]
def transform_key(key, transform_type:)
raise Alba::Error, 'Inflector is nil. You must set inflector before transforming keys.' unless inflector

key = key.to_s

k = case transform_type
when :camel then inflector.camelize(key)
when :lower_camel then inflector.camelize_lower(key)
when :dash then inflector.dasherize(key)
when :snake then inflector.underscore(key)
else raise Alba::Error, "Unknown transform type: #{transform_type}"
end
regularize_key(k)
end

# Register types, used for both builtin and custom types
#
# @see Alba::Type
Expand Down
18 changes: 1 addition & 17 deletions lib/alba/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,7 @@ def handle_error(error, obj, key, attribute, hash)
def transform_key(key)
return Alba.regularize_key(key) if @_transform_type == :none || key.nil? || key.empty? # We can skip transformation

inflector = Alba.inflector
raise Alba::Error, 'Inflector is nil. You must set inflector before transforming keys.' unless inflector

Alba.regularize_key(_transform_key(inflector, key.to_s))
end

def _transform_key(inflector, key)
case @_transform_type
when :camel then inflector.camelize(key)
when :lower_camel then inflector.camelize_lower(key)
when :dash then inflector.dasherize(key)
when :snake then inflector.underscore(key)
else
# :nocov:
raise Alba::Error, "Unknown transform type: #{@_transform_type}"
# :nocov:
end
Alba.transform_key(key, transform_type: @_transform_type)
end

def fetch_attribute(obj, key, attribute) # rubocop:disable Metrics/CyclomaticComplexity
Expand Down
44 changes: 44 additions & 0 deletions test/alba_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,48 @@ def test_inline_serialization_for_multiple_root_keys
end
)
end

def test_transform_key_with_camel
Alba.inflector = :active_support
assert_equal(
'FooBar',
Alba.transform_key(:foo_bar, transform_type: :camel)
)
Alba.inflector = nil
end

def test_transform_key_with_lower_camel
Alba.inflector = :active_support
assert_equal(
'fooBar',
Alba.transform_key('foo_bar', transform_type: :lower_camel)
)
Alba.inflector = nil
end

def test_transform_key_with_dash
Alba.inflector = :active_support
assert_equal(
'foo-bar',
Alba.transform_key(:foo_bar, transform_type: :dash)
)
Alba.inflector = nil
end

def test_transform_key_with_snake
Alba.inflector = :active_support
assert_equal(
'foo_bar',
Alba.transform_key('FooBar', transform_type: :snake)
)
Alba.inflector = nil
end

def test_transform_key_with_unknown
Alba.inflector = :active_support
assert_raises(Alba::Error) do
Alba.transform_key(:foo_bar, transform_type: :this_is_an_error)
end
Alba.inflector = nil
end
end

0 comments on commit 3466aff

Please sign in to comment.