formatted-dates 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/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/*
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ 19 August 2008
2
+ - - - - - - - -
3
+ Initial Release
4
+
5
+ 22 August 2008
6
+ - - - - - - - -
7
+ Add filter param - Paul Campbell
8
+ Add 'today' support - Paul Campbell
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Paul Campbell
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.textile ADDED
@@ -0,0 +1,78 @@
1
+ h1. FormattedDates
2
+
3
+ I flippin' hate mucking about with strftime everytime I want to format dates. I hate adding new methods messing up my models. I hate .to_formatted_string.
4
+
5
+ This formatted_dates plugin is a quick acts_as hook into strftime for date fields your Rails models.
6
+
7
+ h2. Installation
8
+
9
+ Add this to config/environment.rb :
10
+
11
+ <pre><code>config.gem 'formatted-dates', :lib => 'formatted_dates', :version => '0.0.1', :source => 'http://gemcutter.org'</code></pre>
12
+
13
+ and run:
14
+
15
+ sudo rake gems:install
16
+
17
+ or
18
+
19
+ ./script/plugin install git://github.com/paulca/formatted-dates.git
20
+
21
+ h2. Example
22
+
23
+ In yer model:
24
+
25
+ <pre><code>Account < ActiveRecord::Base
26
+ format_dates :timestamps
27
+ end</code></pre>
28
+
29
+ This will give you access to new methods with _formatted appended to the end of any standard Rails timestamps in your model (eg. created_at, updated_at, created_on, updated_on):
30
+
31
+ They format using the default of "%e %B, %Y", which gives something like this:
32
+ updated_on_formatted => "9 April, 2003"
33
+
34
+ You can provide your own string to pass to strftime:
35
+
36
+ <pre><code>Account < ActiveRecord::Base
37
+ format_dates :timestamps, :format => "%m/%d/%Y"
38
+ end</code></pre>
39
+
40
+ And you can pass in other date fields from your model to be formatted:
41
+
42
+ <pre><code>Account < ActiveRecord::Base
43
+ format_dates [:timestamps, :registered_on, :last_login_date], :format => "%m/%d/%Y"
44
+ end</code></pre>
45
+
46
+ You can also format multiple fields differently:
47
+
48
+ <pre><code>Account < ActiveRecord::Base
49
+ format_dates :timestamps
50
+ format_dates :registered_on, :format => "%I:%M%p"
51
+ format_dates :last_login_date, :format => "%m/%d/%Y"
52
+ end</code></pre>
53
+
54
+ If you want to display the time for 'today' differently you can do so:
55
+ <pre><code>Account < ActiveRecord::Base
56
+ format_dates :created_on, :today => "%l:%M%p"
57
+ end<code></pre>
58
+
59
+ And if you want to filter the resulting string, you can do so too:
60
+
61
+ If you want to display the time for 'today' differently you can do so:
62
+ <pre><code>Account < ActiveRecord::Base
63
+ format_dates :created_on, :today => "%l:%M%p", :filter => :downcase # => '10:10pm'
64
+ emd</code></pre>
65
+
66
+ Finally, you can alias your formatted dates to a nice short, memorable name:
67
+
68
+ <pre><code>Account < ActiveRecord::Base
69
+ format_dates :created_on, :as => :date
70
+ end</code></pre>
71
+
72
+ This gives you a method @account.date that is equivalent to created_on_formatted
73
+
74
+ Copyright (c) 2008, 2009 Paul Campbell, released under the MIT license.
75
+
76
+ Follow me on Twitter: http://www.twitter.com/paulca
77
+
78
+ More ramblings on http://www.pabcas.com
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "formatted-dates"
5
+ gemspec.summary = "A simple acts_as style plugin for easily formatting of dates within Rails models"
6
+ gemspec.description = "A Rails plugin that provides _formatted methods for date fields."
7
+ gemspec.email = "paul@rslw.com"
8
+ gemspec.homepage = "http://github.com/paulca/formatted-dates"
9
+ gemspec.authors = ["Paul Campbell"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,48 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{formatted-dates}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Campbell"]
12
+ s.date = %q{2009-10-11}
13
+ s.description = %q{A Rails plugin that provides _formatted methods for date fields.}
14
+ s.email = %q{paul@rslw.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG",
21
+ "MIT-LICENSE",
22
+ "README.textile",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "formatted-dates.gemspec",
26
+ "lib/formatted_date.rb",
27
+ "rails/init.rb",
28
+ "spec/formatted_date_spec.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/paulca/formatted-dates}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.4}
34
+ s.summary = %q{A simple acts_as style plugin for easily formatting of dates within Rails models}
35
+ s.test_files = [
36
+ "spec/formatted_date_spec.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
@@ -0,0 +1,46 @@
1
+ module FormattedDate
2
+
3
+ def self.included(base)
4
+ base.extend FormatDatesMethod
5
+ end
6
+
7
+
8
+ module FormatDatesMethod
9
+
10
+ TIMESTAMPS = [:created_at, :updated_at, :created_on, :updated_on]
11
+
12
+ def format_dates(args = [], options = {})
13
+ format = options[:format] || "%e %B, %Y"
14
+ filter_date = options[:filter] || nil
15
+ as = options[:as] || nil
16
+ today = options[:today] || nil
17
+ args = [args].flatten
18
+
19
+ if args.include?(:timestamps)
20
+ TIMESTAMPS.each do |timestamp|
21
+ args << timestamp
22
+ end
23
+ end
24
+
25
+ args.each do |date_method|
26
+ class_eval "
27
+ def #{date_method}_formatted
28
+ return \"\" if respond_to?(:#{date_method}) and #{date_method}.nil?
29
+ return_date = #{date_method}.strftime(\"#{format}\").strip if respond_to?(:#{date_method})
30
+ "+ (today ? "return_date = #{date_method}.strftime(\"#{today}\").strip if respond_to?(:#{date_method}) and #{date_method}.to_date == Date.today" : '')+"
31
+ "+(filter_date ? "return_date = return_date.#{filter_date}" : '')+"
32
+ return_date
33
+ end
34
+ "
35
+ end
36
+
37
+ unless args.empty?
38
+ if as
39
+ class_eval "alias_method :#{as}, :#{args.first}_formatted"
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'formatted_date'
2
+ ActiveRecord::Base.send(:include, FormattedDate)
@@ -0,0 +1,99 @@
1
+ require File.dirname(__FILE__) + '/../lib/formatted_date.rb'
2
+ require File.dirname(__FILE__) + '/../../../../config/environment.rb'
3
+
4
+ class TestModel
5
+ include FormattedDate
6
+ format_dates :test_date
7
+ format_dates :test_date_with_format, :format => "%m/%d/%Y"
8
+ format_dates :timestamps
9
+ format_dates :created_at, :as => :date
10
+ format_dates :now, :today => "%l:%M%p", :as => :nice, :filter => :downcase
11
+ format_dates [:multi_one, :multi_two], :format => "%m/%d/%Y"
12
+ format_dates :nil_date
13
+
14
+ def nil_date
15
+ nil
16
+ end
17
+
18
+ def now
19
+ Time.parse('18:18')
20
+ end
21
+
22
+ def multi_one
23
+ Date.parse('1 April 2008')
24
+ end
25
+
26
+ def multi_two
27
+ Date.parse('2 April 2008')
28
+ end
29
+
30
+ def created_at
31
+ Date.parse('1 April 2003')
32
+ end
33
+
34
+ def updated_at
35
+ Date.parse('2 April 2003')
36
+ end
37
+
38
+ def test_date
39
+ Date.parse("9 April 2003")
40
+ end
41
+
42
+ def test_date_with_format
43
+ Date.parse("9 April 2003")
44
+ end
45
+ end
46
+
47
+ describe FormattedDate do
48
+ before(:each) do
49
+ @test_model = TestModel.new
50
+ end
51
+
52
+ it "should ignore a nil date" do
53
+ @test_model.nil_date_formatted.should == ""
54
+ end
55
+
56
+ it "should be a Date (my test mocky thing)" do
57
+ @test_model.test_date.should be_a_kind_of(Date)
58
+ end
59
+
60
+ it "should format the test date correctly" do
61
+ @test_model.test_date_formatted.should == '9 April, 2003'
62
+ end
63
+
64
+ it "should format the test date with a format option" do
65
+ @test_model.test_date_with_format_formatted.should == '04/09/2003'
66
+ end
67
+
68
+ it "should format the timestamps" do
69
+ @test_model.created_at_formatted.should == '1 April, 2003'
70
+ end
71
+
72
+ it "should format the timestamps" do
73
+ @test_model.updated_at_formatted.should == '2 April, 2003'
74
+ end
75
+
76
+ it "should not format created_on" do
77
+ @test_model.created_on_formatted.should be_nil
78
+ end
79
+
80
+ it "should not format updated_on" do
81
+ @test_model.updated_on_formatted.should be_nil
82
+ end
83
+
84
+ it "should add a date method" do
85
+ @test_model.date.should == '1 April, 2003'
86
+ end
87
+
88
+ it "should show today differently" do
89
+ @test_model.nice.should == '6:18pm'
90
+ end
91
+
92
+ it "should format multi one" do
93
+ @test_model.multi_one_formatted.should == '04/01/2008'
94
+ end
95
+
96
+ it "should format multi one" do
97
+ @test_model.multi_two_formatted.should == '04/02/2008'
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formatted-dates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Campbell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-11 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rails plugin that provides _formatted methods for date fields.
17
+ email: paul@rslw.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - .gitignore
26
+ - CHANGELOG
27
+ - MIT-LICENSE
28
+ - README.textile
29
+ - Rakefile
30
+ - VERSION
31
+ - formatted-dates.gemspec
32
+ - lib/formatted_date.rb
33
+ - rails/init.rb
34
+ - spec/formatted_date_spec.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/paulca/formatted-dates
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.4
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: A simple acts_as style plugin for easily formatting of dates within Rails models
63
+ test_files:
64
+ - spec/formatted_date_spec.rb