Skip to content

Commit

Permalink
Rename Adapter.get to Adapter.lookup
Browse files Browse the repository at this point in the history
Per #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/master/activejob/lib/active_job/queue_adapter.rb
  • Loading branch information
bf4 committed Sep 9, 2015
1 parent 9952073 commit e096d22
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/general/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ ActiveModel::Serializer::Adapter.register(:special_adapter, MyAdapter)

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

The registered adapter name is always a String, but may be looked up as a Symbol or String.
Helpfully, the Symbol or String is underscored, so that `get(:my_adapter)` and `get("MyAdapter")`
Expand Down
4 changes: 2 additions & 2 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def self.serializer_for(resource, options = {})
end
end

# @see ActiveModel::Serializer::Adapter.get
# @see ActiveModel::Serializer::Adapter.lookup
def self.adapter
ActiveModel::Serializer::Adapter.get(config.adapter)
ActiveModel::Serializer::Adapter.lookup(config.adapter)
end

def self.root_name
Expand Down
6 changes: 3 additions & 3 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def self.create(resource, options = {})
klass.new(resource, options)
end

# @see ActiveModel::Serializer::Adapter.get
# @see ActiveModel::Serializer::Adapter.lookup
def self.adapter_class(adapter)
ActiveModel::Serializer::Adapter.get(adapter)
ActiveModel::Serializer::Adapter.lookup(adapter)
end

# Only the Adapter class has these methods.
Expand Down Expand Up @@ -49,7 +49,7 @@ def register(name, klass)
# @param adapter [String, Symbol, Class] name to fetch adapter by
# @return [ActiveModel::Serializer::Adapter] subclass of Adapter
# @raise [UnknownAdapterError]
def get(adapter)
def lookup(adapter)
# 1. return if is a class
return adapter if adapter.is_a?(Class)
adapter_name = adapter.to_s.underscore
Expand Down
30 changes: 15 additions & 15 deletions test/serializers/adapter_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,33 @@ def test_adapters
]
end

def test_get_adapter_by_string_name
assert_equal ActiveModel::Serializer::Adapter.get('json'.freeze), ActiveModel::Serializer::Adapter::Json
def test_lookup_adapter_by_string_name
assert_equal ActiveModel::Serializer::Adapter.lookup('json'.freeze), ActiveModel::Serializer::Adapter::Json
end

def test_get_adapter_by_symbol_name
assert_equal ActiveModel::Serializer::Adapter.get(:json), ActiveModel::Serializer::Adapter::Json
def test_lookup_adapter_by_symbol_name
assert_equal ActiveModel::Serializer::Adapter.lookup(:json), ActiveModel::Serializer::Adapter::Json
end

def test_get_adapter_by_class
def test_lookup_adapter_by_class
klass = ActiveModel::Serializer::Adapter::Json
assert_equal ActiveModel::Serializer::Adapter.get(klass), klass
assert_equal ActiveModel::Serializer::Adapter.lookup(klass), klass
end

def test_get_adapter_from_environment_registers_adapter
def test_lookup_adapter_from_environment_registers_adapter
ActiveModel::Serializer::Adapter.const_set(:AdapterFromEnvironment, Class.new)
klass = ::ActiveModel::Serializer::Adapter::AdapterFromEnvironment
name = 'adapter_from_environment'.freeze
assert_equal ActiveModel::Serializer::Adapter.get(name), klass
assert_equal ActiveModel::Serializer::Adapter.lookup(name), klass
assert ActiveModel::Serializer::Adapter.adapters.include?(name)
ensure
ActiveModel::Serializer::Adapter.adapter_map.delete(name)
ActiveModel::Serializer::Adapter.send(:remove_const, :AdapterFromEnvironment)
end

def test_get_adapter_for_unknown_name
def test_lookup_adapter_for_unknown_name
assert_raises UnknownAdapterError do
ActiveModel::Serializer::Adapter.get(:json_simple)
ActiveModel::Serializer::Adapter.lookup(:json_simple)
end
end

Expand All @@ -118,7 +118,7 @@ def test_register_adapter
new_adapter_klass = Class.new
ActiveModel::Serializer::Adapter.register(new_adapter_name, new_adapter_klass)
assert ActiveModel::Serializer::Adapter.adapters.include?('foo'.freeze)
assert ActiveModel::Serializer::Adapter.get(:foo), new_adapter_klass
assert ActiveModel::Serializer::Adapter.lookup(:foo), new_adapter_klass
ensure
ActiveModel::Serializer::Adapter.adapter_map.delete(new_adapter_name.to_s)
end
Expand All @@ -127,7 +127,7 @@ def test_inherited_adapter_hooks_register_adapter
Object.const_set(:MyAdapter, Class.new)
my_adapter = MyAdapter
ActiveModel::Serializer::Adapter.inherited(my_adapter)
assert_equal ActiveModel::Serializer::Adapter.get(:my_adapter), my_adapter
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
ensure
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
Object.send(:remove_const, :MyAdapter)
Expand All @@ -138,7 +138,7 @@ def test_inherited_adapter_hooks_register_demodulized_adapter
MyNamespace.const_set(:MyAdapter, Class.new)
my_adapter = MyNamespace::MyAdapter
ActiveModel::Serializer::Adapter.inherited(my_adapter)
assert_equal ActiveModel::Serializer::Adapter.get(:my_adapter), my_adapter
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
ensure
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
MyNamespace.send(:remove_const, :MyAdapter)
Expand All @@ -152,8 +152,8 @@ def test_inherited_adapter_hooks_register_subclass_of_registered_adapter
my_subclassed_adapter = MySubclassedAdapter
ActiveModel::Serializer::Adapter.inherited(my_adapter)
ActiveModel::Serializer::Adapter.inherited(my_subclassed_adapter)
assert_equal ActiveModel::Serializer::Adapter.get(:my_adapter), my_adapter
assert_equal ActiveModel::Serializer::Adapter.get(:my_subclassed_adapter), my_subclassed_adapter
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_adapter), my_adapter
assert_equal ActiveModel::Serializer::Adapter.lookup(:my_subclassed_adapter), my_subclassed_adapter
ensure
ActiveModel::Serializer::Adapter.adapter_map.delete('my_adapter'.freeze)
ActiveModel::Serializer::Adapter.adapter_map.delete('my_subclassed_adapter'.freeze)
Expand Down

0 comments on commit e096d22

Please sign in to comment.