coaster 1.4.6 → 1.4.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/coaster/core_ext/month.rb +42 -16
- data/lib/coaster/core_ext/require_more.rb +1 -1
- data/lib/coaster/serialized_properties.rb +1 -1
- data/lib/coaster/version.rb +1 -1
- data/test/test_month.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41ba0c483410b98c561f02ad57afa970110a369b35c0cb407b9bde9e5a333013
|
4
|
+
data.tar.gz: 651ba61b273861c59b6ed676a4e85a88ada811ca1daa59282075dde6a1bf7bce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 864c6e513179cae78a768acc6335752c5dba18f4bd6110ea58613b0b51dc0ad5c19ff3cfcf7171133f1db8041b09cabc196dc976113451cefc51ad5cd1d7317c
|
7
|
+
data.tar.gz: 3b067f4ae39fd0bb24f91b0dc0f2ce813b57fe8e132c842f6b9023a860c91edcdd99f54e925aeb4f94837cea80101e2043d370b653cefede3a7b11a714391ecd
|
@@ -1,23 +1,26 @@
|
|
1
1
|
require 'attr_extras' # gem
|
2
2
|
|
3
3
|
class Month
|
4
|
-
|
4
|
+
attr_reader :_year, :_month
|
5
|
+
attr_reader :timezone
|
5
6
|
|
6
7
|
class << self
|
7
|
-
def from(object)
|
8
|
+
def from(object, timezone: nil)
|
8
9
|
case object
|
9
|
-
when Month
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
when Month
|
11
|
+
object.timezone = timezone
|
12
|
+
object
|
13
|
+
when String then Month.parse(object, timezone: timezone)
|
14
|
+
when Array then Month.new(object[0], object[1], timezone: timezone)
|
15
|
+
else new(object.year, object.month, timezone: timezone)
|
13
16
|
end
|
14
17
|
end
|
15
18
|
|
16
19
|
# Month.parse('201601')
|
17
20
|
# Month.parse('2016-01')
|
18
|
-
def parse(str)
|
21
|
+
def parse(str, timezone: nil)
|
19
22
|
date = Date.parse(str)
|
20
|
-
from(date)
|
23
|
+
from(date, timezone: timezone)
|
21
24
|
rescue ArgumentError => e
|
22
25
|
if str.instance_variable_defined?(:@_gsub_) && str.instance_variable_get(:@_gsub_)
|
23
26
|
raise e, str: str.instance_variable_get(:@_gsub_)
|
@@ -28,7 +31,7 @@ class Month
|
|
28
31
|
str_gsub.insert(4, '0') if str_gsub.length == 5
|
29
32
|
str_gsub += '01'
|
30
33
|
str_gsub.instance_variable_set(:@_gsub_, str_gsub)
|
31
|
-
parse(str_gsub)
|
34
|
+
parse(str_gsub, timezone: timezone)
|
32
35
|
end
|
33
36
|
|
34
37
|
def current
|
@@ -40,6 +43,17 @@ class Month
|
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
46
|
+
def initialize(year, month, timezone: nil)
|
47
|
+
@_year = year
|
48
|
+
@_month = month
|
49
|
+
self.timezone = timezone
|
50
|
+
end
|
51
|
+
|
52
|
+
def timezone=(tz)
|
53
|
+
tz = ActiveSupport::TimeZone[tz] if tz.is_a?(String)
|
54
|
+
@timezone = tz || Time.zone
|
55
|
+
end
|
56
|
+
|
43
57
|
def year
|
44
58
|
Integer(@_year)
|
45
59
|
end
|
@@ -49,11 +63,11 @@ class Month
|
|
49
63
|
end
|
50
64
|
|
51
65
|
def first_date
|
52
|
-
Date.new(year, month, 1)
|
66
|
+
@first_date ||= Date.new(year, month, 1)
|
53
67
|
end
|
54
68
|
|
55
69
|
def last_date
|
56
|
-
Date.new(year, month, -1)
|
70
|
+
@last_date ||= Date.new(year, month, -1)
|
57
71
|
end
|
58
72
|
|
59
73
|
def each_date(&block)
|
@@ -69,11 +83,11 @@ class Month
|
|
69
83
|
end
|
70
84
|
|
71
85
|
def beginning_of_month
|
72
|
-
first_date.
|
86
|
+
first_date.in_time_zone(timezone)
|
73
87
|
end
|
74
88
|
|
75
89
|
def end_of_month
|
76
|
-
last_date.end_of_day
|
90
|
+
last_date.in_time_zone(timezone).end_of_day
|
77
91
|
end
|
78
92
|
|
79
93
|
def date_for_day(number)
|
@@ -93,16 +107,28 @@ class Month
|
|
93
107
|
end
|
94
108
|
|
95
109
|
def to_s
|
96
|
-
|
110
|
+
first_date.strftime('%Y-%m')
|
97
111
|
end
|
98
112
|
alias_method :inspect, :to_s
|
99
113
|
|
100
114
|
def -(time)
|
101
|
-
|
115
|
+
case time
|
116
|
+
when ActiveSupport::Duration then Month.from(first_date.in_time_zone(timezone) - time)
|
117
|
+
else
|
118
|
+
Month.from(first_date - time)
|
119
|
+
end
|
102
120
|
end
|
103
121
|
|
104
122
|
def +(time)
|
105
|
-
|
123
|
+
case time
|
124
|
+
when ActiveSupport::Duration then Month.from(first_date.in_time_zone(timezone) + time)
|
125
|
+
else
|
126
|
+
Month.from(first_date + time)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def cover?(t)
|
131
|
+
to_time_range.cover?(t)
|
106
132
|
end
|
107
133
|
|
108
134
|
include Comparable
|
@@ -106,7 +106,7 @@ module Coaster
|
|
106
106
|
_define_serialized_property(serialize_column, key, default: default || [])
|
107
107
|
elsif type.respond_to?(:serialized_property_serializer) && (serializer = type.serialized_property_serializer)
|
108
108
|
_define_serialized_property(serialize_column, key, getter: serializer[:getter], setter: serializer[:setter], setter_callback: serializer[:setter_callback], default: default)
|
109
|
-
elsif type && type < ActiveRecord::Base
|
109
|
+
elsif type.is_a?(Class) && type < ActiveRecord::Base
|
110
110
|
_define_serialized_property serialize_column, "#{key}_id", default: default
|
111
111
|
|
112
112
|
define_method key.to_sym do
|
data/lib/coaster/version.rb
CHANGED
data/test/test_month.rb
CHANGED
@@ -3,12 +3,23 @@ require 'minitest/autorun'
|
|
3
3
|
|
4
4
|
module Coaster
|
5
5
|
class TestMonth < Minitest::Test
|
6
|
+
def setup
|
7
|
+
Time.zone = 'Asia/Seoul'
|
8
|
+
end
|
9
|
+
|
6
10
|
def test_month
|
7
11
|
m = Month.parse('202001')
|
8
12
|
assert_equal m.year, 2020
|
9
13
|
assert_equal m.last_date, Date.parse('20200131')
|
10
14
|
assert_equal m.end_of_month, Date.parse('20200131').end_of_day
|
11
15
|
assert_equal m.to_time_range, Date.parse('20200101').beginning_of_day...Date.parse('20200201').beginning_of_day
|
16
|
+
assert_equal m.to_s, '2020-01'
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_timezone
|
20
|
+
m = Month.parse('202001', timezone: 'Pacific/Midway')
|
21
|
+
assert_equal m.timezone, ActiveSupport::TimeZone['Pacific/Midway']
|
22
|
+
assert_equal m.beginning_of_month, Date.parse('20200101').in_time_zone('Pacific/Midway')
|
12
23
|
end
|
13
24
|
|
14
25
|
def test_next_specific_date
|
@@ -24,11 +35,13 @@ module Coaster
|
|
24
35
|
assert_equal d.next_specific_date(29), Date.parse('20200229')
|
25
36
|
d = Date.parse('20200210')
|
26
37
|
assert_equal d.next_specific_date(29), Date.parse('20200229')
|
38
|
+
assert_equal d.next_specific_date(30), Date.parse('20200229')
|
27
39
|
assert_equal d.next_specific_date(31), Date.parse('20200229')
|
28
40
|
d = Date.parse('20210210')
|
29
41
|
assert_equal d.next_specific_date(9), Date.parse('20210309')
|
30
42
|
assert_equal d.next_specific_date(10), Date.parse('20210310')
|
31
43
|
assert_equal d.next_specific_date(29), Date.parse('20210228')
|
44
|
+
assert_equal d.next_specific_date(30), Date.parse('20210228')
|
32
45
|
assert_equal d.next_specific_date(31), Date.parse('20210228')
|
33
46
|
d = Date.parse('20210228')
|
34
47
|
assert_equal d.next_specific_date(31), Date.parse('20210331')
|
@@ -49,5 +62,11 @@ module Coaster
|
|
49
62
|
assert_equal d.prev_specific_date(30), Date.parse('20200130')
|
50
63
|
assert_equal d.prev_specific_date(31), Date.parse('20200131')
|
51
64
|
end
|
65
|
+
|
66
|
+
def test_range
|
67
|
+
mn = Month.now
|
68
|
+
range = (mn..mn)
|
69
|
+
assert range.cover?(Month.from(Date.today))
|
70
|
+
end
|
52
71
|
end
|
53
72
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coaster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- buzz jung
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|