jakewendt-stringify_date 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +36 -0
- data/lib/stringify_date.rb +59 -0
- metadata +83 -0
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= StringifyDate (Gem / Rails Plugin)
|
2
|
+
|
3
|
+
Based on tutorial from
|
4
|
+
{Ryan Bates}[http://railscasts.com/episodes/33-making-a-plugin]
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
Contains a collection of complex assertions.
|
9
|
+
|
10
|
+
config.gem 'jakewendt-stringify_date',
|
11
|
+
:lib => 'stringify_date',
|
12
|
+
:source => 'http://rubygems.org'
|
13
|
+
|
14
|
+
stringify_date :interview_outcome_on, :format => '%m/%d/%Y'
|
15
|
+
|
16
|
+
== ToDo
|
17
|
+
|
18
|
+
* add legitimate text to Rakefile
|
19
|
+
* add TESTS
|
20
|
+
|
21
|
+
== Gemified with Jeweler
|
22
|
+
|
23
|
+
vi Rakefile
|
24
|
+
rake version:write
|
25
|
+
rake gemspec
|
26
|
+
rake install
|
27
|
+
|
28
|
+
rake version:bump:major
|
29
|
+
rake release
|
30
|
+
|
31
|
+
== Example
|
32
|
+
|
33
|
+
Example goes here.
|
34
|
+
|
35
|
+
|
36
|
+
Copyright (c) 2008 [Jake Wendt & Ryan Bates], released under the MIT license
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# StringifyDate
|
2
|
+
|
3
|
+
## models/task.rb
|
4
|
+
#stringify_date :due_at
|
5
|
+
#
|
6
|
+
#def validate
|
7
|
+
# errors.add(:due_at, "is invalid") if due_at_invalid?
|
8
|
+
#end
|
9
|
+
|
10
|
+
# stringify_date.rb
|
11
|
+
module StringifyDate
|
12
|
+
def stringify_date(*names)
|
13
|
+
options = names.extract_options!
|
14
|
+
|
15
|
+
names.each do |name|
|
16
|
+
define_method "#{name}_string" do
|
17
|
+
# read_attribute(name).to_s(:db) unless read_attribute(name).nil?
|
18
|
+
# to_date added to fix sqlite3 quirk
|
19
|
+
# read_attribute(name).to_date.to_s(:db) unless read_attribute(name).nil?
|
20
|
+
|
21
|
+
d = read_attribute(name).try(:to_date)
|
22
|
+
unless d.blank?
|
23
|
+
if options[:format]
|
24
|
+
d.strftime(options[:format])
|
25
|
+
else
|
26
|
+
d.to_s(:db)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
define_method "#{name}_string=" do |date_str|
|
33
|
+
if ( !date_str.blank? )
|
34
|
+
begin
|
35
|
+
# write_attribute(name, Date.parse(date_str))
|
36
|
+
# Chronic.parse will not raise an error to rescue from
|
37
|
+
# chronic actually returns a time
|
38
|
+
date = Chronic.parse(date_str)
|
39
|
+
#Chronic.parse("February 14, 2004") # <= nil
|
40
|
+
# raise ArgumentError if date.nil?
|
41
|
+
date = Date.parse(date_str) if date.nil?
|
42
|
+
# if Date.parse fails, it will raise ArgumentError
|
43
|
+
# so I don't need to do it manually
|
44
|
+
write_attribute(name, date.to_date)
|
45
|
+
rescue ArgumentError
|
46
|
+
instance_variable_set("@#{name}_invalid", true)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
write_attribute(name,'')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
define_method "#{name}_invalid?" do
|
54
|
+
instance_variable_get("@#{name}_invalid")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
ActiveRecord::Base.send(:extend, StringifyDate)
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jakewendt-stringify_date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jake
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-14 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: chronic
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 17
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 2
|
33
|
+
- 3
|
34
|
+
version: 0.2.3
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: longer description of your gem
|
38
|
+
email: github@jake.otherinbox.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- lib/stringify_date.rb
|
47
|
+
- README.rdoc
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/jakewendt/stringify_date
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.7
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: one-line summary of your gem
|
82
|
+
test_files: []
|
83
|
+
|