Class: MockWS::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mockws/crud_manager.rb

Constant Summary collapse

TYPE_MAP =
{
    :string => :to_s,
    :integer => :to_i,
    :other => :to_s 
    
}

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



16
17
18
19
20
21
# File 'lib/mockws/crud_manager.rb', line 16

def initialize( )
    @models = {}
    @definitions = {}
    @keys = {}
    
end

Instance Method Details

#add_model(definition:) ⇒ Object



27
28
29
30
31
# File 'lib/mockws/crud_manager.rb', line 27

def add_model(definition:)
    @models[definition[:object]] = []
    @definitions[definition[:object]] = definition[:model]
    @keys[definition[:object]] = definition[:primary_key]
end

#create(model:, data:) ⇒ Object



37
38
39
40
41
42
# File 'lib/mockws/crud_manager.rb', line 37

def create(model:, data:)
    data[:id] = UUID.generate 
    status = check_record(model: model, data: data)
    @models[model].push data  if status[:status] == true
    return status
end

#delete_model(name:) ⇒ Object



33
34
35
# File 'lib/mockws/crud_manager.rb', line 33

def delete_model(name:)
    @models.delete(name)
end

#destroy(model:, key:) ⇒ Object



58
59
60
61
# File 'lib/mockws/crud_manager.rb', line 58

def destroy(model:, key:)
    res = @models[model].reject!{|value| value[@keys[model]] == key}
    return (res.nil?)? false : true 
end

#exist?(model:, key:) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mockws/crud_manager.rb', line 67

def exist?(model:,  key:)
    return @models[model].index{|value| value[@keys[model]] == key} == 1 
end

#list(model:) ⇒ Object



63
64
65
# File 'lib/mockws/crud_manager.rb', line 63

def list(model: )
    return @models[model]
end

#modelsObject



23
24
25
# File 'lib/mockws/crud_manager.rb', line 23

def models
    return @models.keys
end

#retrieve(model:, key:) ⇒ Object



45
46
47
# File 'lib/mockws/crud_manager.rb', line 45

def retrieve(model:, key:)
    return @models[model].select{|value| value[@keys[model]] == key}.first
end

#update(model:, key:, data:) ⇒ Object



50
51
52
53
54
55
# File 'lib/mockws/crud_manager.rb', line 50

def update(model:, key:, data:)
    record = @models[model].select{|value| value[@keys[model]] == key}.first
    status = check_record(model: model, data: data, update: true)
    record.merge! data if status[:status] == true
    return status
end