Sha256: 0b630468bc5fefc12b44111956721600075c6b6eb026b579d9df932af334e979
Contents?: true
Size: 1.46 KB
Versions: 9
Compression:
Stored size: 1.46 KB
Contents
module Shamu module Entities # A list of {Entities::Entity} records. class List include Enumerable # @param [Enumerable] entities the raw list of entities. def initialize( entities ) fail ArgumentError, "missing entities" if entities.nil? @raw_entities = entities end # Enumerate through each of the entities in the list. def each( &block ) entities.each( &block ) end delegate :first, :count, :empty?, to: :raw_entities alias_method :size, :count alias_method :length, :count # Get an entity by it's primary key. # @param [Object] key the primary key to look for. # @param [Symbol] field to use as the primary key. Default :id. # @return [Entities::Entity] the found entity. # @raise [Shamu::NotFoundError] if the entity cannot be found. def get( key, field: key_attribute ) entity = if field == :id entities.find { |e| e.id == key } else entities.find { |e| e.send( field ) == key } end entity || fail( Shamu::NotFoundError ) end private attr_reader :raw_entities def entities # Array and others do not implement #lazy so allow anything that # doesn't support lazy to just enumerate directly. @entities ||= raw_entities.lazy || raw_entities end def key_attribute :id end end end end
Version data entries
9 entries across 9 versions & 1 rubygems