Class: Appfuel::Domain::EntityCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/appfuel/domain/entity_collection.rb

Overview

Currently this only answers the use case where a collection of active record models are converted into a collection of domain entities via a entity loader.

NOTE: There is no ability yet to add or track the entity state

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_name, entity_loader = nil) ⇒ EntityCollection

Returns a new instance of EntityCollection.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/appfuel/domain/entity_collection.rb', line 13

def initialize(domain_name, entity_loader = nil)
  unless Types.key?(domain_name)
    fail "#{domain_name} is not a registered type"
  end

  @pager  = nil
  @list   = []
  @loaded = false

  parts = domain_name.split('.')
  @domain_name     = domain_name
  @domain_basename = parts.last
  @is_global       = parts.size == 1

  self.entity_loader =  entity_loader if entity_loader
end

Instance Attribute Details

#domain_basenameObject (readonly)

Returns the value of attribute domain_basename.



11
12
13
# File 'lib/appfuel/domain/entity_collection.rb', line 11

def domain_basename
  @domain_basename
end

#domain_nameObject (readonly)

Returns the value of attribute domain_name.



11
12
13
# File 'lib/appfuel/domain/entity_collection.rb', line 11

def domain_name
  @domain_name
end

#entity_loaderObject

Returns the value of attribute entity_loader.



11
12
13
# File 'lib/appfuel/domain/entity_collection.rb', line 11

def entity_loader
  @entity_loader
end

Instance Method Details

#allObject



38
39
40
41
# File 'lib/appfuel/domain/entity_collection.rb', line 38

def all
  load_entities
  @list
end

#collection?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/appfuel/domain/entity_collection.rb', line 30

def collection?
  true
end

#eachObject



48
49
50
51
52
53
# File 'lib/appfuel/domain/entity_collection.rb', line 48

def each
  load_entities
  return @list.each unless block_given?

  @list.each {|entity| yield entity}
end

#entity_loader?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/appfuel/domain/entity_collection.rb', line 68

def entity_loader?
  !@entity_loader.nil?
end

#firstObject



43
44
45
46
# File 'lib/appfuel/domain/entity_collection.rb', line 43

def first
  load_entities
  @list.first
end

#global?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/appfuel/domain/entity_collection.rb', line 34

def global?
  @is_global
end

#pagerObject



55
56
57
58
# File 'lib/appfuel/domain/entity_collection.rb', line 55

def pager
  load_entities
  @pager
end

#to_aObject



60
61
62
63
64
65
66
# File 'lib/appfuel/domain/entity_collection.rb', line 60

def to_a
  list = []
  each do |entity|
    list << entity.to_h
  end
  list
end