timeliness 0.5.1 → 0.5.2

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: 8a3c28fd75e1bedf4e7679a8fd71b9484a5daadbf7dceea15d7f54fb7f5304c3
4
- data.tar.gz: 3bc44bf2be43bba84c55ca5eb37f2ba97c8fa4051d10bd30477a307aab1605e9
3
+ metadata.gz: 6fd0587977d3e4e37b368cb52cf19aa81f61f92bd476b5ee5b75ca077179797e
4
+ data.tar.gz: 69fb540b605ad50751253181acd7c391b9d72d43fb0337f7246a9197c90e2717
5
5
  SHA512:
6
- metadata.gz: 79b6996ca7dce5c043b0dfecf11c2d9cd1016ce5784d134576123f23de9ac8bdc2a4ce0905eda58949d31f03c79e89c37622bdd0e926e70a6e189af265eb5b24
7
- data.tar.gz: 321feb85cf0177e55ed47e5a35026b85610ba8559ae03daca287f5d6bbcc5f1e4d287ae8212267e22049907adef252415b528def599efbaafecdd34bb7d8ad86
6
+ metadata.gz: 34cefe7db667f7e4eee4440ac6806e866b38176bb47b0d006c71562031fdb47c4a6e29b111178ba05e8008553f7c64577d6300c6cf711fba277256e5fd7443f5
7
+ data.tar.gz: 52dfc42917b78c105ff1e70b9523444abbb5c6c03b11d58e03d95e598959edf9f16eef3e2e69cfa9a3bac1112c1697c8f553dedd078e7d0ddf5e9d54abf11e8c
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Timeliness
2
4
  class Format
3
5
  include Helpers
@@ -11,7 +13,6 @@ module Timeliness
11
13
  end
12
14
 
13
15
  def compile!
14
- @token_count = 0
15
16
  found_tokens, token_order = [], []
16
17
 
17
18
  format = format_string.dup
@@ -19,20 +20,16 @@ module Timeliness
19
20
 
20
21
  # Substitute tokens with numbered placeholder
21
22
  Definitions.sorted_token_keys.each do |token|
22
- count = 0
23
23
  format.gsub!(token) do
24
24
  token_regexp_str, arg_key = Definitions.format_tokens[token]
25
- token_index = found_tokens.size
26
25
 
27
- if arg_key
26
+ if arg_key && found_tokens.rassoc(arg_key)
28
27
  raise CompilationFailed, "Token '#{token}' was found more than once in format '#{format_string}'. This has unexpected effects should be removed." if count > 0
29
- count += 1
30
-
31
- token_regexp_str = "(#{token_regexp_str})"
32
- @token_count += 1
33
28
  end
29
+
34
30
  found_tokens << [ token_regexp_str, arg_key ]
35
31
 
32
+ token_index = found_tokens.size - 1
36
33
  "%<#{token_index}>"
37
34
  end
38
35
  end
@@ -40,11 +37,18 @@ module Timeliness
40
37
  # Replace placeholders with token regexps
41
38
  format.gsub!(/%<(\d+)>/) do
42
39
  token_regexp_str, arg_key = found_tokens[$1.to_i]
43
- token_order << arg_key
44
- token_regexp_str
40
+
41
+ if arg_key
42
+ token_order << arg_key
43
+ "(#{token_regexp_str})"
44
+ else
45
+ token_regexp_str
46
+ end
45
47
  end
46
48
 
47
- define_process_method(token_order.compact)
49
+ @token_count = token_order.size
50
+
51
+ define_process_method(token_order)
48
52
  @regexp_string = format
49
53
  @regexp = Regexp.new("^(?>#{format})$")
50
54
  self
@@ -31,12 +31,12 @@ module Timeliness
31
31
 
32
32
  def match(string, format_string=nil)
33
33
  format = single_format(format_string) if format_string
34
- match_regexp = format && format.regexp || @regexp
34
+ match_regexp = format ? format.regexp : @regexp
35
35
 
36
- match_regexp.match(string) do |match_data|
37
- captures = match_data.captures # For a multi-format regexp there are lots of nils
38
- index = captures.find_index { |e| !e.nil? } # Find the start of captures for matched format
39
- values = captures.values_at(index..(index+7))
36
+ if (match_data = match_regexp.match(string))
37
+ captures = match_data.captures # For a multi-format regexp there are lots of nils
38
+ index = captures.index { |e| !e.nil? } # Find the start of captures for matched format
39
+ values = captures[index, 8]
40
40
  format ||= @match_indexes[index]
41
41
  format.process(*values)
42
42
  end
@@ -1,10 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Timeliness
2
4
  module Helpers
5
+ # Helper methods used in format component processing. See Definitions.
3
6
 
4
7
  def full_hour(hour, meridian)
5
8
  hour = hour.to_i
6
9
  return hour if meridian.nil?
7
- if meridian.delete('.').downcase == 'am'
10
+
11
+ meridian.delete!('.')
12
+ meridian.downcase!
13
+
14
+ if meridian == 'am'
8
15
  raise(ArgumentError) if hour == 0 || hour > 12
9
16
  hour == 12 ? 0 : hour
10
17
  else
@@ -20,9 +27,9 @@ module Timeliness
20
27
  end
21
28
  year.to_i
22
29
  end
23
-
30
+
24
31
  def month_index(month)
25
- return month.to_i if month.to_i > 0 || /0+/ =~ month
32
+ return month.to_i if month.match?(/\d/)
26
33
  (month.length > 3 ? month_names : abbr_month_names).index { |str| month.casecmp?(str) }
27
34
  end
28
35
 
@@ -39,10 +46,8 @@ module Timeliness
39
46
  end
40
47
 
41
48
  def offset_in_seconds(offset)
42
- sign = offset =~ /^-/ ? -1 : 1
43
- parts = offset.scan(/\d\d/).map {|p| p.to_f }
44
- parts[1] = parts[1].to_f / 60
45
- (parts[0] + parts[1]) * sign * 3600
49
+ offset =~ /^([-+])?(\d{2}):?(\d{2})/
50
+ ($1 == '-' ? -1 : 1) * ($2.to_f * 3600 + $3.to_f)
46
51
  end
47
52
 
48
53
  def i18n_loaded?
@@ -18,19 +18,20 @@ module Timeliness
18
18
 
19
19
  default_values_by_type(time_array, type, options) unless type == :datetime
20
20
 
21
- make_time(time_array[0..7], options[:zone])
21
+ make_time(time_array, options[:zone])
22
22
  rescue NoMethodError => ex
23
23
  raise ex unless ex.message =~ /undefined method `(zone|use_zone|current)' for Time:Class/
24
24
  raise MissingTimezoneSupport, "ActiveSupport timezone support must be loaded to use timezones other than :utc and :local."
25
25
  end
26
26
 
27
27
  def make_time(time_array, zone_option=nil)
28
- return nil unless fast_date_valid_with_fallback(*time_array[0..2])
28
+ return nil unless fast_date_valid_with_fallback(time_array[0], time_array[1], time_array[2])
29
29
 
30
30
  zone, offset = zone_and_offset(time_array[7]) if time_array[7]
31
31
 
32
32
  value = create_time_in_zone(time_array[0..6].compact, zone || zone_option)
33
33
  value = shift_time_to_zone(value, zone_option) if zone
34
+
34
35
  return nil unless value
35
36
 
36
37
  offset ? value + (value.utc_offset - offset) : value
@@ -74,9 +75,12 @@ module Timeliness
74
75
  def default_values_by_type(values, type, options)
75
76
  case type
76
77
  when :date
77
- values[3..7] = nil
78
+ values.fill(nil, 3..7)
78
79
  when :time
79
- values[0..2] = current_date(options)
80
+ current_date = current_date(options)
81
+ values[0] = current_date[0]
82
+ values[1] = current_date[1]
83
+ values[2] = current_date[2]
80
84
  when nil
81
85
  dummy_date = current_date(options)
82
86
  values[0] ||= dummy_date[0]
@@ -1,3 +1,3 @@
1
1
  module Timeliness
2
- VERSION = '0.5.1'.freeze
2
+ VERSION = '0.5.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timeliness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-07 00:00:00.000000000 Z
11
+ date: 2025-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport