type_scopes 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20ec6bc0d92b4379d1c4b6aeb486004fc37d215f
4
- data.tar.gz: 615034c211e3e76a9c3dbb1ef99e4622accb8b23
3
+ metadata.gz: 26fb4bb8237b5e8afa0912bb38895c1afc4475d7
4
+ data.tar.gz: d331c4b23670597d42be35954bd0b2ca6a24b3b3
5
5
  SHA512:
6
- metadata.gz: 22d1c02c5d20db0e3c9bb60e15185994dd42ec973923ce1230f681a000af7f01e00022363ab1f207201540e889db495505dd5ea080cc02c68891a039dabc6678
7
- data.tar.gz: a2b9e392771c5bcf7d178b5dc45514bdb3f324fc0707491a68f54a3472dbb1544847fbc9351c5f05e882a5d18bbc3b47fcd422af30a1765bbf0139b22d99b435
6
+ metadata.gz: 23b399603b333ceb04df3ffefcb6ee65309e3b9e027dc39f24dca9908870d75924a36100e99b33a10e24e1fecd800594a5b999ba75ad2da3a28e9cf34071cc6a
7
+ data.tar.gz: 5bb76bf2695722732e17f973fce71cd44d747b599b955669781afbebc142548c0dc1cc74a95d126c12070c52300914a7c1f154e15f69ca932db401ce1d1f1409
data/README.md CHANGED
@@ -31,6 +31,16 @@ Transaction.amount_between(100, 200) # => where("amount BETWEEN 100 AND 200")
31
31
  Transaction.description_contains("foo") # => where("description LIKE '%foo%'")
32
32
  Transaction.description_starts_with("foo") # => where("description LIKE 'foo%'")
33
33
  Transaction.description_ends_with("foo") # => where("description LIKE '%foo'")
34
+
35
+ # Boolean scopes
36
+ Transaction.non_profit # => where("non_profit = true")
37
+ Transaction.not_non_profit # => where("non_profit = false")
38
+ Transaction.is_valid # => where("is_valid = true")
39
+ Transaction.is_not_valid # => where("is_valid = false")
40
+ Transaction.has_payment # => where("has_payment = true")
41
+ Transaction.has_not_payment # => where("has_payment = false")
42
+ Transaction.was_processed # => where("was_processed = true")
43
+ Transaction.was_not_processed # => where("was_processed = false")
34
44
  ```
35
45
 
36
46
  For the string scope the pattern matching is escaped:
@@ -0,0 +1,29 @@
1
+ module BooleanScopes
2
+ TYPES = ["bool", "boolean", "tinyint(1)"].freeze
3
+
4
+ def self.included(model)
5
+ model.extend(ClassMethods)
6
+ model.create_boolean_scopes
7
+ end
8
+
9
+ module ClassMethods
10
+ def create_boolean_scopes
11
+ for column in columns
12
+ if TYPES.include?(column.sql_type)
13
+ create_boolean_scopes_for_column(column.name)
14
+ end
15
+ end
16
+ end
17
+
18
+ def create_boolean_scopes_for_column(name)
19
+ scope :"#{name}", lambda { where(quoted_table_name => { name => true }) }
20
+
21
+ prefix, suffix = /\A(has|is|was)_(.+)\z/.match(name).to_a[1..2]
22
+ if prefix && suffix
23
+ scope :"#{prefix}_not_#{suffix}", lambda { where(quoted_table_name => { name => false }) }
24
+ else
25
+ scope :"not_#{name}", lambda { where(quoted_table_name => { name => false }) }
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/type_scopes.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  require "string_scopes"
2
2
  require "numeric_scopes"
3
3
  require "timestamp_scopes"
4
+ require "boolean_scopes"
4
5
 
5
6
  module TypeScopes
6
7
  def self.included(model)
7
8
  model.include(StringScopes)
8
9
  model.include(NumericScopes)
9
10
  model.include(TimestampScopes)
11
+ model.include(BooleanScopes)
10
12
  end
11
13
  end
@@ -1,3 +1,3 @@
1
1
  module TypeScopes
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Bernard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-13 00:00:00.000000000 Z
11
+ date: 2017-11-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Useful scopes based on columns' types (dates, times, strings and numerics).
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - README.md
21
+ - lib/boolean_scopes.rb
21
22
  - lib/numeric_scopes.rb
22
23
  - lib/string_scopes.rb
23
24
  - lib/timestamp_scopes.rb