lib/shamu/services/service.rb in shamu-0.0.9 vs lib/shamu/services/service.rb in shamu-0.0.11
- old
+ new
@@ -76,33 +76,42 @@
initialize do
end
private
+ # Maps a single record to an entity. Requires a `build_entities` method
+ # that maps an enumerable set of records to entities.
+ #
+ # @param [Object] record to map.
+ # @return [Entity] the mapped entity.
+ def build_entity( record )
+ mapped = build_entities( [ record ] )
+ mapped && mapped.first
+ end
+
# @!visibility public
# Takes a raw enumerable list of records and transforms them to a proper
# {Entities::List}.
#
# As simple as the method is, it also serves as a hook for mixins to add
# additional behavior when processing lists.
#
- # If a block is not provided, looks for a method `build_entity(record,
- # records=nil)` where `record` is the individual record to be
- # transformed and `records` is the original collection or database query
- # being transformed.
+ # If a block is not provided, looks for a method `build_entities(
+ # records )` that maps a set of records to their corresponding
+ # entities.
#
# @param [Enumerable] records the raw list of records.
# @yield (record)
# @yieldparam [Object] record the raw value from the `list` to to
# transform to an {Entities::Entity}.
# @yieldreturn [Entities::Entity]
# @return [Entities::List]
def entity_list( records, &transformer )
return Entities::List.new [] unless records
unless transformer
- fail "Either provide a block or add a private method `def build_entity( record, records = nil )` to #{ self.class.name }." unless respond_to?( :build_entity, true ) # rubocop:disable Metrics/LineLength
- transformer ||= ->( record ) { build_entity( record, records ) }
+ fail "Either provide a block or add a private method `def build_entities( records )` to #{ self.class.name }." unless respond_to?( :build_entities, true ) # rubocop:disable Metrics/LineLength
+ transformer ||= method( :build_entities )
end
Entities::List.new LazyTransform.new( records, &transformer )
end
@@ -323,11 +332,11 @@
end
end
# @!visibility public
#
- # @overload result( *validation_sources, request: nil, entity: nil )
+ # @overload result( *values, request: nil, entity: nil )
# @param (see Result#initialize)
# @return [Result]
def result( *args )
Result.new( *args )
end
@@ -352,6 +361,6 @@
when String then ToModelIdExtension::Strings::NUMERIC_PATTERN =~ value
end
end
end
end
-end
\ No newline at end of file
+end