time_period 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in time_period.gemspec
4
+ gemspec
data/README.textile ADDED
@@ -0,0 +1,96 @@
1
+ h1. time_period gem
2
+
3
+ h2. Mission Objective
4
+
5
+ Store time periods as "3 days" or "7 weeks" in a database.
6
+
7
+ We use one simple varchar column in which we store the value as above in the format "<integer> <day|week|month|year>".
8
+
9
+ h2. Example
10
+
11
+ In the migration, define a string field:
12
+
13
+ <pre>
14
+ ...
15
+ t.string :duration_period
16
+ ...
17
+ </pre>
18
+
19
+ In the model, declare the field as 'being a time period':
20
+
21
+ <pre>
22
+ class MyModel < ActiveRecord::Base
23
+ ...
24
+ time_period :duration_period, default => 7.days
25
+ ...
26
+ end
27
+ </pre>
28
+
29
+ then, you get the following methods:
30
+
31
+ * <tt>duration_period</tt> – raw reader of the saved string.
32
+ * <tt>duration_period_unit</tt> – getter for the unit, i.e., "day", "week", ...
33
+ * <tt>duration_period_unit=</tt> – setter for the unit, i.e., "day", "week", ...
34
+ * <tt>duration_period_number</tt> – getter for the number, i.e., 1, 2, 3
35
+ * <tt>duration_period_number=</tt> – setter for the number, i.e., 1, 2, 3
36
+ * <tt>duration_period=</tt> – setter for the complete period, accepts either strings as above or <tt>ActiveSupport::Duration</tt> objects as <tt>3.weeks</tt>
37
+ * <tt>duration_period_value</tt> – reads the value as an <tt>ActiveSupport::Duration</tt> object
38
+
39
+ <pre>
40
+ >> m = MyModel.new
41
+ >> m.duration_period_unit = "day"
42
+ "day"
43
+ >> m.duration
44
+ => "0 day"
45
+ >> m.duration_period_number = 7
46
+ => 7
47
+ >> m.duration
48
+ => "7 day"
49
+ >> m.duration_period_value
50
+ => 7 days
51
+ >> m.duration = '3 week'
52
+ => "3 week"
53
+ >> m.duration_period_value
54
+ => 21 days
55
+ </pre>
56
+
57
+ For <tt>simple_form</tt> forms, we offer an input:
58
+
59
+ <pre>
60
+ <%= simple_form_for @my_thing do |f| %>
61
+ ...
62
+ <%= f.input :duration_period, as: :time_period %>
63
+ ...
64
+ <% end %>
65
+ </pre>
66
+
67
+ The result then looks a little bit like this:
68
+
69
+ <img src="https://github.com/metaminded/time_period/raw/master/doc/form.png" />
70
+
71
+ h2. Disclaimer
72
+
73
+ This is pretty much work-in-progress-without-reasonable-documentation. So if you use it, don't complain ;)
74
+
75
+ Nevertheless, bug reports and improvements are highly appreciated:
76
+
77
+ h2. Contributing to time_period
78
+
79
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
80
+ * Check out the <a href="https://github.com/metaminded/time_period/issues">issue tracker</a> to make sure someone already hasn't requested it and/or contributed it
81
+ * Fork the project
82
+ * Start a feature/bugfix branch
83
+ * Commit and push until you are happy with your contribution
84
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
85
+ * Feel free to send a pull request if you think others (me, for example) would like to have your change incorporated into future versions of time_period.
86
+
87
+ h2. License
88
+
89
+ Copyright (c) 2011-2012 Peter Horn, <a href="http://www.metaminded.com" target="_blank">metaminded UG</a>
90
+
91
+ 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:
92
+
93
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
94
+
95
+ 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.
96
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,11 @@
1
+ class TimePeriodInput < SimpleForm::Inputs::Base
2
+ def input
3
+ @builder.text_field("#{attribute_name}_number") + " &nbsp; ".html_safe +
4
+ @builder.select("#{attribute_name}_unit",
5
+ I18n.translate('time_period.days') => 'day',
6
+ I18n.translate('time_period.weeks') => 'week',
7
+ I18n.translate('time_period.months') => 'month',
8
+ I18n.translate('time_period.years') => 'year'
9
+ )
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ de:
2
+ time_period:
3
+ days: Tage
4
+ weeks: Wochen
5
+ months: Monate
6
+ years: Jahre
7
+
@@ -0,0 +1,6 @@
1
+ en:
2
+ time_period:
3
+ days: Days
4
+ weeks: Weeks
5
+ months: Months
6
+ years: Years
data/doc/form.png ADDED
Binary file
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ # Time periods
4
+ #
5
+ # monkey patch ActiveRecord::Base
6
+ #
7
+ # (c) 2012 Peter Horn, metaminded UG
8
+
9
+ require "time_period/active_record"
10
+
11
+ module TimePeriod
12
+ class Engine < Rails::Engine
13
+ # Woohoo
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ # Time periods
4
+ #
5
+ # monkey patch ActiveRecord::Base
6
+ #
7
+ # (c) 2012 Peter Horn, metaminded UG
8
+
9
+ class ActiveRecord::Base
10
+ def self.time_period(name, oopts)
11
+ opts = {validate: true, default: nil}.merge oopts
12
+
13
+ define_method "#{name}_number" do
14
+ read_attribute(name).split(" ").first.to_i
15
+ end
16
+
17
+ define_method "#{name}_unit" do
18
+ read_attribute(name).split(" ").last
19
+ end
20
+
21
+ define_method "#{name}_value" do
22
+ n, p = read_attribute(name).split(" ")
23
+ case p
24
+ when 'week' then n.to_i.week
25
+ when 'month' then n.to_i.month
26
+ when 'day' then n.to_i.day
27
+ when 'year' then n.to_i.year
28
+ else raise "wrong format"
29
+ end
30
+ end
31
+
32
+ define_method "#{name}_number=" do |number|
33
+ n,p = read_attribute(name).split(" ")
34
+ p ||= 'day'
35
+ write_attribute(name, "#{number.to_i} #{p}")
36
+ number
37
+ end
38
+
39
+ define_method "#{name}_unit=" do |unit|
40
+ n,p = read_attribute(name).split(" ")
41
+ raise "unsupported unit '#{unit}'" unless %w{day week month year}.member?(unit.to_s)
42
+ write_attribute(name, "#{n.to_i} #{unit}")
43
+ unit
44
+ end
45
+
46
+ define_method "#{name}=" do |n_u|
47
+ number,unit = if n_u.is_a?(String)
48
+ n_u.split(" ")
49
+ elsif n_u.is_a?(ActiveSupport::Duration)
50
+ u,n = n_u.parts.flatten
51
+ [n.to_i, u.to_s.singularize]
52
+ else raise "Can't assign '#{n_u}' to '#{name}'."
53
+ end
54
+ raise "unsupported unit '#{unit}'" unless %w{day week month year}.member?(unit.to_s)
55
+ write_attribute(name, "#{number.to_i} #{unit}")
56
+ n_u
57
+ end
58
+
59
+ validates_format_of(name, :with => /^\d+ ((day)|(week)|(month)|(year))$/) if opts[:validate]
60
+
61
+ before_validation do
62
+ self.send("#{name}=", opts[:default]) unless read_attribute(name).present?
63
+ end if opts[:default]
64
+
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module TimePeriod
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "time_period/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "time_period"
7
+ s.version = TimePeriod::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Peter Horn"]
10
+ s.email = ["ph@metaminded.com"]
11
+ s.homepage = "https://github.com/metaminded/time_period"
12
+ s.summary = %q{Store time periods in a single column, with reasonable simple_form support}
13
+ s.description = %q{Store time periods as "1 week" or "2 months" in a single column, with reasonable simple_form support}
14
+
15
+ s.rubyforge_project = "time_period"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency('rails', '> 3.0.0')
23
+ s.add_runtime_dependency('simple_form')
24
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_period
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Horn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70241247290080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70241247290080
25
+ - !ruby/object:Gem::Dependency
26
+ name: simple_form
27
+ requirement: &70241247289480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70241247289480
36
+ description: Store time periods as "1 week" or "2 months" in a single column, with
37
+ reasonable simple_form support
38
+ email:
39
+ - ph@metaminded.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - README.textile
47
+ - Rakefile
48
+ - app/inputs/time_period_input.rb
49
+ - config/locales/de.yml
50
+ - config/locales/en.yml
51
+ - doc/form.png
52
+ - lib/time_period.rb
53
+ - lib/time_period/active_record.rb
54
+ - lib/time_period/version.rb
55
+ - time_period.gemspec
56
+ homepage: https://github.com/metaminded/time_period
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project: time_period
76
+ rubygems_version: 1.8.10
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Store time periods in a single column, with reasonable simple_form support
80
+ test_files: []