goodtimes 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in goodtimes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 C. Jason Harrelson (midas)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # Goodtimes
2
+
3
+ Tools for formatting date and time, including an Activerecord extension.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'goodtimes'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install goodtimes
19
+
20
+
21
+ ## Usage
22
+
23
+ Define some date_time_formats:
24
+
25
+ config/initializers/date_time_formats.rb
26
+
27
+ ```ruby
28
+ {
29
+ :isdn => "%Y-%m-%d",
30
+ :isdn_with_time => "%Y-%m-%d %I:%M %p"
31
+ }.each do |name, pattern|
32
+ Date::DATE_FORMATS.merge!( name => pattern )
33
+ Time::DATE_FORMATS.merge!( name => pattern )
34
+ end
35
+
36
+ {
37
+ :default => "%m/%d/%Y",
38
+ :db => "%Y-%m-%d"
39
+ }.each do |name, pattern|
40
+ Date::DATE_FORMATS.merge!( name => pattern )
41
+ end
42
+
43
+ {
44
+ :default => "%m/%d/%Y %R %Z",
45
+ :db => "%Y-%m-%d %H:%M:%S"
46
+ }.each do |name, pattern|
47
+ Time::DATE_FORMATS.merge!( name => pattern )
48
+ end
49
+ ```
50
+
51
+ Now when you call #to_s on an ActiveRecord instance you will get the
52
+ default format output:
53
+
54
+ ```ruby
55
+ person = Person.new( :date_of_birth => '1999-01-01' )
56
+ person.date_of_birth #=> 01/01/1999
57
+ ```
58
+
59
+ However, the converse is not true. If you try to set #date_of_birth
60
+ with the default format, you will get an 'invalid date' exception (if it
61
+ is not a date that Date can already parse).
62
+
63
+ ```ruby
64
+ person.date_of_birth = '1/1/1999' #=> invalid date
65
+ ```
66
+
67
+ In order to fix this:
68
+
69
+ ```ruby
70
+ class Person
71
+
72
+ date_attr_writer :date_of_birth
73
+
74
+ end
75
+
76
+ person = Person.new( :date_of_birth => '1/1/1999' )
77
+ person.date_of_birth #=> 01/01/1999
78
+ ```
79
+
80
+ When date_attr_writer is called with no options, it tries to use the
81
+ default date format defined for your Rails application:
82
+ Date::DATE_FORMATS[:default].
83
+
84
+ You can also specify a format to use in multiple ways. You can provide
85
+ a symbol and goodtimes will look for a format in Date::DATE_FORMATS that
86
+ matches.
87
+
88
+ ```ruby
89
+ date_attr_writer :date_of_birth, :format => :isdn
90
+ ```
91
+
92
+ Additionally you can give the :format option a string and goodtimes will
93
+ use it as the format.
94
+
95
+ ```ruby
96
+ date_attr_writer :date_of_birth, :format => '%m-%d-%Y'
97
+ date_attr_writer :date_of_birth, :format => Date::DATE_FORMATS[:isdn]
98
+ ```
99
+
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/goodtimes.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'goodtimes/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "goodtimes"
8
+ gem.version = Goodtimes::VERSION
9
+ gem.authors = ["C. Jason Harrelson (midas)"]
10
+ gem.email = ["jason@lookforwardenterprises.com"]
11
+ gem.description = %q{Tools for formatting date and time}
12
+ gem.summary = %q{Tools for formatting date and time, including some Activerecord extensions.}
13
+ gem.homepage = "https://github.com/ninja-loss/goodtimes"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,72 @@
1
+ module Goodtimes
2
+
3
+ module AttrWriters
4
+
5
+ def self.included( base )
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def date_attr_writer( *args )
12
+ options = args.extract_options!
13
+ format = options[:format]
14
+
15
+ if format
16
+ if format.is_a?( Symbol )
17
+ format_key = format
18
+ if Date::DATE_FORMATS.has_key?( format_key )
19
+ format = Date::DATE_FORMATS[format_key]
20
+ else
21
+ raise "The format key :#{format_key} is not a defined date format"
22
+ end
23
+ end
24
+ else
25
+ unless Date::DATE_FORMATS.has_key?( :default )
26
+ raise 'You must provide a format option or define a :default date format'
27
+ end
28
+ format = Date::DATE_FORMATS[:default]
29
+ end
30
+
31
+ args.each do |attr|
32
+ define_method "#{attr}=" do |date|
33
+ if date.is_a?( String )
34
+ date = date.blank? ? nil : Date.strptime( date, format )
35
+ end
36
+ write_attribute( attr, date )
37
+ end
38
+ end
39
+ end
40
+
41
+ def time_attr_writer( *args )
42
+ options = args.extract_options!
43
+ format = options[:format]
44
+
45
+ if format
46
+ if format.is_a?( Symbol )
47
+ format_key = format
48
+ if Time::DATE_FORMATS.has_key?( format_key )
49
+ format = Time::DATE_FORMATS[format_key]
50
+ else
51
+ raise 'The format key you provided is not a defined date format'
52
+ end
53
+ end
54
+ else
55
+ unless Time::DATE_FORMATS.has_key?( :default )
56
+ raise 'You must provide a format option or define a default date format'
57
+ end
58
+ format = Time::DATE_FORMATS[:default]
59
+ end
60
+
61
+ args.each do |attr|
62
+ define_method "#{attr}=" do |date|
63
+ write_attribute( attr, date.blank? ? nil : Time.strptime( date, format ))
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,30 @@
1
+ require 'goodtimes'
2
+
3
+ module Goodtimes
4
+
5
+ if defined? Rails::Railtie
6
+ require 'rails'
7
+
8
+ class Railtie < Rails::Railtie
9
+ initializer 'goodtimes.insert_into_active_record' do
10
+ ActiveSupport.on_load :active_record do
11
+ Goodtimes::Railtie.insert
12
+ end
13
+ end
14
+
15
+ #rake_tasks do
16
+ #load "tasks/paperclip.rake"
17
+ #end
18
+ end
19
+
20
+ end
21
+
22
+ class Railtie
23
+
24
+ def self.insert
25
+ ActiveRecord::Base.send :include, Goodtimes::AttrWriters
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,3 @@
1
+ module Goodtimes
2
+ VERSION = "0.2.0"
3
+ end
data/lib/goodtimes.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "goodtimes/attr_writers"
2
+ require 'goodtimes/railtie'
3
+ require "goodtimes/version"
4
+
5
+ module Goodtimes
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goodtimes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - C. Jason Harrelson (midas)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Tools for formatting date and time
15
+ email:
16
+ - jason@lookforwardenterprises.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - goodtimes.gemspec
27
+ - lib/goodtimes.rb
28
+ - lib/goodtimes/attr_writers.rb
29
+ - lib/goodtimes/railtie.rb
30
+ - lib/goodtimes/version.rb
31
+ homepage: https://github.com/ninja-loss/goodtimes
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Tools for formatting date and time, including some Activerecord extensions.
55
+ test_files: []