Class: ReeDto::BuildDtoCollectionClass

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_dto/package/ree_dto/functions/build_dto_collection_class.rb

Instance Method Summary collapse

Instance Method Details

#call(klass, collection_name, entity_contract) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ree_lib/packages/ree_dto/package/ree_dto/functions/build_dto_collection_class.rb', line 10

def call(klass, collection_name, entity_contract)
  name = camelize(collection_name.to_s)

  const = Class.new(DtoCollection) do
    contract entity_contract => nil
    def add(item)
      @list ||= []
      @list.push(item)
      nil
    end

    contract entity_contract => Nilor[entity_contract]
    def remove(item)
      @list.delete(item)
    end

    alias :<< :add
    alias :push :add
    alias :remove :delete
  end

  const_name = "#{name}CollectionDto"
  klass.const_set(const_name, const)
  klass.const_get(const_name)
end