human_date 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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in human_date.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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,155 @@
1
+ # human_date
2
+
3
+ `human_date` allows you to display times and dates in a human format.
4
+
5
+ ```ruby
6
+ date = Date.current # 2012-08-28
7
+
8
+ human_date(date) # Today
9
+ human_date(time + 1.day) # Tomorrow
10
+ human_date(time - 1.day) # Yesterday
11
+ human_date(time - 2.days) # Aug 26
12
+ human_date(time - 1.year) # Aug 28, 2011
13
+
14
+ # it works with times, too
15
+
16
+ time = Time.now # 2012-08-28 17:20:54
17
+
18
+ human_time(time) # 5:20 pm
19
+ human_time(time - 1.day) # Yesterday
20
+ # ...
21
+ ```
22
+
23
+ `human_date` provides you with 5 states for your Time/Date objects:
24
+
25
+ - `today` displays time only: `5:20 pm`
26
+
27
+ - `tomorrow` displays: `Tomorrow`
28
+
29
+ - `yesterday` displays: `Yesterday`
30
+
31
+ - `current_year` displays the month and the day: `Aug 26`
32
+
33
+ - `other_year` displays the month, the day and the year: `Aug 28, 2011`
34
+
35
+ The default values can be overwriten in the locale file. See the I18n section for details.
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ gem 'human_date'
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install human_date
50
+
51
+ ## Usage
52
+
53
+ - `human_date` for human dates
54
+
55
+ - `human_time` for human times
56
+
57
+ - `human_time_tag` generates HTML5 `<time>` tag
58
+
59
+ ### Date examples
60
+ ```ruby
61
+ date = Date.current
62
+ # 2012-08-28
63
+
64
+ human_date(date)
65
+ # Today
66
+
67
+ human_time_tag(date)
68
+ # <time datetime="2012-08-28">Today</time>
69
+
70
+ human_time_tag(date, pubdate: true)
71
+ # <time datetime="2012-08-28" pubdate="pubdate">Today</time>
72
+ ```
73
+
74
+ ### Time examples
75
+ ```ruby
76
+ time = Time.zone.now
77
+ # 2012-08-28 17:20:54
78
+
79
+ human_time(time)
80
+ # 5:20 pm
81
+
82
+ human_time_tag(time)
83
+ # <time datetime="2012-08-28T17:20:54">5:20 PM</time>
84
+
85
+ ```
86
+
87
+ ### Namespaces
88
+
89
+ If you have several models and want each of them to have a different format (for example, for a `Post` you want to display 'Today' if it was posted today, but for a `Comment` you want to display something like '5:20 pm'), you can use namespaces.
90
+
91
+ ```ruby
92
+ # config/locales/en.yml
93
+ en:
94
+ posts:
95
+ time:
96
+ formats:
97
+ today: "Today"
98
+
99
+ comments:
100
+ time:
101
+ formats:
102
+ today: "%l:%M %p"
103
+ ```
104
+
105
+ ```ruby
106
+ time = Time.now
107
+ # 2012-08-28 17:20:54
108
+
109
+ human_time(time, :posts)
110
+ # Today
111
+
112
+ human_time(time, :comments)
113
+ # 5:20 pm
114
+
115
+ human_time_tag(time, :posts)
116
+ # <time datetime="2012-08-28T17:20:54">Today</time>
117
+
118
+ human_time_tag(time, :comments)
119
+ # <time datetime="2012-08-28T17:20:54">5:20 pm</time>
120
+
121
+ human_time_tag(time, :comments, pubdate: true)
122
+ # <time datetime="2012-08-28T17:20:54" pubdate="pubdate">5:20 pm</time>
123
+ ```
124
+
125
+ ## I18n
126
+
127
+ You can overwrite the default values in your locale file.
128
+ ```ruby
129
+ # config/locales/en.yml
130
+ en:
131
+ date:
132
+ formats:
133
+ today: "Today"
134
+ tomorrow: "Tomorrow"
135
+ yesterday: "Yesterday"
136
+ current_year: "%h %e"
137
+ other_year: "%h %e, %Y"
138
+
139
+ time:
140
+ formats:
141
+ today: "%l:%M %p"
142
+ tomorrow: "Tomorrow"
143
+ yesterday: "Yesterday"
144
+ current_year: "%h %e"
145
+ previous_years: "%h %e, %Y"
146
+
147
+ ```
148
+
149
+ ## Contributing
150
+
151
+ 1. Fork it
152
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
153
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
154
+ 4. Push to the branch (`git push origin my-new-feature`)
155
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
1
+ en:
2
+ date:
3
+ formats:
4
+ today: "Today"
5
+ tomorrow: "Tomorrow"
6
+ yesterday: "Yesterday"
7
+ current_year: "%h %e"
8
+ other_year: "%h %e, %Y"
9
+
10
+ time:
11
+ formats:
12
+ today: "%l:%M %p"
13
+ tomorrow: "Tomorrow"
14
+ yesterday: "Yesterday"
15
+ current_year: "%h %e"
16
+ other_year: "%h %e, %Y"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/human_date/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alexander Zaytsev"]
6
+ gem.email = ["alexander@say26.com"]
7
+ gem.description = %q{View helpers to make your Dates and Times more human like}
8
+ gem.summary = %q{Display dates and times for humans, not robots!}
9
+ gem.homepage = "http://github.com/AlexanderZaytsev/human_date"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "human_date"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = HumanTime::VERSION
17
+
18
+ gem.add_development_dependency "activesupport"
19
+ gem.add_development_dependency "actionpack"
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "timecop"
22
+ gem.add_development_dependency "tzinfo"
23
+ end
@@ -0,0 +1,16 @@
1
+ require 'human_date/view_helpers'
2
+
3
+
4
+ module HumanTime
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer 'rails-i18n' do |app|
8
+ I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../app/config/locales'), '*.yml')]
9
+ I18n.load_path.flatten!
10
+ end
11
+
12
+ initializer 'human_date.view_helpers' do
13
+ ActionView::Base.send :include, ViewHelpers
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module HumanTime
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,55 @@
1
+ include ActionView::Helpers::TagHelper
2
+
3
+ module HumanDate
4
+ module ViewHelpers
5
+ def human_date(object, namespace = nil)
6
+ I18n.localize(object, format: format_for_object(object, namespace)).strip
7
+ end
8
+ alias_method :human_time, :human_date
9
+
10
+ def human_time_tag(object, namespace_or_options = nil, options = nil)
11
+ if namespace_or_options.is_a? Hash
12
+ namespace = nil
13
+ options = namespace_or_options
14
+ else
15
+ namespace = namespace_or_options
16
+ options ||= {}
17
+ end
18
+
19
+ options.merge! datetime: object.iso8601
20
+
21
+ content_tag :time, human_time(object, namespace), options
22
+ end
23
+
24
+ private
25
+ def format_for_object(object, namespace = nil)
26
+ if namespace
27
+ format_for_namespace(object, namespace)
28
+ else
29
+ format_name_for_object(object)
30
+ end
31
+ end
32
+
33
+ def format_for_namespace(object, namespace)
34
+ object_type = object.respond_to?(:sec) ? 'time' : 'date'
35
+ format_name = format_name_for_object(object)
36
+ I18n.translate "#{namespace}.#{object_type}.formats.#{format_name}"
37
+ end
38
+
39
+ def format_name_for_object(object)
40
+ date = object.to_date
41
+ if date == Date.current
42
+ format = :today
43
+ elsif date == Date.yesterday
44
+ format = :yesterday
45
+ elsif date == Date.tomorrow
46
+ format = :tomorrow
47
+ elsif date.year == Date.today.year
48
+ format = :current_year
49
+ elsif date.year != Date.today.year
50
+ format = :other_year
51
+ end
52
+ format
53
+ end
54
+ end
55
+ end
data/lib/human_date.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'active_support/core_ext'
2
+ require 'action_view'
3
+ require 'human_date/version'
4
+ require 'human_date/railtie' if defined?(Rails)
5
+ require 'human_date/view_helpers'
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ include HumanDate::ViewHelpers
5
+
6
+
7
+ describe HumanDate::ViewHelpers do
8
+
9
+ before do
10
+ Timecop.freeze Time.parse('2012-08-28 17:20:54 +01:00').in_time_zone
11
+ @now = Time.zone.now
12
+ @today = Date.current
13
+ end
14
+
15
+ describe '#human_time' do
16
+ it 'returns date in human format' do
17
+ human_date(@today).should eq('Today')
18
+ end
19
+
20
+ it 'returns time in human format' do
21
+ human_time(@now).should eq('5:20 PM')
22
+ end
23
+
24
+ it 'works with namespaces' do
25
+ human_time(@now, :comments).should eq('17:20')
26
+ end
27
+
28
+ it 'works with locales' do
29
+ I18n.locale = :ru
30
+ human_time(@now).should eq('17:20:54')
31
+ I18n.locale = :en
32
+ end
33
+ end
34
+
35
+ describe '#human_time_tag' do
36
+ it 'returns <time> tag for time' do
37
+ human_time_tag(@now).should eq('<time datetime="2012-08-28T17:20:54+01:00">5:20 PM</time>')
38
+ end
39
+
40
+ it 'returns <time> tag for date' do
41
+ human_time_tag(@today).should eq('<time datetime="2012-08-28">Today</time>')
42
+ end
43
+
44
+ it 'allows passing options to <time> tag' do
45
+ human_time_tag(@today, pubdate: true).should eq('<time datetime="2012-08-28" pubdate="pubdate">Today</time>')
46
+ end
47
+
48
+ it 'allows using namespace' do
49
+ human_time_tag(@now, :comments).should eq('<time datetime="2012-08-28T17:20:54+01:00">17:20</time>')
50
+ end
51
+
52
+ it 'allows using namespace and passing options to <time> tag' do
53
+ human_time_tag(@now, :comments, pubdate: true).should eq('<time datetime="2012-08-28T17:20:54+01:00" pubdate="pubdate">17:20</time>')
54
+ end
55
+ end
56
+
57
+ describe '#format_name_for_object' do
58
+ it 'returns the valid format for Time' do
59
+ format_name_for_object(@now).should == :today
60
+ format_name_for_object(@now + 1.day).should == :tomorrow
61
+ format_name_for_object(@now - 1.day).should == :yesterday
62
+ format_name_for_object(@now - 2.days).should == :current_year
63
+ format_name_for_object(@now - 1.year).should == :other_year
64
+ end
65
+
66
+ it 'returns the valid format for Date' do
67
+ format_name_for_object(@today).should == :today
68
+ format_name_for_object(@today + 1.day).should == :tomorrow
69
+ format_name_for_object(@today - 1.day).should == :yesterday
70
+ format_name_for_object(@today - 2.days).should == :current_year
71
+ format_name_for_object(@today - 1.year).should == :other_year
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,17 @@
1
+ en:
2
+ date:
3
+ formats:
4
+ today: "Today"
5
+ current_year: "%h %e"
6
+ other_year: "%h %e, %Y"
7
+
8
+ time:
9
+ pm: 'PM'
10
+ am: 'AM'
11
+ formats:
12
+ today: "%l:%M %p"
13
+
14
+ comments:
15
+ time:
16
+ formats:
17
+ today: "%H:%M"
@@ -0,0 +1,4 @@
1
+ ru:
2
+ time:
3
+ formats:
4
+ today: "%H:%M:%S"
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require 'human_date'
3
+ require 'timecop'
4
+
5
+ I18n.locale = :en
6
+ Time.zone = 'London'
7
+
8
+ I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/locales'), '*.yml')]
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: human_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Zaytsev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &2153176320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2153176320
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &2153175900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2153175900
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2153175480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2153175480
47
+ - !ruby/object:Gem::Dependency
48
+ name: timecop
49
+ requirement: &2153175060 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2153175060
58
+ - !ruby/object:Gem::Dependency
59
+ name: tzinfo
60
+ requirement: &2153174640 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2153174640
69
+ description: View helpers to make your Dates and Times more human like
70
+ email:
71
+ - alexander@say26.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - app/config/locales/en.yml
83
+ - human_date.gemspec
84
+ - lib/human_date.rb
85
+ - lib/human_date/railtie.rb
86
+ - lib/human_date/version.rb
87
+ - lib/human_date/view_helpers.rb
88
+ - spec/human_date_spec.rb
89
+ - spec/locales/en.yml
90
+ - spec/locales/ru.yml
91
+ - spec/spec_helper.rb
92
+ homepage: http://github.com/AlexanderZaytsev/human_date
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.15
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Display dates and times for humans, not robots!
116
+ test_files:
117
+ - spec/human_date_spec.rb
118
+ - spec/locales/en.yml
119
+ - spec/locales/ru.yml
120
+ - spec/spec_helper.rb