mongo_inspired_compare 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59bdc9bce2a74975ebba9c8d23d8e430e5d24cf7
4
+ data.tar.gz: a1d1f4d4914016fdbaca22d9f49f821864719355
5
+ SHA512:
6
+ metadata.gz: 20cb87fa8e2787634f189bbcb88eb217c248311e881fe4296a6137512be14afb910e144ca0d73a22346efac59058c69dc62fcaf35807d1f53830a74a7fb2c9cb
7
+ data.tar.gz: 555dadd69e083dbf6bbc49e140800d4a4fb1b5c2d6cced50377c535df5f1cdbf10e4e5e5dc09046e243772445c0a3a31c55eab476e1ec36d7f9eb9dd0682aad7
@@ -0,0 +1,48 @@
1
+ class MongoInspiredCompare
2
+ # mongo-inspired syntax for criteria
3
+ # if criterion is not hashes then simply tests for equality
4
+ # if criteron is a hash, then it considers it to be {operator: comparitor}
5
+ # it handles some smart conversion of "1.2" to 1.2 if the comparitor is a number
6
+
7
+ def self.compare( val, criterion )
8
+ if criterion.class != Hash
9
+ val == criterion
10
+ else
11
+ # v might look like { "$gte" => 12, "$lt" => 1 }
12
+ criterion.all? do |oper,v2|
13
+ if (v2.class == Float or v2.class == Integer or v2.class == Fixnum) and val.class == String
14
+ num_val = numerical(val)
15
+ else
16
+ num_val = val
17
+ end
18
+
19
+ if oper == "$in" # "$in" => [">4000", {"$gt" => -100 }]
20
+ v2.any? {|v3| compare( val, v3)}
21
+ elsif oper == "$nin" # "$in" => [">4000", {"$gt" => -100 }]
22
+ !v2.any? {|v3| compare( val, v3)}
23
+ elsif oper == "$or"
24
+ v2.any? {|v3| compare( val, v3)}
25
+ elsif oper == "$and"
26
+ v2.all? {|v3| compare( val, v3)}
27
+ elsif num_val == nil
28
+ false
29
+ elsif oper == "$gte"
30
+ num_val >= v2
31
+ elsif oper == "$gt"
32
+ num_val > v2
33
+ elsif oper == "$lte"
34
+ num_val <= v2
35
+ elsif oper == "$lt"
36
+ num_val < v2
37
+ else
38
+ raise "Error: unknown operator #{ oper }"
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+ def self.numerical( str )
46
+ (str and str.strip =~ /^[\-\d\.]+$/) ? str.to_f : nil
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ require "./lib/mongo_inspired_compare.rb"
2
+
3
+ def expect( val, criterion, to_return )
4
+
5
+ puts "> MongoInspiredCompare.compare( #{ val.inspect }, #{ criterion} )"
6
+ puts "=> #{ actually_returned = MongoInspiredCompare.compare( val, criterion) }\n\n"
7
+
8
+ puts "===================== expected #{ to_return }" unless to_return == actually_returned
9
+ end
10
+
11
+
12
+ puts "\n## Basic Comparisons"
13
+ expect( 5, 5, true)
14
+
15
+ expect( 5, 6, false)
16
+
17
+
18
+ puts "\n## Basic Operators"
19
+ expect( 5, {"$gte" => 5}, true)
20
+
21
+ expect( 5, {"$lt" => 5}, false)
22
+
23
+ expect( 5, {"$in" => [1, 2, 3, 4, 5]}, true)
24
+
25
+ expect( 5, {"$nin" => [1, 2, 3, 4, 5]}, false)
26
+
27
+ puts "\n## Nesting"
28
+
29
+ expect( 5, {"$in" => [{"$lt" => 5}, {"$gte" => 5}]}, true)
30
+
31
+ puts "\n## And/or Operators"
32
+
33
+ expect( 5, {"$or" => [{"$lt" => 5}, {"$gte" => 5}]}, true)
34
+
35
+ expect( 5, {"$and" => [{"$lt" => 5}, {"$gte" => 5}]}, false)
36
+
37
+
38
+ puts "\n## Invariance"
39
+ expect( "5", {"$gte" => 5}, true)
40
+
41
+ expect( "5x", {"$gte" => 5}, false)
42
+
43
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_inspired_compare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Do
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple library for comparing values against criteria written in a Mongo-inspired
14
+ syntax
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/mongo_inspired_compare.rb
21
+ - test_mongo_inspired_compare.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.11
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: When creating a rules engine for dealing with large amounts of numeric data
46
+ (like clinical data e.g. laboratory and flowsheet values), it can advantageous to
47
+ abstract away the rules rather than to hard-code them. Mongo queries offer a clear,
48
+ compact syntax that can encode complex rules and be stored as JSON. This gem offers
49
+ a simple way to compare values against criteria written in a Mongo-inspired syntax.
50
+ test_files: []