Class: Ant::Server::Nanoservice::Datasource::Repository

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/ant/server/nanoservice/datasource/repository.rb

Direct Known Subclasses

JSONRepository, Sequel

Instance Method Summary collapse

Constructor Details

#initialize(id, id_generator) ⇒ Repository

Returns a new instance of Repository.



9
10
11
12
# File 'lib/ant/server/nanoservice/datasource/repository.rb', line 9

def initialize(id, id_generator)
  @id = id
  @id_generator = id_generator
end

Instance Method Details

#create(id = nil) ⇒ Object



20
21
22
23
24
25
# File 'lib/ant/server/nanoservice/datasource/repository.rb', line 20

def create(id = nil)
  data = create_initial_object(id)
  existent = exist?(data[@id])
  raise(ObjectAlreadyExists.new(data[@id], existent)) if existent
  create_(data)
end

#create_initial_object(id) ⇒ Object



14
15
16
17
18
# File 'lib/ant/server/nanoservice/datasource/repository.rb', line 14

def create_initial_object(id)
  object = { @id => @id_generator.call(id, @id) }
  object.merge!(id) if id.is_a?(Hash)
  object
end

#exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/ant/server/nanoservice/datasource/repository.rb', line 27

def exist?(id)
  get(id)
rescue ObjectNotFound
  nil
end