Class: Husky::Repo::InMemory
- Inherits:
-
Object
- Object
- Husky::Repo::InMemory
- Defined in:
- lib/husky/repo.rb
Instance Method Summary collapse
- #all ⇒ Object
- #create(attributes = {}) ⇒ Object
- #destroy(object) ⇒ Object
- #find(id) ⇒ Object
- #get_record(id) ⇒ Object
-
#initialize ⇒ InMemory
constructor
A new instance of InMemory.
- #new(attributes = {}) ⇒ Object
- #save(original_entity) ⇒ Object
- #update(item, attributes) ⇒ Object
Constructor Details
#initialize ⇒ InMemory
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
#all ⇒ Object
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 |