date_range_formatter 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/Gemfile +1 -0
- data/README.md +14 -14
- data/lib/date_range_formatter.rb +33 -5
- data/lib/date_range_formatter/version.rb +1 -1
- data/lib/locale/en.yml +12 -1
- data/test/lib/date_range_formatter_test.rb +13 -1
- data/test/test_helper.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 519b570cbd7197b36603406f38ba039b399a7442
|
4
|
+
data.tar.gz: f60d00bd1dd302db894fe7b06e863245fee4d68a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba44e05668dd36eb52626b0730b7af0335f7d2161f88b681ed1b05c97939ed33cce7b018c40afae7a79d16df07a1f604f497dc8f4c9d9ca896ae69b24c1308b5
|
7
|
+
data.tar.gz: 6ebef3db359d7f83c276367d169dda15b89e02f9583d23ace0b346b287470f46979d35fd333e5fb5c0e5ca55a9380d18ca27dbf644d03fa2a8b7368a70474e1f
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/darkleaf/date_range_formatter.svg?branch=master)](https://travis-ci.org/darkleaf/date_range_formatter)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/date_range_formatter.svg)](http://badge.fury.io/rb/date_range_formatter)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/darkleaf/date_range_formatter.png)](https://codeclimate.com/github/darkleaf/date_range_formatter)
|
5
6
|
[![Build Status](https://drone.io/github.com/darkleaf/date_range_formatter/status.png)](https://drone.io/github.com/darkleaf/date_range_formatter/latest)
|
6
7
|
|
7
8
|
This gem makes working with dates more pretty. It works well with Ruby application and most frameworks like [Ruby on Rails](https://github.com/rails/rails "Ruby on Rails").
|
@@ -19,25 +20,16 @@ Imagine the situation when you need to show dates of some stuff at your website.
|
|
19
20
|
2 January 2015
|
20
21
|
15 July 2016 - 13 February 2017
|
21
22
|
|
22
|
-
To show this dates in this format you need to describe the format of displaying dates like 'default' in your locale file:
|
23
|
-
|
24
|
-
```yaml
|
25
|
-
en:
|
26
|
-
date_range:
|
27
|
-
default: # it's the 'format'
|
28
|
-
separator: " - " # added ' - ' symbol to the date ranges
|
29
|
-
month: "%B"
|
30
|
-
year: "%Y"
|
31
|
-
same_months: "%{from_day}-%{until_day} %{month} %{year}"
|
32
|
-
same_years: "%{from_day} %{from_month} - %{until_day} %{until_month} %{year}"
|
33
|
-
```
|
34
|
-
|
35
23
|
After that you should call the module DateRangeFormatter with arguments wchich describes the range of dates and format to display. For example, we have date_beginning, date_ending and format by default:
|
36
24
|
|
37
25
|
```ruby
|
38
26
|
DateRangeFormatter.format('2013-01-14', '2013-02-15')
|
39
27
|
#=> '14 January - 15 February 2013'
|
28
|
+
```
|
40
29
|
|
30
|
+
Also you can use `format_range` method and enumerable object:
|
31
|
+
|
32
|
+
```ruby
|
41
33
|
DateRangeFormatter.format_range(['2013-02-20', '2013-01-14', '2013-01-15'])
|
42
34
|
#=> '14 January - 20 February 2013'
|
43
35
|
```
|
@@ -50,7 +42,15 @@ date_ending = Date.new(2014, 02, 15)
|
|
50
42
|
date_range_str = DateRangeFormatter.format(date_beginning, date_ending, 'short')
|
51
43
|
```
|
52
44
|
|
53
|
-
|
45
|
+
If you want to show hours, you can call it:
|
46
|
+
```ruby
|
47
|
+
DateRangeFormatter.format('10:00 2013-01-14', '20:00 2013-01-14', :with_time)
|
48
|
+
#=> '14 January 2013, 10am - 08pm'
|
49
|
+
```
|
50
|
+
|
51
|
+
See [predefined formats](https://github.com/darkleaf/date_range_formatter/blob/master/lib/locale/en.yml). Also you can override this formats or add your own.
|
52
|
+
|
53
|
+
That's all. Enjoy your profit!
|
54
54
|
|
55
55
|
## Other
|
56
56
|
|
data/lib/date_range_formatter.rb
CHANGED
@@ -12,7 +12,7 @@ module DateRangeFormatter
|
|
12
12
|
|
13
13
|
def format_range(enumerable_range, format = :default)
|
14
14
|
return if enumerable_range.none?
|
15
|
-
sorted_range = enumerable_range.map(&:
|
15
|
+
sorted_range = enumerable_range.map(&:to_datetime).sort
|
16
16
|
date_beginning = sorted_range.first
|
17
17
|
date_ending = sorted_range.last
|
18
18
|
|
@@ -31,14 +31,15 @@ module DateRangeFormatter
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def to_s
|
34
|
+
return I18n.t 'same_hours', same_hours_data.merge(scope: ['date_range', format]) if same_hours?
|
34
35
|
return I18n.t 'same_days', same_days_data.merge(scope: ['date_range', format]) if same_days?
|
35
36
|
return I18n.t 'same_months', same_months_data.merge(scope: ['date_range', format]) if same_months?
|
36
37
|
return I18n.t 'same_years', same_years_data.merge(scope: ['date_range', format]) if same_years?
|
38
|
+
I18n.t 'different_components', different_components_data.merge(scope: ['date_range', format])
|
39
|
+
end
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
separator = I18n.t "separator", scope: ["date_range", format]
|
41
|
-
[from_str, separator, until_str].join
|
41
|
+
def same_hours?
|
42
|
+
same_days? && from_date.hour == until_date.hour
|
42
43
|
end
|
43
44
|
|
44
45
|
def same_days?
|
@@ -54,8 +55,19 @@ module DateRangeFormatter
|
|
54
55
|
end
|
55
56
|
|
56
57
|
private
|
58
|
+
def same_hours_data
|
59
|
+
{
|
60
|
+
hour: formatted_hour(from_date),
|
61
|
+
day: from_date.day,
|
62
|
+
month: formatted_month(from_date),
|
63
|
+
year: formatted_year(from_date),
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
57
67
|
def same_days_data
|
58
68
|
{
|
69
|
+
from_hour: formatted_hour(from_date),
|
70
|
+
until_hour: formatted_hour(until_date),
|
59
71
|
day: from_date.day,
|
60
72
|
month: formatted_month(from_date),
|
61
73
|
year: formatted_year(from_date),
|
@@ -81,6 +93,22 @@ module DateRangeFormatter
|
|
81
93
|
}
|
82
94
|
end
|
83
95
|
|
96
|
+
def different_components_data
|
97
|
+
{
|
98
|
+
from_day: from_date.day,
|
99
|
+
until_day: until_date.day,
|
100
|
+
from_month: formatted_month(from_date),
|
101
|
+
until_month: formatted_month(until_date),
|
102
|
+
from_year: formatted_year(from_date),
|
103
|
+
until_year: formatted_year(until_date),
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
def formatted_hour(date)
|
108
|
+
format_str = I18n.t "hour", scope: ["date_range", format]
|
109
|
+
I18n.l date, format: format_str
|
110
|
+
end
|
111
|
+
|
84
112
|
def formatted_month(date)
|
85
113
|
format_str = I18n.t "month", scope: ["date_range", format]
|
86
114
|
I18n.l date, format: format_str
|
data/lib/locale/en.yml
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
en:
|
2
2
|
date_range:
|
3
3
|
default:
|
4
|
-
separator: " - "
|
5
4
|
month: "%B"
|
6
5
|
year: "%Y"
|
6
|
+
same_hours: "%{day} %{month} %{year}"
|
7
7
|
same_days: "%{day} %{month} %{year}"
|
8
8
|
same_months: "%{from_day}-%{until_day} %{month} %{year}"
|
9
9
|
same_years: "%{from_day} %{from_month} - %{until_day} %{until_month} %{year}"
|
10
|
+
different_components: "%{from_day} %{from_month} %{from_year} - %{until_day} %{until_month} %{until_year}"
|
11
|
+
|
12
|
+
with_time:
|
13
|
+
hour: "%I%P"
|
14
|
+
month: "%B"
|
15
|
+
year: "%Y"
|
16
|
+
same_hours: "%{day} %{month} %{year}, %{hour}"
|
17
|
+
same_days: "%{day} %{month} %{year}, %{from_hour} - %{until_hour}"
|
18
|
+
same_months: "%{from_day}-%{until_day} %{month} %{year}"
|
19
|
+
same_years: "%{from_day} %{from_month} - %{until_day} %{until_month} %{year}"
|
20
|
+
different_components: "%{from_day} %{from_month} %{from_year} - %{until_day} %{until_month} %{until_year}"
|
@@ -31,7 +31,19 @@ class DateRangeFormatterTest < TestHelper
|
|
31
31
|
date_ending = Date.new(2014, 02, 15)
|
32
32
|
date_range_str = DateRangeFormatter.format(date_beginning, date_ending)
|
33
33
|
|
34
|
-
assert_equal '
|
34
|
+
assert_equal '14 January 2013 - 15 February 2014', date_range_str
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_same_hours_with_time_format
|
38
|
+
date_range_str = DateRangeFormatter.format('10:00 2013-01-14', '10:00 2013-01-14', :with_time)
|
39
|
+
|
40
|
+
assert_equal '14 January 2013, 10am', date_range_str
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_same_days_with_time_format
|
44
|
+
date_range_str = DateRangeFormatter.format('10:00 2013-01-14', '20:00 2013-01-14', :with_time)
|
45
|
+
|
46
|
+
assert_equal '14 January 2013, 10am - 08pm', date_range_str
|
35
47
|
end
|
36
48
|
end
|
37
49
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_range_formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Kuzmin
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|