date_as_bool 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +74 -0
- data/Rakefile +33 -0
- data/lib/date_as_bool/active_record/as/active_record.rb +3 -0
- data/lib/date_as_bool/active_record/as/bool.rb +34 -0
- data/lib/date_as_bool/version.rb +3 -0
- data/lib/date_as_bool.rb +7 -0
- data/lib/tasks/date_as_bool_tasks.rake +4 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e2b4fb4fb7506c04047aa82aebe688e26505d5ab
|
4
|
+
data.tar.gz: a3473d477d4a5a47d625f674fb0f3f4fb1c0778f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6a837110cc0f3b01f8ab4cb2161b2b663f99dfe212b1c3a0bdb37bf43d99b893eac1c829713fe549123a39c10000671ab501279bd7729302a390be1bde696f8
|
7
|
+
data.tar.gz: 1d3584c5e18dcbd8b2c63ccb34f4ebec6730386b4cad4d02a79104b0f3486244cf2f7622d5af76863411f98d34cc02d244eb2119a2f1059b60876023c7186027
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Carlo Martinucci
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# DateAsBool
|
2
|
+
A simple Rails Gem to get both a datetime and a boolean behaviour from the same database column. I found myself quite often with the following design pattern:
|
3
|
+
|
4
|
+
```ruby
|
5
|
+
# == Schema Information
|
6
|
+
#
|
7
|
+
# Table name: subscriptions
|
8
|
+
#
|
9
|
+
# id :integer not null, primary key
|
10
|
+
# paid_at :datetime
|
11
|
+
# ...
|
12
|
+
#
|
13
|
+
|
14
|
+
class Subscription < ApplicationRecord
|
15
|
+
attr_accessor :paid
|
16
|
+
def paid=(bool)
|
17
|
+
self.paid_at = bool ? Time.now : nil
|
18
|
+
end
|
19
|
+
def paid
|
20
|
+
paid_at.present?
|
21
|
+
end
|
22
|
+
def paid!
|
23
|
+
paid = true
|
24
|
+
save
|
25
|
+
end
|
26
|
+
def paid?
|
27
|
+
paid_at.present?
|
28
|
+
end
|
29
|
+
...
|
30
|
+
```
|
31
|
+
|
32
|
+
This gem abstracts this behaviour in one line.
|
33
|
+
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
Let's say you have a `users` table with column `verified_at:datetime`.
|
37
|
+
|
38
|
+
Add
|
39
|
+
```ruby
|
40
|
+
class User < ApplicationRecord
|
41
|
+
date_as_bool :verified_at
|
42
|
+
...
|
43
|
+
```
|
44
|
+
|
45
|
+
Now you can use `user.verified?` to test for `verified_at` presence, and `user.verified!` as an alias for `user.update(verified_at: Time.now)`.
|
46
|
+
|
47
|
+
You can also reset the field or change it dynamically with `user.update(verified: bool)`. If false, it resets `verified_at` to `nil`.
|
48
|
+
|
49
|
+
|
50
|
+
## Advanced Usage
|
51
|
+
If you want to give a different name to the boolean method, you can specify it as the second argument: `date_as_bool :verified_at` gets translated to `date_as_bool :verified_at, :verified`.
|
52
|
+
|
53
|
+
## Installation
|
54
|
+
Add this line to your application's Gemfile:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
gem 'date_as_bool'
|
58
|
+
```
|
59
|
+
|
60
|
+
And then execute:
|
61
|
+
```bash
|
62
|
+
$ bundle
|
63
|
+
```
|
64
|
+
|
65
|
+
Or install it yourself as:
|
66
|
+
```bash
|
67
|
+
$ gem install date_as_bool
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
Pull requests are greatly appreciated. This is my first gem, intended mainly for learning purposes. Its only target is to capture a small design pattern which may be safer to implement just by copy&paste.
|
72
|
+
|
73
|
+
## License
|
74
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'DateAsBool'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task default: :test
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module As #:nodoc:
|
3
|
+
module Bool #:nodoc:
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def date_as_bool datetime_name, bool_name=nil
|
7
|
+
caller_class = self
|
8
|
+
|
9
|
+
if bool_name.nil?
|
10
|
+
bool_name = datetime_name.to_s.split("_")[0..-2].join("_").to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
caller_class.class_eval do
|
14
|
+
attr_accessor bool_name
|
15
|
+
define_method bool_name do
|
16
|
+
self.send(datetime_name).present?
|
17
|
+
end
|
18
|
+
define_method :"#{bool_name}=" do |bool|
|
19
|
+
self.send(:"#{datetime_name}=", bool ? Time.now : nil)
|
20
|
+
end
|
21
|
+
define_method :"#{bool_name}!" do
|
22
|
+
self.send(:"#{bool_name}=", true)
|
23
|
+
self.save!
|
24
|
+
end
|
25
|
+
define_method :"#{bool_name}?" do
|
26
|
+
self.send(bool_name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/date_as_bool.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_as_bool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carlo Martinucci
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.1.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.1.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A simple Rails Gem to get both a datetime and a boolean behaviour from
|
42
|
+
the same database column, i.e. have a verified_at:datetime to log times of verification
|
43
|
+
and sort, and also .verified? and .verify! methods.
|
44
|
+
email:
|
45
|
+
- carlo.martinucci@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- MIT-LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/date_as_bool.rb
|
54
|
+
- lib/date_as_bool/active_record/as/active_record.rb
|
55
|
+
- lib/date_as_bool/active_record/as/bool.rb
|
56
|
+
- lib/date_as_bool/version.rb
|
57
|
+
- lib/tasks/date_as_bool_tasks.rake
|
58
|
+
homepage: https://carlomartinucci.github.io/date_as_bool
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.6.8
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Get both a datetime and a boolean behaviour with only one column.
|
82
|
+
test_files: []
|