Class: Shamu::Entities::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/shamu/entities/list.rb

Overview

A list of Entity records.

Direct Known Subclasses

PagedList

Instance Method Summary collapse

Constructor Details

#initialize(entities) ⇒ List

Returns a new instance of List.

Parameters:

  • entities (Enumerable)

    the raw list of entities.



9
10
11
12
# File 'lib/shamu/entities/list.rb', line 9

def initialize( entities )
  fail ArgumentError, "missing entities" if entities.nil?
  @raw_entities = entities
end

Instance Method Details

#each(&block) ⇒ Object

Enumerate through each of the entities in the list.



15
16
17
# File 'lib/shamu/entities/list.rb', line 15

def each( &block )
  entities.each( &block )
end

#get(key, field: key_attribute) ⇒ Entities::Entity

Get an entity by it's primary key.

Parameters:

  • key (Object)

    the primary key to look for.

  • field (Symbol) (defaults to: key_attribute)

    to use as the primary key. Default :id.

Returns:

Raises:



35
36
37
38
39
40
41
42
43
# File 'lib/shamu/entities/list.rb', line 35

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

#paged?Boolean

See PagedList for paged implementation.

Returns:

  • (Boolean)

    true if the list represents a slice of a larger set.



26
27
28
# File 'lib/shamu/entities/list.rb', line 26

def paged?
  false
end