Class: Ant::Server::Nanoservice::Datasource::Repository
- Inherits:
-
Object
- Object
- Ant::Server::Nanoservice::Datasource::Repository
- Includes:
- Exceptions
- Defined in:
- lib/ant/server/nanoservice/datasource/repository.rb
Direct Known Subclasses
Instance Method Summary collapse
- #create(id = nil) ⇒ Object
- #create_initial_object(id) ⇒ Object
- #exist?(id) ⇒ Boolean
-
#initialize(id, id_generator) ⇒ Repository
constructor
A new instance of Repository.
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
27 28 29 30 31 |
# File 'lib/ant/server/nanoservice/datasource/repository.rb', line 27 def exist?(id) get(id) rescue ObjectNotFound nil end |