semantic_date_time_tags 0.0.10 → 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/Gemfile.lock +1 -1
- data/lib/config/locales/semantic_date_time_tags.en.yml +3 -0
- data/lib/semantic_date_time_tags/tag.rb +61 -14
- data/lib/semantic_date_time_tags/tag/date.rb +7 -7
- data/lib/semantic_date_time_tags/tag/date_range.rb +12 -9
- data/lib/semantic_date_time_tags/tag/date_time.rb +7 -7
- data/lib/semantic_date_time_tags/tag/time.rb +8 -7
- data/lib/semantic_date_time_tags/version.rb +1 -1
- data/lib/semantic_date_time_tags/view_helpers.rb +5 -5
- data/test/semantic_date_time_tags/view_helpers_test.rb +35 -5
- 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: aeed110b330e1302a7615ba2f93304b79e62a30c
|
4
|
+
data.tar.gz: cdca5a50d3f7f8e526eaf41e96edd931415d86f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79e1b964be9feb59c656b2737c6262fba6c6ae850cc4582e0ad77f6d458c0b2730ec0169ed470bbfc5d1d5611985965b03cb9fda99c869e9bc7d54ab81df8054
|
7
|
+
data.tar.gz: 5262805904fb6d6ce1411f4bb230b5dc7f537a777d1c20b133b04ef38003d781993d9d12e8bf79631804666cacd2202ae07c64c5db1f8ee49f0d85d03ae89a92
|
data/Gemfile.lock
CHANGED
@@ -7,13 +7,14 @@ module SemanticDateTimeTags
|
|
7
7
|
include ActionView::Helpers::DateHelper
|
8
8
|
include ActionView::Helpers::TagHelper
|
9
9
|
|
10
|
+
attr_accessor :obj
|
11
|
+
attr_accessor :options
|
10
12
|
attr_accessor :output_buffer
|
11
13
|
|
12
14
|
# =====================================================================
|
13
15
|
|
14
|
-
def initialize obj,
|
16
|
+
def initialize obj, options={}
|
15
17
|
@obj = obj
|
16
|
-
@tag_name = tag_name
|
17
18
|
@options = options.tap{ |opts| opts.delete(:scope) }
|
18
19
|
end
|
19
20
|
|
@@ -31,34 +32,54 @@ module SemanticDateTimeTags
|
|
31
32
|
type_class,
|
32
33
|
current_date_class,
|
33
34
|
current_year_class,
|
35
|
+
midnight_class,
|
36
|
+
noon_class,
|
34
37
|
whole_hour_class,
|
35
38
|
whole_minute_class,
|
36
|
-
|
39
|
+
options[:class]
|
37
40
|
].flatten.reject(&:blank?)
|
38
41
|
end
|
39
42
|
|
40
43
|
def type_class
|
41
|
-
|
44
|
+
obj.class.to_s.underscore
|
42
45
|
end
|
43
46
|
|
44
47
|
def current_date_class
|
45
|
-
return unless [::Date,::DateTime].any?{ |c|
|
46
|
-
'current_date' if
|
48
|
+
return unless [::Date,::DateTime].any?{ |c| obj.instance_of? c }
|
49
|
+
'current_date' if obj.today?
|
47
50
|
end
|
48
51
|
|
49
52
|
def current_year_class
|
50
|
-
return unless [::Date,::DateTime].any?{ |c|
|
51
|
-
'current_year' if
|
53
|
+
return unless [::Date,::DateTime].any?{ |c| obj.instance_of? c }
|
54
|
+
'current_year' if obj.year == ::Date.today.year
|
52
55
|
end
|
53
56
|
|
54
57
|
def whole_hour_class
|
55
|
-
return unless [::Time,::DateTime].any?{ |c|
|
56
|
-
'whole_hour' unless
|
58
|
+
return unless [::Time,::DateTime].any?{ |c| obj.instance_of? c }
|
59
|
+
'whole_hour' unless obj.min > 0
|
57
60
|
end
|
58
61
|
|
59
62
|
def whole_minute_class
|
60
|
-
return unless [::Time,::DateTime].any?{ |c|
|
61
|
-
'whole_minute' unless
|
63
|
+
return unless [::Time,::DateTime].any?{ |c| obj.instance_of? c }
|
64
|
+
'whole_minute' unless obj.sec > 0
|
65
|
+
end
|
66
|
+
|
67
|
+
def noon_class
|
68
|
+
return unless [::Time,::DateTime].any?{ |c| obj.instance_of? c }
|
69
|
+
'noon' if obj == obj.noon
|
70
|
+
end
|
71
|
+
|
72
|
+
def midnight_class
|
73
|
+
return unless [::Time,::DateTime].any?{ |c| obj.instance_of? c }
|
74
|
+
'midnight' if obj == obj.midnight
|
75
|
+
end
|
76
|
+
|
77
|
+
# ---------------------------------------------------------------------
|
78
|
+
|
79
|
+
def dom_data
|
80
|
+
{
|
81
|
+
in_words: in_words
|
82
|
+
}
|
62
83
|
end
|
63
84
|
|
64
85
|
private # =============================================================
|
@@ -68,11 +89,37 @@ module SemanticDateTimeTags
|
|
68
89
|
end
|
69
90
|
|
70
91
|
def format
|
71
|
-
|
92
|
+
options.fetch :format, :full
|
72
93
|
end
|
73
94
|
|
95
|
+
# ---------------------------------------------------------------------
|
96
|
+
|
74
97
|
def localized_obj
|
75
|
-
I18n.l
|
98
|
+
I18n.l obj, format: format
|
99
|
+
end
|
100
|
+
|
101
|
+
# ---------------------------------------------------------------------
|
102
|
+
|
103
|
+
def tag_name
|
104
|
+
options.fetch :tag_name, :time
|
105
|
+
end
|
106
|
+
|
107
|
+
# ---------------------------------------------------------------------
|
108
|
+
|
109
|
+
def in_words
|
110
|
+
[ noon_in_words, midnight_in_words ].reject(&:blank?).join(' ')
|
111
|
+
end
|
112
|
+
|
113
|
+
def noon_in_words
|
114
|
+
return unless [::Time,::DateTime].any?{ |c| obj.instance_of? c }
|
115
|
+
return unless obj == obj.noon
|
116
|
+
I18n.t :noon, scope: %i(time in_words)
|
117
|
+
end
|
118
|
+
|
119
|
+
def midnight_in_words
|
120
|
+
return unless [::Time,::DateTime].any?{ |c| obj.instance_of? c }
|
121
|
+
return unless obj == obj.midnight
|
122
|
+
I18n.t :midnight, scope: %i(time in_words)
|
76
123
|
end
|
77
124
|
|
78
125
|
end
|
@@ -4,24 +4,24 @@ module SemanticDateTimeTags
|
|
4
4
|
class Tag
|
5
5
|
class Date < Tag
|
6
6
|
|
7
|
-
def initialize obj,
|
7
|
+
def initialize obj, options={}
|
8
8
|
raise 'object must be Date or DateTime' unless [::Date, ::DateTime].any? { |c| obj.instance_of? c }
|
9
|
-
super(obj,
|
9
|
+
super(obj, options)
|
10
10
|
end
|
11
11
|
|
12
12
|
# ---------------------------------------------------------------------
|
13
13
|
|
14
14
|
def to_html
|
15
|
-
if
|
16
|
-
datetime =
|
17
|
-
|
15
|
+
if tag_name == :time
|
16
|
+
datetime = obj.acts_like?(:time) ? obj.xmlschema : obj.iso8601
|
17
|
+
options[:datetime] = datetime
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
options[:class] = dom_classes
|
21
21
|
|
22
22
|
value = SemanticDateTimeTags::FormatParser.new(format_string, localized_obj).to_html.html_safe
|
23
23
|
|
24
|
-
content_tag(
|
24
|
+
content_tag(tag_name, options) { value }.html_safe
|
25
25
|
end
|
26
26
|
|
27
27
|
private # =============================================================
|
@@ -2,6 +2,9 @@ module SemanticDateTimeTags
|
|
2
2
|
class Tag
|
3
3
|
class DateRange < Tag
|
4
4
|
|
5
|
+
attr_accessor :date_from
|
6
|
+
attr_accessor :date_to
|
7
|
+
|
5
8
|
def initialize date_from, date_to=nil, options={}
|
6
9
|
@date_from = date_from
|
7
10
|
@date_to = date_to
|
@@ -11,25 +14,25 @@ module SemanticDateTimeTags
|
|
11
14
|
# ---------------------------------------------------------------------
|
12
15
|
|
13
16
|
def spans_years?
|
14
|
-
return false if
|
15
|
-
|
17
|
+
return false if date_to.nil?
|
18
|
+
date_from.year != date_to.year
|
16
19
|
end
|
17
20
|
|
18
21
|
def spans_months?
|
19
|
-
|
22
|
+
date_from.month != date_to.month
|
20
23
|
end
|
21
24
|
|
22
25
|
def within_a_week?
|
23
|
-
(
|
26
|
+
(date_from - date_to) <= 7
|
24
27
|
end
|
25
28
|
|
26
29
|
def one_day?
|
27
|
-
|
30
|
+
date_from == date_to
|
28
31
|
end
|
29
32
|
|
30
33
|
def both_in_current_year?
|
31
34
|
return false if spans_years?
|
32
|
-
|
35
|
+
date_from.year == ::Date.today.year
|
33
36
|
end
|
34
37
|
|
35
38
|
# ---------------------------------------------------------------------
|
@@ -48,9 +51,9 @@ module SemanticDateTimeTags
|
|
48
51
|
# ---------------------------------------------------------------------
|
49
52
|
|
50
53
|
def to_html
|
51
|
-
from = SemanticDateTimeTags::Tag::Date.new(
|
54
|
+
from = SemanticDateTimeTags::Tag::Date.new(date_from, class: 'from').to_html
|
52
55
|
sep = content_tag(:span, separator, class: 'date_range_separator')
|
53
|
-
to = SemanticDateTimeTags::Tag::Date.new(
|
56
|
+
to = SemanticDateTimeTags::Tag::Date.new(date_to, class: 'to').to_html
|
54
57
|
|
55
58
|
content_tag(:span, class: dom_classes) { [ from, sep, to ].join.html_safe }.html_safe
|
56
59
|
end
|
@@ -58,7 +61,7 @@ module SemanticDateTimeTags
|
|
58
61
|
private # =============================================================
|
59
62
|
|
60
63
|
def separator
|
61
|
-
|
64
|
+
options.fetch(:separator, ' – ')
|
62
65
|
end
|
63
66
|
|
64
67
|
end
|
@@ -6,22 +6,22 @@ module SemanticDateTimeTags
|
|
6
6
|
|
7
7
|
def initialize obj, options={}
|
8
8
|
raise 'object must be DateTime' unless obj.instance_of?(::DateTime)
|
9
|
-
super(obj,
|
9
|
+
super(obj, options)
|
10
10
|
end
|
11
11
|
|
12
12
|
# ---------------------------------------------------------------------
|
13
13
|
|
14
14
|
def to_html
|
15
|
-
if
|
16
|
-
datetime =
|
17
|
-
|
15
|
+
if tag_name == :time
|
16
|
+
datetime = obj.acts_like?(:time) ? obj.xmlschema : obj.iso8601
|
17
|
+
options[:datetime] = datetime
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
options[:class] = dom_classes
|
21
21
|
|
22
22
|
value = SemanticDateTimeTags::FormatParser.new(format_string, localized_obj).to_html.html_safe
|
23
23
|
|
24
|
-
time_tag(
|
24
|
+
time_tag(obj, options) { value }.html_safe
|
25
25
|
end
|
26
26
|
|
27
27
|
private # =============================================================
|
@@ -31,7 +31,7 @@ module SemanticDateTimeTags
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def localized_obj
|
34
|
-
|
34
|
+
obj.strftime I18n.t( format, scope: scope, locale: I18n.locale )
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
@@ -4,24 +4,25 @@ module SemanticDateTimeTags
|
|
4
4
|
class Tag
|
5
5
|
class Time < Tag
|
6
6
|
|
7
|
-
def initialize obj,
|
7
|
+
def initialize obj, options={}
|
8
8
|
raise 'object must be Time' unless obj.instance_of?(::Time)
|
9
|
-
super(obj,
|
9
|
+
super(obj, options)
|
10
10
|
end
|
11
11
|
|
12
12
|
# ---------------------------------------------------------------------
|
13
13
|
|
14
14
|
def to_html
|
15
|
-
if
|
16
|
-
datetime =
|
17
|
-
|
15
|
+
if tag_name == :time
|
16
|
+
datetime = obj.acts_like?(:time) ? obj.xmlschema : obj.iso8601
|
17
|
+
options[:datetime] = datetime
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
options[:class] = dom_classes
|
21
|
+
options[:data] = dom_data
|
21
22
|
|
22
23
|
value = SemanticDateTimeTags::FormatParser.new(format_string, localized_obj).to_html
|
23
24
|
|
24
|
-
content_tag(
|
25
|
+
content_tag(tag_name, options) { value }.html_safe
|
25
26
|
end
|
26
27
|
|
27
28
|
private # =============================================================
|
@@ -23,18 +23,18 @@ module SemanticDateTimeTags
|
|
23
23
|
end
|
24
24
|
|
25
25
|
# accepts only datetime
|
26
|
-
def semantic_date_time_tag date_time,
|
26
|
+
def semantic_date_time_tag date_time, options={}
|
27
27
|
SemanticDateTimeTags::Tag::DateTime.new(date_time, options).to_html
|
28
28
|
end
|
29
29
|
|
30
30
|
# accepts datetime and date
|
31
|
-
def semantic_date_tag date,
|
32
|
-
SemanticDateTimeTags::Tag::Date.new(date,
|
31
|
+
def semantic_date_tag date, options={}
|
32
|
+
SemanticDateTimeTags::Tag::Date.new(date, options).to_html
|
33
33
|
end
|
34
34
|
|
35
35
|
# accepts datetime and time
|
36
|
-
def semantic_time_tag time,
|
37
|
-
SemanticDateTimeTags::Tag::Time.new(time,
|
36
|
+
def semantic_time_tag time, options={}
|
37
|
+
SemanticDateTimeTags::Tag::Time.new(time, options).to_html
|
38
38
|
end
|
39
39
|
|
40
40
|
end
|
@@ -13,14 +13,18 @@ describe SemanticDateTimeTags::ViewHelpers do
|
|
13
13
|
|
14
14
|
describe '#semantic_time_tag' do
|
15
15
|
let(:time_object_hours) { time_object.strftime('%H') }
|
16
|
+
let(:time_object_midnight) { Time.new(2015, 11, 3, 24, 00) }
|
16
17
|
let(:time_object_minutes) { time_object.strftime('%M') }
|
18
|
+
let(:time_object_noon) { Time.new(2015, 11, 3, 12, 00) }
|
17
19
|
let(:time_object_whole_hour) { Time.new(2014, 8, 21, 15) }
|
18
20
|
let(:time_object_whole_minute) { Time.new(2014, 8, 21, 15, 30) }
|
19
21
|
|
22
|
+
|
20
23
|
it 'does not work with a date object' do
|
21
24
|
proc { semantic_time_tag(date_object) }.must_raise RuntimeError
|
22
25
|
end
|
23
26
|
|
27
|
+
|
24
28
|
it 'returns hours wrapped in a span tag' do
|
25
29
|
semantic_time_tag(time_object).must_match Regexp.new("<span.+?hours.+?H.+?>#{time_object_hours}</span>")
|
26
30
|
end
|
@@ -29,14 +33,16 @@ describe SemanticDateTimeTags::ViewHelpers do
|
|
29
33
|
semantic_time_tag(time_object).must_match Regexp.new("<span.+?minutes.+?M.+?>#{time_object_minutes}</span>")
|
30
34
|
end
|
31
35
|
|
36
|
+
|
32
37
|
it 'wraps the whole thing in a time tag by default' do
|
33
38
|
semantic_time_tag(time_object).must_match /\A<time.+?<\/time>\z/
|
34
39
|
end
|
35
40
|
|
36
41
|
it 'wraps the whole thing in a span tag if passed as argument' do
|
37
|
-
semantic_time_tag(time_object, :span).must_match /\A<span.+?<\/span>\z/
|
42
|
+
semantic_time_tag(time_object, { tag_name: :span }).must_match /\A<span.+?<\/span>\z/
|
38
43
|
end
|
39
44
|
|
45
|
+
|
40
46
|
it 'adds whole_hour class if time is whole hour' do
|
41
47
|
semantic_time_tag(time_object_whole_hour).must_match /\A<time.+?whole_hour.+?<\/time>\z/
|
42
48
|
end
|
@@ -45,8 +51,27 @@ describe SemanticDateTimeTags::ViewHelpers do
|
|
45
51
|
semantic_time_tag(time_object_whole_minute).must_match /\A<time.+?whole_minute.+?<\/time>\z/
|
46
52
|
end
|
47
53
|
|
54
|
+
|
55
|
+
it 'adds noon class if time is noon' do
|
56
|
+
semantic_time_tag(time_object_noon).must_match /\A<time.+?noon.+?<\/time>\z/
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'adds midnight class if time is midnight' do
|
60
|
+
semantic_time_tag(time_object_midnight).must_match /\A<time.+?midnight.+?<\/time>\z/
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
it 'adds noon as data-in-words if time is noon' do
|
65
|
+
semantic_time_tag(time_object_noon).must_match /\A<time.+?data-in-words=\"noon\".+?<\/time>\z/
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'adds midnight as data-in-words if time is midnight' do
|
69
|
+
semantic_time_tag(time_object_midnight).must_match /\A<time.+?data-in-words=\"midnight\".+?<\/time>\z/
|
70
|
+
end
|
71
|
+
|
72
|
+
|
48
73
|
it 'allows to pass :format' do
|
49
|
-
semantic_time_tag(time_object,
|
74
|
+
semantic_time_tag(time_object, format: :test).must_include '~'
|
50
75
|
end
|
51
76
|
end
|
52
77
|
|
@@ -57,24 +82,28 @@ describe SemanticDateTimeTags::ViewHelpers do
|
|
57
82
|
let(:date_object_month) { date_object.strftime('%-m') }
|
58
83
|
let(:date_object_year) { date_object.year }
|
59
84
|
|
85
|
+
|
60
86
|
it 'should only work with a date or datetime object' do
|
61
87
|
proc { semantic_date_tag(time_object) }.must_raise RuntimeError
|
62
88
|
end
|
63
89
|
|
90
|
+
|
64
91
|
it 'wraps everything in a time tag by default' do
|
65
92
|
semantic_date_tag(date_object).must_match /\A<time.+?<\/time>\z/
|
66
93
|
end
|
67
94
|
|
68
95
|
it 'wraps everything in a span tag if passed as argument' do
|
69
|
-
semantic_date_tag(date_object, :span).must_match /\A<span.+?<\/span>\z/
|
96
|
+
semantic_date_tag(date_object, { tag_name: :span }).must_match /\A<span.+?<\/span>\z/
|
70
97
|
end
|
71
98
|
|
99
|
+
|
72
100
|
it 'returns year, month and day wrapped in a span tags' do
|
73
101
|
semantic_date_tag(date_object).must_match Regexp.new("<span.+?year.+?>#{date_object_year}</span>")
|
74
102
|
semantic_date_tag(date_object).must_match Regexp.new("<span.+?month.+?>#{date_object_month}</span>")
|
75
103
|
semantic_date_tag(date_object).must_match Regexp.new("<span.+?day.+?>#{date_object_day}</span>")
|
76
104
|
end
|
77
105
|
|
106
|
+
|
78
107
|
it 'adds current_date class if date is today' do
|
79
108
|
semantic_date_tag(Date.today).must_include "current_date"
|
80
109
|
semantic_date_tag(Date.today-1.day).wont_include "current_date"
|
@@ -85,8 +114,9 @@ describe SemanticDateTimeTags::ViewHelpers do
|
|
85
114
|
semantic_date_tag(Date.today-1.year).wont_include "current_year"
|
86
115
|
end
|
87
116
|
|
117
|
+
|
88
118
|
it 'allows to pass :format' do
|
89
|
-
semantic_date_tag(Date.today,
|
119
|
+
semantic_date_tag(Date.today, format: :test).must_include '~'
|
90
120
|
end
|
91
121
|
end
|
92
122
|
|
@@ -102,7 +132,7 @@ describe SemanticDateTimeTags::ViewHelpers do
|
|
102
132
|
end
|
103
133
|
|
104
134
|
it 'allows to pass :format' do
|
105
|
-
semantic_date_time_tag(date_time_object, false, format: :test).must_include '~'
|
135
|
+
semantic_date_time_tag(date_time_object, all_day: false, format: :test).must_include '~'
|
106
136
|
end
|
107
137
|
end
|
108
138
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semantic_date_time_tags
|
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
|
- Tomas Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|