Skip to content

Commit 819137e

Browse files
committed
Rename Adapter.get to Adapter.lookup
Per rails-api#1017 (comment) comment by sandstrom in discussion of the inherited hook > I'm thinking that it would be better to register adapters manually, without using the hook, i.e. > have people call ActiveModel::Serializer::Adapter.register directly (perhaps in an initializer). > Possibly, some inspiration can be taken from how ActiveJob adapters are wired[1]. > [1] https://github.com/rails/rails/blob/a11571cec3213753d63ac3e6b4bb3b97fe2594a6/activejob/lib/active_job/queue_adapter.rb#L52-L56
1 parent 6f6c03f commit 819137e

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

docs/general/adapters.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ ActiveModel::Serializer::Adapter.register(:special_adapter, MyAdapter)
9191

9292
| `ActiveModel::Serializer::Adapter.adapter_map` | A Hash of all known adapters { adapter_name => adapter_class } |
9393
| `ActiveModel::Serializer::Adapter.adapters` | A (sorted) Array of all known adapter_names |
94-
| `ActiveModel::Serializer::Adapter.get(name_or_klass)` | The adapter_class, else raises an `ActiveModel::Serializer::Adapter::UnknownAdapter` error |
95-
| `ActiveModel::Serializer::Adapter.adapter_class(adapter)` | delegates to `ActiveModel::Serializer::Adapter.get(adapter)` |
96-
| `ActiveModel::Serializer.adapter` | a convenience method for `ActiveModel::Serializer::Adapter.get(config.adapter)` |
94+
| `ActiveModel::Serializer::Adapter.lookup(name_or_klass)` | The adapter_class, else raises an `ActiveModel::Serializer::Adapter::UnknownAdapter` error |
95+
| `ActiveModel::Serializer::Adapter.adapter_class(adapter)` | delegates to `ActiveModel::Serializer::Adapter.lookup(adapter)` |
96+
| `ActiveModel::Serializer.adapter` | a convenience method for `ActiveModel::Serializer::Adapter.lookup(config.adapter)` |
9797

9898
The registered adapter name is always a String, but may be looked up as a Symbol or String.
9999
Helpfully, the Symbol or String is underscored, so that `get(:my_adapter)` and `get("MyAdapter")`

lib/active_model/serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def self.serializer_for(resource, options = {})
9494
end
9595
end
9696

97-
# @see ActiveModel::Serializer::Adapter.get
97+
# @see ActiveModel::Serializer::Adapter.lookup
9898
def self.adapter
99-
ActiveModel::Serializer::Adapter.get(config.adapter)
99+
ActiveModel::Serializer::Adapter.lookup(config.adapter)
100100
end
101101

102102
def self.root_name

lib/active_model/serializer/adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def self.create(resource, options = {})
1717
klass.new(resource, options)
1818
end
1919

20-
# @see ActiveModel::Serializer::Adapter.get
20+
# @see ActiveModel::Serializer::Adapter.lookup
2121
def self.adapter_class(adapter)
22-
ActiveModel::Serializer::Adapter.get(adapter)
22+
ActiveModel::Serializer::Adapter.lookup(adapter)
2323
end
2424

2525
# Only the Adapter class has these methods.
@@ -49,7 +49,7 @@ def register(name, klass)
4949
# @param adapter [String, Symbol, Class] name to fetch adapter by
5050
# @return [ActiveModel::Serializer::Adapter] subclass of Adapter
5151
# @raise [UnknownAdapterError]
52-
def get(adapter)
52+
def lookup(adapter)
5353
# 1. return if is a class
5454
return adapter if adapter.is_a?(Class)
5555
adapter_name = adapter.to_s.underscore

test/serializers/adapter_for_test.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,33 @@ def test_adapters
7878
]
7979
end
8080

81-
def test_get_adapter_by_string_name
82-
assert_equal ActiveModel::Serializer::Adapter.get('json'.freeze), ActiveModel::Serializer::Adapter::Json
81+
def test_lookup_adapter_by_string_name
82+
assert_equal ActiveModel::Serializer::Adapter.lookup('json'.freeze), ActiveModel::Serializer::Adapter::Json
8383
end
8484

85-
def test_get_adapter_by_symbol_name
86-
assert_equal ActiveModel::Serializer::Adapter.get(:json), ActiveModel::Serializer::Adapter::Json
85+
def test_lookup_adapter_by_symbol_name
86+
assert_equal ActiveModel::Serializer::Adapter.lookup(:json), ActiveModel::Serializer::Adapter::Json
8787
end
8888

89-
def test_get_adapter_by_class
89+
def test_lookup_adapter_by_class
9090
klass = ActiveModel::Serializer::Adapter::Json
91-
assert_equal ActiveModel::Serializer::Adapter.get(klass), klass
91+
assert_equal ActiveModel::Serializer::Adapter.lookup(klass), klass
9292
end
9393

94-
def test_get_adapter_from_environment_registers_adapter
94+
def test_lookup_adapter_from_environment_registers_adapter
9595
ActiveModel::Serializer::Adapter.const_set(:AdapterFromEnvironment, Class.new)
9696
klass = ::ActiveModel::Serializer::Adapter::AdapterFromEnvironment
9797
name = 'adapter_from_environment'.freeze
98-
assert_equal ActiveModel::Serializer::Adapter.get(name), klass
98+
assert_equal ActiveModel::Serializer::Adapter.lookup(name), klass
9999
assert ActiveModel::Serializer::Adapter.adapters.include?(name)
100100
ensure
101101
ActiveModel::Serializer::Adapter.adapter_map.delete(name)
102102
ActiveModel::Serializer::Adapter.send(:remove_const, :AdapterFromEnvironment)
103103
end
104104

105-
def test_get_adapter_for_unknown_name
105+
def test_lookup_adapter_for_unknown_name
106106
assert_raises UnknownAdapterError do
107-
ActiveModel::Serializer::Adapter.get(:json_simple)
107+
ActiveModel::Serializer::Adapter.lookup(:json_simple)
108108
end
109109
end
110110

@@ -118,7 +118,7 @@ def test_register_adapter
118118
new_adapter_klass = Class.new
119119
ActiveModel::Serializer::Adapter.register(new_adapter_name, new_adapter_klass)
120120
assert ActiveModel::Serializer::Adapter.adapters.include?('foo'.freeze)
121-
assert ActiveModel::Serializer::Adapter.get(:foo), new_adapter_klass
121+
assert ActiveModel::Serializer::Adapter.lookup(:foo), new_adapter_klass
122122
ensure
123123
ActiveModel::Serializer::Adapter.adapter_map.delete(new_adapter_name.to_s)
124124
end
@@ -127,7 +127,7 @@ def test_inherited_adapter_hooks_register_adapter
127127
Object.const_set(:MyAdapter, Class.new)
128128
my_adapter = MyAdapter
129129
ActiveModel::Serializer::Adapter.inherited(my_adapter)
130-
assert_equal ActiveModel::Serializer::Adapter.get(:my_adapter), my_adapter
130+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
131131
ensure
132132
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
133133
Object.send(:remove_const, :MyAdapter)
@@ -138,7 +138,7 @@ def test_inherited_adapter_hooks_register_demodulized_adapter
138138
MyNamespace.const_set(:MyAdapter, Class.new)
139139
my_adapter = MyNamespace::MyAdapter
140140
ActiveModel::Serializer::Adapter.inherited(my_adapter)
141-
assert_equal ActiveModel::Serializer::Adapter.get(:my_adapter), my_adapter
141+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
142142
ensure
143143
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
144144
MyNamespace.send(:remove_const, :MyAdapter)
@@ -152,8 +152,8 @@ def test_inherited_adapter_hooks_register_subclass_of_registered_adapter
152152
my_subclassed_adapter = MySubclassedAdapter
153153
ActiveModel::Serializer::Adapter.inherited(my_adapter)
154154
ActiveModel::Serializer::Adapter.inherited(my_subclassed_adapter)
155-
assert_equal ActiveModel::Serializer::Adapter.get(:my_adapter), my_adapter
156-
assert_equal ActiveModel::Serializer::Adapter.get(:my_subclassed_adapter), my_subclassed_adapter
155+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
156+
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_subclassed_adapter), my_subclassed_adapter
157157
ensure
158158
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
159159
ActiveModel::Serializer::Adapter.adapter_map.delete('my_subclassed_adapter'.freeze)

0 commit comments

Comments
 (0)