time_for_a_boolean 0.0.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 277793114b162bb1a4753e9400c14ae68708e217
4
- data.tar.gz: 5eae5941a38459c5a383dd8eb4561a8d839eb7e2
2
+ SHA256:
3
+ metadata.gz: af337b26f0dc5ce70747c5146b92e0a115d987ed835cd3131e42aca5611bb58c
4
+ data.tar.gz: 59329bac85be78bad931c35fd629278d5676419f2ae8aeaba13c1e7312f55695
5
5
  SHA512:
6
- metadata.gz: d084a3437da16608c2539cd5b8d9f11e6b0e5f1d0956c6d77c84aa15ee3f7cfde0bb3934ebf3d96ea3d70cd072e6deef1ffb781b225a2b29e3a668a575f77228
7
- data.tar.gz: dfdc83188a96c03337bd9dbcef49700c2e15772703dfdbdb61087d193e59889965768fb184c217dd041a029248b309bc03ad1c062d2c8c45bb53e8b5989dbebb
6
+ metadata.gz: 4a78da81585e4c74ec3a3560308020390b3a089726d9461796a9a9b0481f38f9f5de88710e276a9a08d241059942542e8bfa0630d317e8a55be18ef1a800d2eb
7
+ data.tar.gz: e9ae065b1573973fc993ec6a918a770261383baa36b41cd0b09dbc77cab9cf40e917bd3c2fffb032c58246585e109e22f379997826d1457150e6901b502164b3
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  Time for A Boolean
2
2
  ==================
3
3
 
4
- [![Build Status](https://travis-ci.org/calebthompson/time_for_a_boolean.png)](https://travis-ci.org/calebthompson/time_for_a_boolean)
5
- [![Code Climate](https://codeclimate.com/github/calebthompson/time_for_a_boolean.png)](https://codeclimate.com/github/calebthompson/time_for_a_boolean)
6
- [![Coverage Status](https://coveralls.io/repos/calebthompson/time_for_a_boolean/badge.png)](https://coveralls.io/r/calebthompson/time_for_a_boolean)
4
+ [![Build Status](https://api.travis-ci.org/calebthompson/time_for_a_boolean.svg?branch=master)](https://travis-ci.org/calebthompson/time_for_a_boolean)
5
+ [![Code Climate](https://codeclimate.com/github/calebthompson/time_for_a_boolean.svg)](https://codeclimate.com/github/calebthompson/time_for_a_boolean)
6
+ [![Coverage Status](https://coveralls.io/repos/calebthompson/time_for_a_boolean/badge.svg)](https://coveralls.io/r/calebthompson/time_for_a_boolean)
7
7
 
8
8
 
9
9
  > Sally: Hey, we need to add a flag to Post
@@ -63,9 +63,9 @@ methods are defined:
63
63
 
64
64
  | Method | Description
65
65
  | --------------- | -----------
66
- | `Post#deleted` | `true` if `Post#deleted_at` is set, `false` otherwise
66
+ | `Post#deleted` | `true` if `Post#deleted_at` is set to a time before `Time.current`, `false` otherwise
67
67
  | `Post#deleted?` | Alias for `Post#deleted`
68
- | `Post#deleted=` | Sets the timestamp to `DateTime.now` if the new value is true, and `nil` otherwise
68
+ | `Post#deleted=` | Sets the timestamp to `Time.current` if the new value is true, and `nil` otherwise
69
69
 
70
70
  These methods allow you to use a timestamp as you would a boolean value in your
71
71
  application.
@@ -77,3 +77,17 @@ Okay... why?
77
77
  * `COUNT(posts.deleted_at)` gives you the count of deleted posts, which is
78
78
  useful when writing a report. Define and use `Post.deleted.count` when you
79
79
  have Ruby available.
80
+
81
+ Other Options
82
+ -------------
83
+
84
+ If you have a date or time column that does not follow the `attribute_at` convention,
85
+ you can specify the attribute name:
86
+
87
+ ```
88
+ class User < ActiveRecord::Base
89
+ time_for_a_boolean :expires, :expires_on
90
+ end
91
+ ```
92
+
93
+ This is especially useful when using date only columns.
@@ -1,20 +1,28 @@
1
- require 'active_record/connection_adapters/column'
2
- require 'time_for_a_boolean/version'
3
- require 'time_for_a_boolean/railtie'
1
+ require "active_support/core_ext/module/delegation"
2
+ require "active_support/core_ext/time/calculations"
3
+ require "active_model/type"
4
+ require "time_for_a_boolean/version"
5
+ require "time_for_a_boolean/railtie"
4
6
 
5
7
  module TimeForABoolean
6
- def time_for_a_boolean(attribute)
8
+ def time_for_a_boolean(attribute, field=:"#{attribute}_at")
7
9
  define_method(attribute) do
8
- !send("#{attribute}_at").nil? &&
9
- send("#{attribute}_at") <= -> { DateTime.current }.()
10
+ !send(field).nil? && send(field) <= -> { Time.current }.()
10
11
  end
11
- alias_method "#{attribute}?", attribute
12
- define_method("#{attribute}=") do |value|
13
- if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
14
- send("#{attribute}_at=", -> { DateTime.current }.())
12
+
13
+ alias_method :"#{attribute}?", attribute
14
+
15
+ setter_attribute = :"#{field}="
16
+ define_method(:"#{attribute}=") do |value|
17
+ if ActiveModel::Type::Boolean::FALSE_VALUES.include?(value)
18
+ send(setter_attribute, nil)
15
19
  else
16
- send("#{attribute}_at=", nil)
20
+ send(setter_attribute, -> { Time.current }.())
17
21
  end
18
22
  end
23
+
24
+ define_method(:"#{attribute}!") do
25
+ send(:"#{attribute}=", true)
26
+ end
19
27
  end
20
28
  end
@@ -1,3 +1,3 @@
1
1
  module TimeForABoolean
2
- VERSION = '0.0.4'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -22,6 +22,12 @@ describe TimeForABoolean do
22
22
  expect(klass.new).to respond_to :attribute=
23
23
  end
24
24
 
25
+ it 'defines the bangable method to act as an implicit writer' do
26
+ klass.time_for_a_boolean :attribute
27
+
28
+ expect(klass.new).to respond_to :attribute!
29
+ end
30
+
25
31
  describe 'attribute method' do
26
32
  it 'calls nil? on the backing timestamp' do
27
33
  klass.time_for_a_boolean :attribute
@@ -35,7 +41,7 @@ describe TimeForABoolean do
35
41
 
36
42
  it 'is true if the attribute is not nil' do
37
43
  klass.time_for_a_boolean :attribute
38
- allow(object).to receive(:attribute_at).and_return(DateTime.now - 10)
44
+ allow(object).to receive(:attribute_at).and_return(Time.now - 10)
39
45
 
40
46
  expect(object.attribute).to be_truthy
41
47
  end
@@ -53,6 +59,39 @@ describe TimeForABoolean do
53
59
 
54
60
  expect(object.attribute).to be_falsey
55
61
  end
62
+
63
+ context 'when the user has defined their own attribute name' do
64
+ it 'calls nil? on the backing value' do
65
+ klass.time_for_a_boolean :attribute, :attribute_on
66
+ date = double(nil?: true)
67
+ allow(object).to receive(:attribute_on).and_return(date)
68
+
69
+ object.attribute
70
+
71
+ expect(date).to have_received(:nil?)
72
+ end
73
+
74
+ it 'is true if the attribute is not nil' do
75
+ klass.time_for_a_boolean :attribute, :attribute_on
76
+ allow(object).to receive(:attribute_on).and_return(Date.current - 10)
77
+
78
+ expect(object.attribute).to be_truthy
79
+ end
80
+
81
+ it 'is false if the attribute is nil' do
82
+ klass.time_for_a_boolean :attribute, :attribute_on
83
+ allow(object).to receive(:attribute_on)
84
+
85
+ expect(object.attribute).to be_falsey
86
+ end
87
+
88
+ it 'is false if the attribute date is in the future' do
89
+ klass.time_for_a_boolean :attribute, :attribute_on
90
+ allow(object).to receive(:attribute_on).and_return(Date.current + 1)
91
+
92
+ expect(object.attribute).to be_falsey
93
+ end
94
+ end
56
95
  end
57
96
 
58
97
  describe 'the query method' do
@@ -70,14 +109,14 @@ describe TimeForABoolean do
70
109
 
71
110
  object.attribute = true
72
111
 
73
- expect(object.attribute_at).to be_kind_of(DateTime)
112
+ expect(object.attribute_at).to be_kind_of(Time)
74
113
  end
75
114
 
76
115
  it 'sets the timestamp to nil if value is false' do
77
116
  klass.time_for_a_boolean :attribute
78
117
  klass.send(:attr_accessor, :attribute_at)
79
118
 
80
- object.attribute_at = DateTime.now
119
+ object.attribute_at = Time.now
81
120
  object.attribute = false
82
121
 
83
122
  expect(object.attribute_at).to be_nil
@@ -89,20 +128,31 @@ describe TimeForABoolean do
89
128
 
90
129
  object.attribute = '1'
91
130
 
92
- expect(object.attribute_at).to be_kind_of(DateTime)
131
+ expect(object.attribute_at).to be_kind_of(Time)
93
132
  end
94
133
 
95
134
  it 'works with other representations of false' do
96
135
  klass.time_for_a_boolean :attribute
97
136
  klass.send(:attr_accessor, :attribute_at)
98
137
 
99
- object.attribute_at = DateTime.now
138
+ object.attribute_at = Time.now
100
139
  object.attribute = '0'
101
140
 
102
141
  expect(object.attribute_at).to be_nil
103
142
  end
104
143
  end
105
144
 
145
+ describe 'the bangable method' do
146
+ it 'sets the timestamp to now' do
147
+ klass.time_for_a_boolean :attribute
148
+ klass.send(:attr_accessor, :attribute_at)
149
+
150
+ object.attribute!
151
+
152
+ expect(object.attribute_at).to be_kind_of(Time)
153
+ end
154
+ end
155
+
106
156
  def klass
107
157
  @klass ||= Class.new do
108
158
  extend TimeForABoolean
@@ -1,29 +1,30 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'time_for_a_boolean/version'
4
+ require "time_for_a_boolean/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = 'time_for_a_boolean'
8
- spec.version = TimeForABoolean::VERSION
9
- spec.authors = ['Caleb Thompson']
10
- spec.email = ['caleb@calebthompson.io']
7
+ spec.name = "time_for_a_boolean"
8
+ spec.version = TimeForABoolean::VERSION
9
+ spec.authors = ["Caleb Thompson"]
10
+ spec.email = ["caleb@calebthompson.io"]
11
11
  spec.description = <<-DESCRIPTION
12
12
  Use timestamp values to represent boolean data such as deleted, published,
13
13
  or subscribed.
14
14
  DESCRIPTION
15
- spec.summary = 'Back boolean values with timestamps'
16
- spec.homepage = 'https://github.com/calebthompson/time_for_a_boolean'
17
- spec.license = 'MIT'
15
+ spec.summary = "Back boolean values with timestamps"
16
+ spec.homepage = "https://github.com/calebthompson/time_for_a_boolean"
17
+ spec.license = "MIT"
18
18
 
19
- spec.files = `git ls-files`.split($/)
20
- spec.test_files = spec.files.grep(%r{^spec/})
21
- spec.require_paths = ['lib']
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.test_files = spec.files.grep(%r{^spec/})
21
+ spec.require_paths = ["lib"]
22
22
 
23
- spec.add_dependency 'rails'
23
+ spec.add_dependency "activerecord"
24
+ spec.add_dependency "railties"
24
25
 
25
- spec.add_development_dependency 'bundler', '~> 1.3'
26
- spec.add_development_dependency 'coveralls'
27
- spec.add_development_dependency 'rake'
28
- spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "coveralls"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "rspec"
29
30
  end
metadata CHANGED
@@ -1,17 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_for_a_boolean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Thompson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-31 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
@@ -28,16 +42,16 @@ dependencies:
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '1.3'
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '1.3'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: coveralls
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -91,7 +105,6 @@ extra_rdoc_files: []
91
105
  files:
92
106
  - ".gitignore"
93
107
  - ".ruby-gemset"
94
- - ".ruby-version"
95
108
  - CONTRIBUTING.md
96
109
  - Gemfile
97
110
  - LICENSE
@@ -106,7 +119,7 @@ homepage: https://github.com/calebthompson/time_for_a_boolean
106
119
  licenses:
107
120
  - MIT
108
121
  metadata: {}
109
- post_install_message:
122
+ post_install_message:
110
123
  rdoc_options: []
111
124
  require_paths:
112
125
  - lib
@@ -121,9 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
134
  - !ruby/object:Gem::Version
122
135
  version: '0'
123
136
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 2.2.2
126
- signing_key:
137
+ rubygems_version: 3.1.2
138
+ signing_key:
127
139
  specification_version: 4
128
140
  summary: Back boolean values with timestamps
129
141
  test_files:
@@ -1 +0,0 @@
1
- 2.1.2