Class: Husky::Repo::InMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/husky/repo.rb

Instance Method Summary collapse

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



54
55
56
57
# File 'lib/husky/repo.rb', line 54

def initialize
  @records = {}
  @id      = 1
end

Instance Method Details

#allObject



59
60
61
# File 'lib/husky/repo.rb', line 59

def all
  @records.map { |key, value| value }
end

#create(attributes = {}) ⇒ Object



84
85
86
# File 'lib/husky/repo.rb', line 84

def create(attributes = {})
  save(new(attributes))
end

#destroy(object) ⇒ Object



92
93
94
# File 'lib/husky/repo.rb', line 92

def destroy(object)
  @records.delete(object.id)
end

#find(id) ⇒ Object



67
68
69
# File 'lib/husky/repo.rb', line 67

def find(id)
  entity.build(get_record(id))
end

#get_record(id) ⇒ Object



71
72
73
# File 'lib/husky/repo.rb', line 71

def get_record(id)
  @records[id.to_i]
end

#new(attributes = {}) ⇒ Object



63
64
65
# File 'lib/husky/repo.rb', line 63

def new(attributes = {})
  entity.build(data_source.new(attributes))
end

#save(original_entity) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/husky/repo.rb', line 75

def save(original_entity)
  object = original_entity.data
  object.id = @id
  object.update_attributes({ id: @id })
  @records[@id] = object
  @id += 1
  entity.build(object)
end

#update(item, attributes) ⇒ Object



88
89
90
# File 'lib/husky/repo.rb', line 88

def update(item, attributes)
  entity.build(item.update_attributes(attributes))
end