memorydb 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/memorydb/base.rb +7 -3
  2. data/lib/memorydb/db.rb +33 -21
  3. metadata +2 -2
data/lib/memorydb/base.rb CHANGED
@@ -40,9 +40,13 @@ module MemoryDb
40
40
  def model_or_hash_as_attrs(model_or_hash)
41
41
  if model_or_hash
42
42
  if model_or_hash.is_a?(Hash)
43
- verify_attributes!(model_or_hash)
44
- model_klass.new(model_or_hash).attributes
45
- elsif model_or_hash.is_a?(model_klass)
43
+ if model_klass
44
+ verify_attributes!(model_or_hash)
45
+ model_klass.new(model_or_hash).attributes
46
+ else
47
+ model_or_hash
48
+ end
49
+ elsif model_klass && model_or_hash.is_a?(model_klass)
46
50
  model_or_hash.attributes
47
51
  else
48
52
  raise ArgumentError.new("A hash or a #{model_klass} must be given to create a record")
data/lib/memorydb/db.rb CHANGED
@@ -37,29 +37,37 @@ module MemoryDb
37
37
  attrs = model_or_hash_as_attrs(given)
38
38
  attributes = attrs.merge(primary_key => _id)
39
39
  store!(_id, attributes)
40
- model_klass.new(attributes)
40
+ return_model_or_hash(attributes)
41
+ end
42
+
43
+ def return_model_or_hash(attrs)
44
+ if model_klass
45
+ model_klass.new(attrs)
46
+ else
47
+ attrs
48
+ end
41
49
  end
42
50
 
43
51
  def update!(model_or_id, attributes={})
44
52
  attributes ||= {}
45
- model = case
46
- when model_or_id.is_a?(model_klass)
47
- if model = find_by_id(model_or_id.send(primary_key))
48
- model_or_id
49
- else
50
- raise ArgumentError.new("Could not update record with id: #{model_or_id.send(primary_key)} because it does not exist") unless model
51
- end
52
-
53
- else
54
- if model = find_by_id(model_or_id)
55
- model
56
- else
57
- raise ArgumentError.new("Could not update record with id: #{model_or_id} because it does not exist") unless model
58
- end
59
- end
60
- updated_attrs = model.attributes.merge(attributes_without_pkey(attributes))
61
- store!(model.send(primary_key), updated_attrs)
62
- model_klass.new(updated_attrs)
53
+ model_or_hash = case
54
+ when model_klass && model_or_id.is_a?(model_klass)
55
+ if model = find_by_id(model_or_id.send(primary_key))
56
+ model_or_id
57
+ else
58
+ raise ArgumentError.new("Could not update record with id: #{model_or_id.send(primary_key)} because it does not exist") unless model
59
+ end
60
+
61
+ else
62
+ if model = find_by_id(model_or_id)
63
+ model
64
+ else
65
+ raise ArgumentError.new("Could not update record with id: #{model_or_id} because it does not exist") unless model
66
+ end
67
+ end
68
+ updated_attrs = model_or_hash_as_attrs(model_or_hash).merge(attributes_without_pkey(attributes))
69
+ store!(model_or_hash[primary_key], updated_attrs)
70
+ return_model_or_hash(updated_attrs)
63
71
  end
64
72
 
65
73
  def find_by_id(id)
@@ -80,7 +88,11 @@ module MemoryDb
80
88
 
81
89
  def execute_find(query)
82
90
  models = raw_find(query)
83
- models.map { |h| model_klass.new(h) }
91
+ if model_klass
92
+ models.map { |h| model_klass.new(h) }
93
+ else
94
+ models
95
+ end
84
96
  end
85
97
 
86
98
  def execute_count(query)
@@ -195,7 +207,7 @@ module MemoryDb
195
207
  end
196
208
 
197
209
  def remove_model!(model)
198
- @store.delete(model.send(primary_key))
210
+ @store.delete(model[primary_key])
199
211
  nil
200
212
  end
201
213
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memorydb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-01 00:00:00.000000000 Z
12
+ date: 2013-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake