Class: Kybus::Storage::Datasource::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/kybus/storage/datasource/model.rb

Overview

Wraps an object inside a model, this will allow to validate that values are inside the restrictions. It dependes on a repository for storing values.

Direct Known Subclasses

EmptyModel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Model

Returns a new instance of Model.



40
41
42
# File 'lib/kybus/storage/datasource/model.rb', line 40

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/kybus/storage/datasource/model.rb', line 13

def data
  @data
end

#repository=(value) ⇒ Object (writeonly)

Sets the attribute repository

Parameters:

  • value

    the value to set the attribute repository to.



14
15
16
# File 'lib/kybus/storage/datasource/model.rb', line 14

def repository=(value)
  @repository = value
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/kybus/storage/datasource/model.rb', line 26

def [](key)
  @data[key]
end

#[]=(key, val) ⇒ Object

:nocov: #



22
23
24
# File 'lib/kybus/storage/datasource/model.rb', line 22

def []=(key, val)
  @data[key] = val
end

#createObject



35
36
37
38
# File 'lib/kybus/storage/datasource/model.rb', line 35

def create
  run_validations!
  @repository.create(@data)
end

#run_validations!Object

:nocov: #



17
18
19
# File 'lib/kybus/storage/datasource/model.rb', line 17

def run_validations!
  puts 'WARN: model with no validations'
end

#storeObject



30
31
32
33
# File 'lib/kybus/storage/datasource/model.rb', line 30

def store
  run_validations!
  @repository.store(@data)
end

#to_json(options = nil) ⇒ Object



44
45
46
# File 'lib/kybus/storage/datasource/model.rb', line 44

def to_json(options = nil)
  @data.to_json(options)
end