act_as_time_as_boolean 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ Gemfile.lock
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2013 caedes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # ActAsTimeAsBoolean
2
+
3
+ _Add time_as_boolean feature to your ruby classes_
4
+
5
+ ## Installation
6
+
7
+ ```shell
8
+ gem install act_as_time_as_boolean
9
+ ```
10
+
11
+ Or in your Gemfile:
12
+
13
+ ```ruby
14
+ gem 'act_as_time_as_boolean'
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ class Item
21
+ include ActAsTimeAsBoolean
22
+
23
+ attr_accessor :active_at
24
+
25
+ time_as_boolean :active, opposite: :inactive
26
+ end
27
+
28
+ item = Item.new
29
+
30
+ p item.active?
31
+ #=> false
32
+
33
+ p item.inactive?
34
+ #=> true
35
+
36
+ item.active = true
37
+
38
+ p item.active?
39
+ #=> true
40
+
41
+ p item.inactive?
42
+ #=> false
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork repository
48
+ 2. Create a branch following a [successfull branching model](http://nvie.com/posts/a-successful-git-branching-model/)
49
+ 3. Write your feature/fix
50
+ 4. Write tests
51
+ 5. Pull request
52
+
53
+ ## Licence
54
+
55
+ Released under the MIT License. See the [LICENSE](https://github.com/caedes/act_as_time_as_boolean/blob/master/LICENSE.md) file for further details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../lib/act_as_time_as_boolean/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'act_as_time_as_boolean'
5
+ s.version = ActAsTimeAsBoolean::VERSION
6
+ s.authors = ['caedes']
7
+ s.email = ['laurentromain@gmail.com']
8
+ s.homepage = 'https://github.com/caedes/act_as_time_as_boolean'
9
+ s.summary = 'Add time_as_boolean feature to your ruby classes'
10
+ s.description = s.summary
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+
14
+ s.require_path = 'lib'
15
+
16
+ s.add_development_dependency 'rake'
17
+ s.add_development_dependency 'rspec', '~> 2.13'
18
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'act_as_time_as_boolean'
@@ -0,0 +1,2 @@
1
+ require 'act_as_time_as_boolean/version'
2
+ require 'act_as_time_as_boolean/base'
@@ -0,0 +1,33 @@
1
+ module ActAsTimeAsBoolean
2
+
3
+ def self.included(base)
4
+ base.define_singleton_method(:time_as_boolean) do |field, options={}|
5
+ field = field.to_sym
6
+
7
+ self.send :define_method, field do
8
+ !send(:"#{field}_at").nil?
9
+ end
10
+
11
+ self.send :alias_method, :"#{field}?", :"#{field}"
12
+
13
+ self.send :define_method, :"#{field}=" do |value|
14
+ if (value && value != 'false' && value != '0' && !self.send(field)) || (!value && self.send(field))
15
+ if value && value != 'false' && value != '0'
16
+ send :"#{field}_at=", Time.now
17
+ true
18
+ else
19
+ send :"#{field}_at=", nil
20
+ end
21
+ end
22
+ end
23
+
24
+ if options[:opposite]
25
+ self.send :define_method, :"#{options[:opposite]}" do
26
+ send(:"#{field}_at").nil?
27
+ end
28
+
29
+ self.send :alias_method, :"#{options[:opposite]}?", :"#{options[:opposite]}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ActAsTimeAsBoolean
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,146 @@
1
+ require_relative '../spec_helper'
2
+
3
+ class SimpleTimeAsBooleanModel
4
+ include ActAsTimeAsBoolean
5
+
6
+ attr_accessor :active_at
7
+
8
+ time_as_boolean :active
9
+ end
10
+
11
+ class TimeAsBooleanWithOppositeModel
12
+ include ActAsTimeAsBoolean
13
+
14
+ attr_accessor :active_at
15
+
16
+ def initialize(options={})
17
+ @active_at = options[:active_at] || nil
18
+ end
19
+
20
+ time_as_boolean :active, opposite: :inactive
21
+ end
22
+
23
+ describe ActAsTimeAsBoolean do
24
+ it 'defines time_as_boolean class method' do
25
+ SimpleTimeAsBooleanModel.singleton_methods.should include(:time_as_boolean)
26
+ end
27
+
28
+ describe 'calling time_as_boolean' do
29
+ describe 'without param' do
30
+ it 'raises an ArgumentError' do
31
+ expect do
32
+ class NoParamTimeAsBooleanModel
33
+ include ActAsTimeAsBoolean
34
+
35
+ time_as_boolean
36
+ end
37
+ end.to raise_error(ArgumentError)
38
+ end
39
+ end
40
+
41
+ describe 'with :active param' do
42
+ subject { SimpleTimeAsBooleanModel.new.methods }
43
+
44
+ it 'defines active method' do
45
+ subject.should include(:active)
46
+ end
47
+ it 'defines active? method' do
48
+ subject.should include(:active?)
49
+ end
50
+ it 'defines active= method' do
51
+ subject.should include(:active=)
52
+ end
53
+ end
54
+
55
+ describe 'with :active and opposite param' do
56
+ subject { TimeAsBooleanWithOppositeModel.new }
57
+
58
+ it 'defines active method' do
59
+ subject.methods.should include(:active)
60
+ end
61
+ it 'defines active? method' do
62
+ subject.methods.should include(:active?)
63
+ end
64
+ it 'defines active= method' do
65
+ subject.methods.should include(:active=)
66
+ end
67
+ it 'defines inactive method' do
68
+ subject.methods.should include(:inactive)
69
+ end
70
+ it 'defines inactive? method' do
71
+ subject.methods.should include(:inactive?)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe 'using ActAsTimeAsBoolean' do
77
+ describe 'with an active instance' do
78
+ let(:time) { Time.now }
79
+ subject { TimeAsBooleanWithOppositeModel.new active_at: time }
80
+
81
+ describe 'calling active' do
82
+ it { subject.active.should be_true }
83
+ end
84
+
85
+ describe 'calling active?' do
86
+ it { subject.active?.should be_true }
87
+ end
88
+
89
+ describe 'calling active=' do
90
+ describe 'with true' do
91
+ before { subject.active = true }
92
+
93
+ it { subject.active_at.should == time }
94
+ end
95
+
96
+ describe 'with false' do
97
+ before { subject.active = false }
98
+
99
+ it { subject.active_at.should be_nil }
100
+ end
101
+ end
102
+
103
+ describe 'calling inactive' do
104
+ it { subject.inactive.should be_false }
105
+ end
106
+
107
+ describe 'calling inactive?' do
108
+ it { subject.inactive?.should be_false }
109
+ end
110
+ end
111
+
112
+ describe 'with an inactive instance' do
113
+ subject { TimeAsBooleanWithOppositeModel.new }
114
+
115
+ describe 'calling active' do
116
+ it { subject.active.should be_false }
117
+ end
118
+
119
+ describe 'calling active?' do
120
+ it { subject.active?.should be_false }
121
+ end
122
+
123
+ describe 'calling active=' do
124
+ describe 'with true' do
125
+ before { subject.active = true }
126
+
127
+ it { subject.active_at.class.should == Time }
128
+ end
129
+
130
+ describe 'with false' do
131
+ before { subject.active = false }
132
+
133
+ it { subject.active_at.should be_nil }
134
+ end
135
+ end
136
+
137
+ describe 'calling inactive' do
138
+ it { subject.inactive.should be_true }
139
+ end
140
+
141
+ describe 'calling inactive?' do
142
+ it { subject.inactive?.should be_true }
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1 @@
1
+ require 'act_as_time_as_boolean'
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: act_as_time_as_boolean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - caedes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.13'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.13'
46
+ description: Add time_as_boolean feature to your ruby classes
47
+ email:
48
+ - laurentromain@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.md
56
+ - README.md
57
+ - Rakefile
58
+ - act_as_time_as_boolean.gemspec
59
+ - init.rb
60
+ - lib/act_as_time_as_boolean.rb
61
+ - lib/act_as_time_as_boolean/base.rb
62
+ - lib/act_as_time_as_boolean/version.rb
63
+ - spec/lib/act_as_time_as_boolean_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: https://github.com/caedes/act_as_time_as_boolean
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.25
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Add time_as_boolean feature to your ruby classes
89
+ test_files: []