ifc_date 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6979a62fb8321d9418b5789cc02303e536abcc042350412d538158b979370557
4
- data.tar.gz: aa6d5344f97499fc6f173ba790a615e76507a092eec2b8b3d5af8b0f9af1cb05
3
+ metadata.gz: 8ad433793e0ad036ce9c1ec24d911e51bb6c2bedd3de4bc803e80f45c6fa0816
4
+ data.tar.gz: 8b035067f852ec5af13aa02d7743e3ca4659ed0dcbfe2ba99165de1a8baac1e4
5
5
  SHA512:
6
- metadata.gz: 51e4513597bc76020664126c47fac826e56f048a16ec74897d4afe9f07d38f14b4c856567ebf5e2bc537ad1d8d0bed7f92d3d012473def43f2c0cd7973e0ef7f
7
- data.tar.gz: 5ff49d42a1b8cb58e552617b2ecce0a85341490932e273a24b85a462368b5c3b4e3249c832a7f99307bf9b01daf2ac07d90ee4cc3fb9da21f750368739f31b0d
6
+ metadata.gz: 901f390a464a7d8af4f38874943a25bcae0e7c3eb0ab91ee673e851296e0a9eef5f7bf8853d6e91a54301eded36f887fbd0c3c641a52a9d7fa2a4efc22f4c3e0
7
+ data.tar.gz: 0c945503fff33804c196b06d69fca51cf6a8756c5edfa7bbd5b96da7f71752ea4f5b93b8624f578ebe41a3e75f4a954898ddfb73efd663bc2fe6f01363bf344f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-07-xx
4
+ * Add `to_s` and `inspect` method to return ISO8601 style representation
5
+ * Attach the starday to the week it attaches instead of using zero.
6
+ So '2022-12-31'.to\_date.ifc.week will be 52 instead of 0
7
+ and '2020-06-17'.to\_date.ifc.week will be 24 instead of 0
8
+ * more consistent implementation using the cleaned divmod structure
9
+
3
10
  ## [0.1.0] - 2022-06-08
4
11
 
5
12
  - Initial release
13
+
14
+
15
+
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ifc_date (0.1.0)
4
+ ifc_date (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,13 +9,14 @@ module IfcDateClassMethods
9
9
  # NOTE: only use for explicitly non nil return methods, false is fine
10
10
  def memoize(method_name)
11
11
  unmemoized_name = "_unmemoized_#{method_name}"
12
- instance_var_name = method_name.end_with?('?') ? "@is_#{method_name[0..-2]}" : "@#{method_name}"
12
+ cache_key = method_name.end_with?('?') ? "is_#{method_name[0..-2]}" : "#{method_name}"
13
13
  alias_method unmemoized_name, method_name
14
14
  define_method method_name do |*args, &block|
15
- instance_var = instance_variable_get instance_var_name
16
- return instance_var unless instance_var.nil?
15
+ @_memoized_methods ||= {}
16
+ return @_memoized_methods[cache_key] if @_memoized_methods.has_key?(cache_key)
17
17
  evaluated_result = send(unmemoized_name, *args, &block)
18
- instance_variable_set instance_var_name, evaluated_result
18
+ @_memoized_methods[cache_key] = evaluated_result
19
19
  end
20
20
  end
21
+
21
22
  end
@@ -0,0 +1,11 @@
1
+ module IfcDateInstanceMethods
2
+ # In case a crucial element of the object itself changes the memoization should be cleared.
3
+ # Note that this is not expected since the current way of reasoning assumes new object creation.
4
+ # Maybe something of the future could be:
5
+ # date = Date.today.ifc
6
+ # date.move_forward 3.days
7
+ # But this is for later consideration
8
+ def clear_memoization_results
9
+ @_memoized_methods.clear
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class IfcDate
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/ifc_date.rb CHANGED
@@ -4,12 +4,16 @@ require 'date'
4
4
  require_relative 'ifc_date/version'
5
5
  require_relative 'ifc_date/ext/to_ifc'
6
6
  require_relative 'ifc_date/class_methods'
7
+ require_relative 'ifc_date/instance_methods.rb'
7
8
 
8
9
  class IfcDate
9
10
  class Error < StandardError; end
10
11
  extend IfcDateClassMethods
12
+ include IfcDateInstanceMethods
13
+
11
14
  attr_reader :day_of_year, :year
12
15
  alias yday day_of_year
16
+
13
17
  DAYNAMES = Date::DAYNAMES + ['Starday']
14
18
  ABBR_DAYNAMES = Date::ABBR_DAYNAMES + ['Sta']
15
19
  MONTHNAMES = Date::MONTHNAMES.dup.insert(6, 'Sol')
@@ -32,18 +36,26 @@ class IfcDate
32
36
  self.class.is_leap_year?(year)
33
37
  end
34
38
 
39
+ # This method caches the divmod result of the:
40
+ # zero based (working number starts with 1) day of the year
41
+ # where Leap day is removed. By applying the divmod on the
42
+ # 'cleaner' representation of the day of the year, other parts
43
+ # of the code become easier to reason about.
35
44
  memoize def day_of_year_divmod_seven
36
- if leap_year? && day_of_year > 169
45
+ if leap_year? && day_of_year > 168
37
46
  (day_of_year - 2).divmod(7)
38
47
  else
39
48
  (day_of_year - 1).divmod(7)
40
49
  end
41
50
  end
42
51
 
43
- # day_of_year starts from 1,
44
- # divmod should be zero based
52
+ # This method caches the divmod result of the:
53
+ # zero based (working number starts with 1) day of the year
54
+ # where Leap day is removed. By applying the divmod on the
55
+ # 'cleaner' representation of the day of the year, other parts
56
+ # of the code become easier to reason about.
45
57
  memoize def day_of_year_divmod_twenty_eight
46
- if leap_year? && day_of_year > 169
58
+ if leap_year? && day_of_year > 168
47
59
  (day_of_year - 2).divmod(28)
48
60
  else
49
61
  (day_of_year - 1).divmod(28)
@@ -60,8 +72,8 @@ class IfcDate
60
72
  end
61
73
 
62
74
  memoize def week_of_year
63
- return 0 if starday?
64
- day_of_year_divmod_seven[0] + 1
75
+ # The min operation if for the year day, which would be 53, but is appended to week 52
76
+ [day_of_year_divmod_seven[0] + 1, 52].min
65
77
  end
66
78
  alias week week_of_year
67
79
 
@@ -78,11 +90,10 @@ class IfcDate
78
90
  alias mday day_of_month
79
91
 
80
92
  # Return the month in index of the year.
81
- # NOTE Januari will be 1, so different indexing thant the wday
93
+ # NOTE January will be 1, so different indexing thant the wday
82
94
  def month_of_year
83
- return 13 if day_of_year > 364 # prevent 14
84
- return 6 if leap_year? && day_of_year == 169
85
- day_of_year_divmod_twenty_eight[0] + 1
95
+ # The min operation if for the year day, which would be 14, but is appended to month 13
96
+ [day_of_year_divmod_twenty_eight[0] + 1, 13].min
86
97
  end
87
98
  alias month month_of_year
88
99
 
@@ -101,4 +112,9 @@ class IfcDate
101
112
  def abbr_month_name
102
113
  ABBR_MONTHNAMES[month_of_year]
103
114
  end
115
+
116
+ def inspect
117
+ "#{year.to_s}-#{month_of_year.to_s.rjust(2, '0')}-#{day_of_month.to_s}"
118
+ end
119
+ alias to_s inspect
104
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ifc_date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin ter Kuile
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-08 00:00:00.000000000 Z
11
+ date: 2022-07-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby implementation of the International Fixed Calendar system. Other
14
14
  name references are Cotsworth and Eastman
@@ -28,6 +28,7 @@ files:
28
28
  - lib/ifc_date.rb
29
29
  - lib/ifc_date/class_methods.rb
30
30
  - lib/ifc_date/ext/to_ifc.rb
31
+ - lib/ifc_date/instance_methods.rb
31
32
  - lib/ifc_date/version.rb
32
33
  - sig/ifc_date.rbs
33
34
  homepage: https://gitlab.com/benja-2/ifc_date