has_duration 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.
- data/MIT-LICENSE +20 -0
- data/README.md +34 -0
- data/Rakefile +27 -0
- data/lib/has_duration/version.rb +3 -0
- data/lib/has_duration.rb +55 -0
- metadata +78 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
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,34 @@
|
|
1
|
+
# @markup markdown
|
2
|
+
|
3
|
+
# HasDuration
|
4
|
+
|
5
|
+
Do you use rails time convenience methods? Those things are great, they let you do stuff like `1.day.from_now` or `Time.now - 10.years`. The `10.years` part is an {ActiveSupport::Duration} object.
|
6
|
+
|
7
|
+
This plugin extends ActiveRecord so you can conveniently store durations like: how often to contact a given customer, or how long can your users stay in the bathtub without their fingertips wrinkling.
|
8
|
+
|
9
|
+
|
10
|
+
## Installation: Add this to your Gemfile
|
11
|
+
|
12
|
+
gem 'has_duration'
|
13
|
+
|
14
|
+
## Given a table defined as:
|
15
|
+
|
16
|
+
create_table :user do |t|
|
17
|
+
t.string :contact_every, null: false
|
18
|
+
t.string :bathtub_tolerance
|
19
|
+
end
|
20
|
+
|
21
|
+
## You can have a model like this
|
22
|
+
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
has_duration :contact_every
|
25
|
+
# Has duration does not validate presence, you have to do that yourself.
|
26
|
+
validates :contact_every, presence: true
|
27
|
+
has_duration :bathtub_tolerance
|
28
|
+
end
|
29
|
+
|
30
|
+
user = User.create!(contact_every: 1.year, bathtub_tolerance: 30.minutes)
|
31
|
+
puts 'that must be a record' if customer.bathtub_tolerance > 3.hours
|
32
|
+
puts "If contacted today, next contact would be made on #{user.contact_every.from_now}"
|
33
|
+
|
34
|
+
This project uses MIT-LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'HasDuration'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
data/lib/has_duration.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# The DurationValidator checks that a given field has an {ActiveSupport::Duration} or nil
|
2
|
+
# If you also need presence checks you should use ActiveSupport's built in presence validator.
|
3
|
+
# @note
|
4
|
+
# Although this method is available to you, you should use
|
5
|
+
# {HasDuration::ActiveRecordExtension::has_duration} which also adds serialization.
|
6
|
+
class DurationValidator < ActiveModel::EachValidator
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
unless value.is_a?(ActiveSupport::Duration) || value.nil?
|
9
|
+
record.errors[attribute] << 'must be an ActiveSupport::Duration (1.month, 2.years, etc)'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module HasDuration
|
15
|
+
# The DurationSerializer is a custom ActiveRecord serializer that stores durations in a text
|
16
|
+
# field in your model.
|
17
|
+
# @note
|
18
|
+
# Although this method is available to you, you should use
|
19
|
+
# {HasDuration::ActiveRecordExtension::has_duration} which also adds validation.
|
20
|
+
class DurationSerializer
|
21
|
+
def self.dump(duration)
|
22
|
+
duration.inspect.gsub(' ','.') if duration.is_a?(ActiveSupport::Duration)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.load(duration)
|
26
|
+
return if duration.nil?
|
27
|
+
if duration =~ /^(\d{0,10})\.(year|month|week|day|hour|minute|second)s?$/
|
28
|
+
$1.to_i.send($2)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module ActiveRecordExtension
|
34
|
+
# Adds a field to your ActiveRecord model that validates and serializes ActiveSupport::Duration objects, like: '1 year', '2 seconds', etc.
|
35
|
+
#
|
36
|
+
# For example:
|
37
|
+
# class VisitDuration < ActiveRecord::Base
|
38
|
+
# has_duration :doctor
|
39
|
+
# validates :doctor, presence: true
|
40
|
+
# has_duration :club
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# # ruby > durations = VisitDuration.create(doctor: 1.hour, club: 4.hours)
|
44
|
+
# # ruby > Time.now
|
45
|
+
# # => 2012-09-16 12:12:22 -0300
|
46
|
+
# # ruby > durations.doctor.from_now
|
47
|
+
# # => 2012-09-16 13:12:22 -0300
|
48
|
+
def has_duration(field_name)
|
49
|
+
serialize field_name, DurationSerializer
|
50
|
+
validates field_name, duration: true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
ActiveRecord::Base.send(:extend, HasDuration::ActiveRecordExtension)
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_duration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- nubis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70311913057680 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.8
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70311913057680
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3
|
27
|
+
requirement: &70311913057160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70311913057160
|
36
|
+
description: ! 'Extends ActiveRecord to let you store time durations like ''1.month''
|
37
|
+
or ''10.years''.
|
38
|
+
|
39
|
+
It does it by providing a serializer and validator for ActiveSupport::Duration objects.
|
40
|
+
|
41
|
+
'
|
42
|
+
email:
|
43
|
+
- nubis@woobiz.com.ar
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/has_duration/version.rb
|
49
|
+
- lib/has_duration.rb
|
50
|
+
- MIT-LICENSE
|
51
|
+
- Rakefile
|
52
|
+
- README.md
|
53
|
+
homepage: http://github.com/nubis/has_duration
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.15
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Extends ActiveRecord to let you store time durations like '1.month' or '10.years'
|
77
|
+
test_files: []
|
78
|
+
has_rdoc:
|