diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-11-19 11:54:19 -0800 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-11-19 12:11:26 -0800 |
commit | cafed35b610d830049b7dffd924cbcdeedab72bb (patch) | |
tree | ab617d9d93f2e3e8e32fd1ed2a4861cdb58b837d /activerecord/lib | |
parent | e0c938759fdb2f82057aa46a8410533a60424114 (diff) | |
download | rails-cafed35b610d830049b7dffd924cbcdeedab72bb.tar.gz rails-cafed35b610d830049b7dffd924cbcdeedab72bb.tar.bz2 rails-cafed35b610d830049b7dffd924cbcdeedab72bb.zip |
Add tests for `TypeMap#fetch` and push up to `TypeMap`
It doesn't make sense for the subclass to implement this method, and not
have it on the parent. We can also DRY up the implementation of
`#lookup` to be defined in terms of fetch, which will give us a single
point of entry
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/type/hash_lookup_type_map.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/type/type_map.rb | 6 |
2 files changed, 5 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/type/hash_lookup_type_map.rb b/activerecord/lib/active_record/type/hash_lookup_type_map.rb index bf92680268..90a8c674cb 100644 --- a/activerecord/lib/active_record/type/hash_lookup_type_map.rb +++ b/activerecord/lib/active_record/type/hash_lookup_type_map.rb @@ -3,10 +3,6 @@ module ActiveRecord class HashLookupTypeMap < TypeMap # :nodoc: delegate :key?, to: :@mapping - def lookup(type, *args) - @mapping.fetch(type, proc { default_value }).call(type, *args) - end - def fetch(type, *args, &block) @mapping.fetch(type, block).call(type, *args) end diff --git a/activerecord/lib/active_record/type/type_map.rb b/activerecord/lib/active_record/type/type_map.rb index 88c5f9c497..13d4943861 100644 --- a/activerecord/lib/active_record/type/type_map.rb +++ b/activerecord/lib/active_record/type/type_map.rb @@ -6,6 +6,10 @@ module ActiveRecord end def lookup(lookup_key, *args) + fetch(lookup_key, *args) { default_value } + end + + def fetch(lookup_key, *args) matching_pair = @mapping.reverse_each.detect do |key, _| key === lookup_key end @@ -13,7 +17,7 @@ module ActiveRecord if matching_pair matching_pair.last.call(lookup_key, *args) else - default_value + yield lookup_key, *args end end |