memorydb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,6 +61,16 @@ module MemoryDb
61
61
  self
62
62
  end
63
63
 
64
+ def and(*filters)
65
+ @filters << filter_factory.and(*filters)
66
+ self
67
+ end
68
+
69
+ def not(filter)
70
+ @filters << filter_factory.not(filter)
71
+ self
72
+ end
73
+
64
74
  def sort(field, order)
65
75
  assert_field!(field)
66
76
  assert_order!(order)
data/lib/memorydb/db.rb CHANGED
@@ -131,8 +131,8 @@ module MemoryDb
131
131
  end
132
132
  end
133
133
 
134
- def filter_matches?(filter, model)
135
- value = model[filter.field]
134
+ def filter_matches?(filter, hash)
135
+ value = filter.field ? hash.fetch(filter.field) : nil
136
136
  case filter.operator
137
137
  when '='; value == filter.value
138
138
  when '!='; value != filter.value
@@ -143,8 +143,12 @@ module MemoryDb
143
143
  when 'in'; filter.value.include?(value)
144
144
  when '!in'; !filter.value.include?(value)
145
145
  when 'or'; filter.value.any? do |sub_filter|
146
- filter_matches?(sub_filter, model)
146
+ filter_matches?(sub_filter, hash)
147
147
  end
148
+ when 'and'; filter.value.all? do |sub_filter|
149
+ filter_matches?(sub_filter, hash)
150
+ end
151
+ when 'not'; !filter_matches?(filter.value, hash)
148
152
  when 'like'; value =~ /#{filter.value}/i
149
153
  end
150
154
  end
@@ -162,7 +166,7 @@ module MemoryDb
162
166
  end
163
167
 
164
168
  def compare_model(model1, model2, sort)
165
- field1, field2 = model1[sort.field], model2[sort.field]
169
+ field1, field2 = model1.fetch(sort.field), model2.fetch(sort.field)
166
170
  field1 == field2 ? nil :
167
171
  field1 < field2 && sort.order == :asc ? -1 :
168
172
  field1 > field2 && sort.order == :desc ? -1 : 1
@@ -59,6 +59,14 @@ module MemoryDb
59
59
  Filter.new(nil, 'or', filters)
60
60
  end
61
61
 
62
+ def and(*filters)
63
+ Filter.new(nil, 'and', filters)
64
+ end
65
+
66
+ def not(filter)
67
+ Filter.new(nil, 'not', filter)
68
+ end
69
+
62
70
  private
63
71
 
64
72
  def assert_to_a!(name, value)
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.1
4
+ version: 0.0.2
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-06-19 00:00:00.000000000 Z
12
+ date: 2013-06-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json