easy_dates 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +5 -0
- data/README.rdoc +87 -0
- data/Rakefile +12 -0
- data/easy_dates.gemspec +30 -0
- data/lib/easy_dates/base.rb +51 -0
- data/lib/easy_dates.rb +5 -0
- metadata +67 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
= Easy Dates
|
2
|
+
|
3
|
+
Easy date formatting for ActiveRecord. This gem keeps your models clean, and you don't have to worry about formatting your dates continually with strftime.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem install gemcutter
|
8
|
+
gem tumble
|
9
|
+
gem install easy_dates
|
10
|
+
|
11
|
+
or it can be installed as a rails plugin.
|
12
|
+
|
13
|
+
script/plugin install git://github.com/platform45/easy_dates.git
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
Define your date setup with define_easy_dates.
|
18
|
+
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
define_easy_dates do
|
21
|
+
format_for :created_at, :updated_at
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
The format_for method takes multiple options. For example:
|
26
|
+
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
define_easy_dates do
|
29
|
+
format_for :updated_at, :signup_date, :test_date # Array
|
30
|
+
format_for :created_at, :format => "%H:%M" # Uses strftime settings, :format can be used on arrays too
|
31
|
+
format_for :updated_at, :format => "%H:%M%P", :as => "updated_time" # :as will allow you to set your own name for your required format (:as does not work on an array of time columns like in the first example)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Once your dates have been defined you can access the new formatted date as follows:
|
36
|
+
|
37
|
+
@user = User.first
|
38
|
+
|
39
|
+
# Example 1 / format_for :updated_at, :signup_date, :test_date
|
40
|
+
@user.formatted_created_at
|
41
|
+
@user.formatted_signup_date
|
42
|
+
@user.formatted_test_date
|
43
|
+
|
44
|
+
# Example 2 / format_for :created_at, :format => "%H:%M"
|
45
|
+
@user.formated_created_at
|
46
|
+
|
47
|
+
# Example 3 / format_for :updated_at, :format => "%H:%M%P", :as => "updated_time"
|
48
|
+
@user.updated_time
|
49
|
+
|
50
|
+
In short:
|
51
|
+
|
52
|
+
Set your own name
|
53
|
+
:as => "new_name
|
54
|
+
|
55
|
+
Set your own format
|
56
|
+
:format => "strftime format"
|
57
|
+
|
58
|
+
Convenience method "formatted_column_name" is created, unless specified with :as
|
59
|
+
|
60
|
+
== Contact
|
61
|
+
|
62
|
+
Follow me on twitter: http://twitter.com/ryan_za
|
63
|
+
|
64
|
+
Email: ryan *at* platform45.com
|
65
|
+
|
66
|
+
== License
|
67
|
+
|
68
|
+
Copyright (c) 2009 Platform45
|
69
|
+
|
70
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
71
|
+
a copy of this software and associated documentation files (the
|
72
|
+
"Software"), to deal in the Software without restriction, including
|
73
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
74
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
75
|
+
permit persons to whom the Software is furnished to do so, subject to
|
76
|
+
the following conditions:
|
77
|
+
|
78
|
+
The above copyright notice and this permission notice shall be
|
79
|
+
included in all copies or substantial portions of the Software.
|
80
|
+
|
81
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
82
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
83
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
84
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
85
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
86
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
87
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('easy_dates', '0.0.1') do |p|
|
6
|
+
p.description = "Easy date formatting for ActiveRecord"
|
7
|
+
p.url = "http://github.com/platform45/easy_dates"
|
8
|
+
p.author = "Ryan Oberholzer"
|
9
|
+
p.email = "ryan@platform45.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
data/easy_dates.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{easy_dates}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ryan Oberholzer"]
|
9
|
+
s.date = %q{2009-12-18}
|
10
|
+
s.description = %q{Easy date formatting for ActiveRecord}
|
11
|
+
s.email = %q{ryan@platform45.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/easy_dates.rb", "lib/easy_dates/base.rb"]
|
13
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/easy_dates.rb", "lib/easy_dates/base.rb", "easy_dates.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/platform45/easy_dates}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easy_dates", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{easy_dates}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{Easy date formatting for ActiveRecord}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module EasyDates
|
2
|
+
module Base
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
TIMESTAMPS = [:created_at, :updated_at]
|
10
|
+
|
11
|
+
def define_easy_dates(&date_block)
|
12
|
+
instance_eval(&date_block)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def format_for(names = [], options = {})
|
18
|
+
format = choose_date_format(options[:format]) || "%Y-%m-%d"
|
19
|
+
names = [*names].flatten
|
20
|
+
|
21
|
+
if names.include?(:timestamps)
|
22
|
+
TIMESTAMPS.each {|ts| names << ts }
|
23
|
+
end
|
24
|
+
|
25
|
+
names.each do |name|
|
26
|
+
chosen_name = names.count == 1 ? (options[:as] || "formatted_#{name}") : "formatted_#{name}"
|
27
|
+
generate_method(name, chosen_name, format) unless name.equal?(:timestamps)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def generate_method(name, chosen_name, format)
|
32
|
+
class_eval <<-EOL
|
33
|
+
def #{chosen_name}
|
34
|
+
#{name}.strftime("#{format}")
|
35
|
+
end
|
36
|
+
EOL
|
37
|
+
end
|
38
|
+
|
39
|
+
def choose_date_format(format)
|
40
|
+
case format
|
41
|
+
when :us
|
42
|
+
"%m-%d-%Y"
|
43
|
+
when :world
|
44
|
+
"%m-%d-%Y"
|
45
|
+
else
|
46
|
+
format
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/easy_dates.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_dates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Oberholzer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-18 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Easy date formatting for ActiveRecord
|
17
|
+
email: ryan@platform45.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- lib/easy_dates.rb
|
25
|
+
- lib/easy_dates/base.rb
|
26
|
+
files:
|
27
|
+
- Manifest
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- lib/easy_dates.rb
|
31
|
+
- lib/easy_dates/base.rb
|
32
|
+
- easy_dates.gemspec
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/platform45/easy_dates
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --line-numbers
|
40
|
+
- --inline-source
|
41
|
+
- --title
|
42
|
+
- Easy_dates
|
43
|
+
- --main
|
44
|
+
- README.rdoc
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "1.2"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: easy_dates
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Easy date formatting for ActiveRecord
|
66
|
+
test_files: []
|
67
|
+
|