lib/shamu/services/service.rb in shamu-0.0.8 vs lib/shamu/services/service.rb in shamu-0.0.9

- old
+ new

@@ -114,13 +114,13 @@ # @param [Array<Integer>] ids of records found. # @param [Class] null_class to use when an id doesn't have a matching # record. # @param [Symbol,#call(record)] match the attribute or a Proc used to # extract the id used to compare records. - # @param [Symbol,#call] coerce a method that can be used to coerce id values - # to the same type (eg :to_i). If not set, automatically uses :to_i - # if match is an 'id' attribute. + # @param [Symbol,#call] coerce a method that can be used to coerce id + # values to the same type (eg :to_i). If not set, automatically uses + # :to_model_id if match is an 'id' attribute. # @yield (see #entity_list) # @yieldparam (see #entity_list) # @yieldreturn (see #entity_list) # @return [Entities::List] # @@ -151,11 +151,11 @@ # # def entity_lookup_list( records, ids, null_class, match: :id, coerce: :not_set, &transformer ) matcher = entity_lookup_list_matcher( match ) coerce = coerce_method( coerce, match ) - ids = ids.map( &coerce ) if coerce + ids = ids.map( &coerce ) if coerce list = entity_list records, &transformer matched = ids.map do |id| list.find { |e| matcher.call( e ) == id } || scorpion.fetch( null_class, { id: id }, {} ) end @@ -173,11 +173,11 @@ end end def coerce_method( coerce, match ) return coerce unless coerce == :not_set - :to_i if match.is_a?( Symbol ) && match =~ /(^|_)id$/ + :to_model_id if match.is_a?( Symbol ) && match =~ /(^|_)ids?$/ end # @!visibility public # # For services that expose a standard `lookup` method, find_by_lookup @@ -295,9 +295,11 @@ # cached_lookup( ids ) do |missing_ids| # entity_lookup_list( Models::User.where( id: missing_ids ), missing_ids, UserEntity::Missing ) # end # end def cached_lookup( ids, match: :id, coerce: :not_set, entity: nil, &lookup ) + coerce = coerce_method( coerce, match ) + ids = ids.map( &coerce ) if coerce cache = cache_for( key: match, coerce: coerce, entity: entity ) missing_ids = cache.uncached_keys( ids ) cache_entities( cache, match, missing_ids, &lookup ) if missing_ids.any? \ No newline at end of file