date_range_formatter 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -2
- data/lib/date_range_formatter/version.rb +1 -1
- data/lib/date_range_formatter.rb +19 -1
- data/lib/locale/en.yml +1 -0
- data/test/lib/date_range_formatter_test.rb +2 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e002e59aee2911d9192d69da6ed4ab83f1091538
|
4
|
+
data.tar.gz: fb8a825c58f5ea82b846fab2fabb4ef334bf4120
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46c3ed5474248d53be8ea36c90b90a71e21632e4361420d89f64e6bbdb8b54a2bf2a2266af107787cfaa4a2ef5b457261a6e78152533e54fea393b7dbf2a7851
|
7
|
+
data.tar.gz: a98d262d8a491a608e4eee483bff1ca7ba746618e2b0bc8e8dae05cc41b01ed90901080842cbd387f20d058d889d30a8aa1ef678c4cc121b2a1a05fdfc5cb4c5
|
data/README.md
CHANGED
@@ -1,4 +1,59 @@
|
|
1
|
-
|
2
|
-
====================
|
1
|
+
# Date Range Formatter
|
3
2
|
|
4
3
|
[![Build Status](https://travis-ci.org/darkleaf/date_range_formatter.svg?branch=master)](https://travis-ci.org/darkleaf/date_range_formatter)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/date_range_formatter.svg)](http://badge.fury.io/rb/date_range_formatter)
|
5
|
+
[![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
|
+
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").
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
Very simple. Just add this code to Gemfile:
|
11
|
+
|
12
|
+
gem 'date_range_formatter'
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
Imagine the situation when you need to show dates of some stuff at your website. For example:
|
17
|
+
|
18
|
+
1 - 9 May 2014
|
19
|
+
2 January 2015
|
20
|
+
15 July 2016 - 13 February 2017
|
21
|
+
|
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
|
+
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
|
+
|
37
|
+
```ruby
|
38
|
+
DateRangeFormatter.format('2013-01-14', '2013-02-15')
|
39
|
+
#=> '14 January - 15 February 2013'
|
40
|
+
|
41
|
+
DateRangeFormatter.format_range(['2013-02-20', '2013-01-14', '2013-01-15'])
|
42
|
+
#=> '14 January - 20 February 2013'
|
43
|
+
```
|
44
|
+
|
45
|
+
If you want to show dates by another format, you can call it:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
date_beginning = Date.new(2013, 01, 14)
|
49
|
+
date_ending = Date.new(2014, 02, 15)
|
50
|
+
date_range_str = DateRangeFormatter.format(date_beginning, date_ending, 'short')
|
51
|
+
```
|
52
|
+
|
53
|
+
That's all. Enjoy yout profit!
|
54
|
+
|
55
|
+
## Other
|
56
|
+
|
57
|
+
This idea was appeared by looking at the [article](https://coderwall.com/p/fkg-ng). Thanks to [@mbillard](https://github.com/mbillard).
|
58
|
+
|
59
|
+
|
data/lib/date_range_formatter.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/core_ext/string/conversions.rb'
|
1
2
|
require 'active_support/i18n'
|
2
3
|
|
3
4
|
I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml"
|
@@ -6,6 +7,15 @@ module DateRangeFormatter
|
|
6
7
|
|
7
8
|
class << self
|
8
9
|
def format(date_beginning, date_ending, format = :default)
|
10
|
+
format_range [date_beginning, date_ending], format
|
11
|
+
end
|
12
|
+
|
13
|
+
def format_range(enumerable_range, format = :default)
|
14
|
+
return if enumerable_range.none?
|
15
|
+
sorted_range = enumerable_range.map(&:to_date).sort
|
16
|
+
date_beginning = sorted_range.first
|
17
|
+
date_ending = sorted_range.last
|
18
|
+
|
9
19
|
f = Formatter.new date_beginning, date_ending, format
|
10
20
|
f.to_s
|
11
21
|
end
|
@@ -21,7 +31,7 @@ module DateRangeFormatter
|
|
21
31
|
end
|
22
32
|
|
23
33
|
def to_s
|
24
|
-
return I18n.
|
34
|
+
return I18n.t 'same_days', same_days_data.merge(scope: ['date_range', format]) if same_days?
|
25
35
|
return I18n.t 'same_months', same_months_data.merge(scope: ['date_range', format]) if same_months?
|
26
36
|
return I18n.t 'same_years', same_years_data.merge(scope: ['date_range', format]) if same_years?
|
27
37
|
|
@@ -44,6 +54,14 @@ module DateRangeFormatter
|
|
44
54
|
end
|
45
55
|
|
46
56
|
private
|
57
|
+
def same_days_data
|
58
|
+
{
|
59
|
+
day: from_date.day,
|
60
|
+
month: formatted_month(from_date),
|
61
|
+
year: formatted_year(from_date),
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
47
65
|
def same_months_data
|
48
66
|
{
|
49
67
|
from_day: from_date.day,
|
data/lib/locale/en.yml
CHANGED
@@ -5,11 +5,9 @@ class DateRangeFormatterTest < TestHelper
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def test_same_days
|
8
|
-
|
9
|
-
date_ending = Date.new(2013, 01, 14)
|
10
|
-
date_range_str = DateRangeFormatter.format(date_beginning, date_ending)
|
8
|
+
date_range_str = DateRangeFormatter.format('2013-01-14', '2013-01-14')
|
11
9
|
|
12
|
-
assert_equal '2013
|
10
|
+
assert_equal '14 January 2013', date_range_str
|
13
11
|
end
|
14
12
|
|
15
13
|
def test_same_months
|
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.0
|
4
|
+
version: 0.1.0
|
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-
|
12
|
+
date: 2014-06-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
109
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.1.11
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: Date range formatter
|