Class: MockApi::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mock_api/store.rb

Defined Under Namespace

Classes: Repository

Instance Method Summary collapse

Constructor Details

#initialize(*entity_types) ⇒ Store

Returns a new instance of Store.



3
4
5
6
7
8
9
# File 'lib/mock_api/store.rb', line 3

def initialize(*entity_types)
  @store = {}
  @entity_types = entity_types
  entity_types.each do |entity_type|
    @store[entity_type] = Repository.new([])
  end
end

Instance Method Details

#[](entity_type) ⇒ Object



15
16
17
# File 'lib/mock_api/store.rb', line 15

def [](entity_type)
  @store[entity_type]
end

#add(entity_type, entity) ⇒ Object



11
12
13
# File 'lib/mock_api/store.rb', line 11

def add(entity_type, entity)
  @store[entity_type].add(entity)
end

#mixinObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mock_api/store.rb', line 23

def mixin
  store = @store
  entity_types = @entity_types
  Module.new do
    define_singleton_method :included do |klass|
      entity_types.each do |entity_type|
        klass.define_method(entity_type) { store[entity_type] }
      end
    end
    define_singleton_method :extended do |klass|
      entity_types.each do |entity_type|
        klass.define_singleton_method(entity_type) { store[entity_type] }
      end
    end
  end
end

#resetObject



19
20
21
# File 'lib/mock_api/store.rb', line 19

def reset
  @store.transform_values! { Repository.new([]) }
end