coaster 1.4.6 → 1.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc563bd833f85761a0dd634b98527069c0c61331a354a9c62e48bbe9dbbf9173
4
- data.tar.gz: 011c8922be26c2e8c5a6c192baf1d1e1d673ebae2e64c3518f1c5d1078d8b130
3
+ metadata.gz: 5e8ae6d7e690ce70628c0687e935bd182eb3257e1b6253790a00cbd3c9ed6fc9
4
+ data.tar.gz: a9d9708ee72f4872184c9c2969b2b414015d48eedd99ee5bfe5881630f93bf25
5
5
  SHA512:
6
- metadata.gz: e32aa85da5169a73b3bb85cfd7757aa14b3a03269700364e9747432b5e1e8b2338ab9607977c575a94bd6239919c158495fa42375df0447b2a8ba429db3b4499
7
- data.tar.gz: d146502d6f5c06d33949822e348a596b77a1fe712e9ae0f5e24493bff450bdc513b478faebbdf06505b737b668d54693377576ab85f0b58cf55457b5ed1e2a04
6
+ metadata.gz: c17c5fe51f8dc29c2961d74918b949a7f0907df3e0fbbc273f75cb60aaf15fc561f51dbc8b17cf53bdc113df173acf7439b47a58a1d2d33ae8310783422328c6
7
+ data.tar.gz: cb22eedcdb827462e4b66beec6426a6b71561230e1fae62d873bd4244910ff1c1baeb176753e11734a7ec8db202d0e38472c1deef075a25fdabec2ea529a1fc0
@@ -1,23 +1,26 @@
1
1
  require 'attr_extras' # gem
2
2
 
3
3
  class Month
4
- vattr_initialize :_year, :_month
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 then object
10
- when String then Month.parse(object)
11
- when Array then Month.new(object[0], object[1])
12
- else new(object.year, object.month)
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.beginning_of_day
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,24 @@ class Month
93
107
  end
94
108
 
95
109
  def to_s
96
- "#{year}-#{month}"
110
+ first_date.strftime('%Y-%m')
97
111
  end
98
112
  alias_method :inspect, :to_s
99
113
 
100
114
  def -(time)
101
- first_date - time
115
+ case time
116
+ when ActiveSupport::Duration then first_date.in_time_zone(timezone) - time
117
+ else
118
+ first_date - time
119
+ end
102
120
  end
103
121
 
104
122
  def +(time)
105
- first_date + time
123
+ case time
124
+ when ActiveSupport::Duration then first_date.in_time_zone(timezone) + time
125
+ else
126
+ first_date + time
127
+ end
106
128
  end
107
129
 
108
130
  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
@@ -1,3 +1,3 @@
1
1
  module Coaster
2
- VERSION = '1.4.6'
2
+ VERSION = '1.4.7'
3
3
  end
data/test/test_month.rb CHANGED
@@ -9,6 +9,13 @@ module Coaster
9
9
  assert_equal m.last_date, Date.parse('20200131')
10
10
  assert_equal m.end_of_month, Date.parse('20200131').end_of_day
11
11
  assert_equal m.to_time_range, Date.parse('20200101').beginning_of_day...Date.parse('20200201').beginning_of_day
12
+ assert_equal m.to_s, '2020-01'
13
+ end
14
+
15
+ def test_timezone
16
+ m = Month.parse('202001', timezone: 'Pacific/Midway')
17
+ assert_equal m.timezone, ActiveSupport::TimeZone['Pacific/Midway']
18
+ assert_equal m.beginning_of_month, Date.parse('20200101').in_time_zone('Pacific/Midway')
12
19
  end
13
20
 
14
21
  def test_next_specific_date
@@ -24,11 +31,13 @@ module Coaster
24
31
  assert_equal d.next_specific_date(29), Date.parse('20200229')
25
32
  d = Date.parse('20200210')
26
33
  assert_equal d.next_specific_date(29), Date.parse('20200229')
34
+ assert_equal d.next_specific_date(30), Date.parse('20200229')
27
35
  assert_equal d.next_specific_date(31), Date.parse('20200229')
28
36
  d = Date.parse('20210210')
29
37
  assert_equal d.next_specific_date(9), Date.parse('20210309')
30
38
  assert_equal d.next_specific_date(10), Date.parse('20210310')
31
39
  assert_equal d.next_specific_date(29), Date.parse('20210228')
40
+ assert_equal d.next_specific_date(30), Date.parse('20210228')
32
41
  assert_equal d.next_specific_date(31), Date.parse('20210228')
33
42
  d = Date.parse('20210228')
34
43
  assert_equal d.next_specific_date(31), Date.parse('20210331')
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.6
4
+ version: 1.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - buzz jung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-24 00:00:00.000000000 Z
11
+ date: 2024-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj