reading 1.1.0 → 1.1.1

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: 2895207e57d29e0056fac8eca5ddecf39853916bb3691df1c6adb2d657d9c709
4
- data.tar.gz: 115aa06d704b41f03aeaf8b39fcfcc2bc4dc3f3ee49c232e644903a5b098bd8c
3
+ metadata.gz: 4bf09fbcc2e77924a371eade404c83ad4fd5c3d7576eac45d0c1236c3db479f6
4
+ data.tar.gz: f854509f8ae88553df964428d18098fceeb3c885093728c2f54bba530199c8cb
5
5
  SHA512:
6
- metadata.gz: 865dd1537a65a89e253f8b98ad82e6e15e3f21bd76467db5c06297575004ecc9f5a63cf766c339e61b143c478c1a2e7a6873bd370a57ef609469b070e243214f
7
- data.tar.gz: 4ca3fe68262114c9791973c9b843fe2c33c6f96d99f0e2566f58ac5ca97c3196a772f84eaebc2db5665010ef11aa10d500f430a4496ab7b204cc8554f8826312
6
+ metadata.gz: a285b194b566697bd586628d1266ed5582e1cfab48dc3023e243635e4b21b3f31f100cf4b41a1777358b70175c15a8b2d57d7589859697c917e1914f452e492e
7
+ data.tar.gz: 6aa82f83175a22cd652b545aa46935ba5ef72009d33b8bbcd35a6667e1c2081cbe708fce40703503027aaf5938c5020a475fea9575d6e5d45fe75bef49233b4e
@@ -2,7 +2,7 @@ module Reading
2
2
  class Item
3
3
  # The length of an item when it is a time, as opposed to pages. (Pages are
4
4
  # represented simply with an Integer or Float.)
5
- class Item::TimeLength
5
+ class TimeLength
6
6
  include Comparable
7
7
 
8
8
  attr_reader :value # in total minutes
@@ -12,17 +12,17 @@ module Reading
12
12
  @value = value
13
13
  end
14
14
 
15
- # Builds an Item::TimeLength from a string.
15
+ # Builds a TimeLength from a string.
16
16
  # @param string [String] a time duration in "h:mm" format.
17
17
  # @return [TimeLength, nil]
18
18
  def self.parse(string)
19
- return nil unless string.match? /\A\d+:\d\d\z/
19
+ return nil unless string.match?(/\A\d+:\d\d\z/)
20
20
 
21
21
  hours, minutes = string.split(":").map(&:to_i)
22
22
  new((hours * 60) + minutes)
23
23
  end
24
24
 
25
- # Builds an Item::TimeLength based on a page count.
25
+ # Builds a TimeLength based on a page count.
26
26
  # @param pages [Integer, Float]
27
27
  # @return [TimeLength]
28
28
  def self.from_pages(pages)
@@ -99,24 +99,24 @@ module Reading
99
99
  # @param other [TimeLength, Numeric]
100
100
  # @return [TimeLength]
101
101
  def +(other)
102
- if other.is_a? Item::TimeLength
102
+ if other.is_a? TimeLength
103
103
  self.class.new(value + other.value)
104
104
  elsif other.is_a? Numeric
105
105
  self.class.new(value + self.class.pages_to_minutes(other))
106
106
  else
107
- raise TypeError, "#{other.class} can't be added to Item::TimeLength."
107
+ raise TypeError, "#{other.class} can't be added to TimeLength."
108
108
  end
109
109
  end
110
110
 
111
111
  # @param other [TimeLength, Numeric]
112
112
  # @return [TimeLength]
113
113
  def -(other)
114
- if other.is_a? Item::TimeLength
114
+ if other.is_a? TimeLength
115
115
  self.class.new(value - other.value)
116
116
  elsif other.is_a? Numeric
117
117
  self.class.new(value - self.class.pages_to_minutes(other))
118
118
  else
119
- raise TypeError, "#{other.class} can't be subtracted from Item::TimeLength."
119
+ raise TypeError, "#{other.class} can't be subtracted from TimeLength."
120
120
  end
121
121
  end
122
122
 
@@ -140,6 +140,17 @@ module Reading
140
140
  end
141
141
  end
142
142
 
143
+ # For relativizing progress to a percentage of amount.
144
+ # @param other [TimeLength, Numeric]
145
+ # @return [TimeLength]
146
+ def percentage_of(other)
147
+ if other.is_a? TimeLength
148
+ value.to_f / other.value
149
+ else
150
+ raise TypeError, "TimeLength can't be percent-divided by #{other.class}."
151
+ end
152
+ end
153
+
143
154
  # See https://web.archive.org/web/20221206095821/https://www.mutuallyhuman.com/blog/class-coercion-in-ruby/
144
155
  # @param other [Numeric]
145
156
  def coerce(other)
@@ -159,7 +170,7 @@ module Reading
159
170
  other = self.class.from_pages(other)
160
171
  end
161
172
 
162
- unless other.is_a? Item::TimeLength
173
+ unless other.is_a? TimeLength
163
174
  raise TypeError, "TimeLength can't be compared to #{other.class} #{other}."
164
175
  end
165
176
 
@@ -90,14 +90,28 @@ module Reading
90
90
  Attributes::Shared.length(parsed_row[:length], format:)
91
91
  no_end_date = !dates.end if dates &&
92
92
  Config.hash.fetch(:enabled_columns).include?(:end_dates)
93
+ progress = Attributes::Shared.progress(start_entry, no_end_date:) ||
94
+ Attributes::Shared.progress(parsed_row[:head][head_index]) ||
95
+ (1.0 if end_entry)
96
+ amount =
97
+ if dates && length
98
+ length
99
+ elsif !progress.is_a?(Float)
100
+ progress
101
+ end
102
+
103
+ # Change progress from absolute to relative (percentage) if amount is given.
104
+ if amount && progress && !progress.is_a?(Float)
105
+ amount_time = amount.is_a?(Item::TimeLength) ? amount : Item::TimeLength.from_pages(amount)
106
+ progress_time = progress.is_a?(Item::TimeLength) ? progress : Item::TimeLength.from_pages(progress)
107
+ progress = progress_time.percentage_of(amount_time)
108
+ end
93
109
 
94
110
  [
95
111
  {
96
- dates: dates,
97
- amount: (length if dates),
98
- progress: Attributes::Shared.progress(start_entry, no_end_date:) ||
99
- Attributes::Shared.progress(parsed_row[:head][head_index]) ||
100
- (1.0 if end_entry),
112
+ dates:,
113
+ amount:,
114
+ progress:,
101
115
  name: span_template.fetch(:name),
102
116
  favorite?: span_template.fetch(:favorite?),
103
117
  }.map { |k, v| [k, v || span_template.fetch(k)] }.to_h
@@ -212,15 +212,22 @@ module Reading
212
212
  # is when tracking fixed-length items such as books. See
213
213
  # https://github.com/fpsvogel/reading/blob/main/doc/csv-format.md#history-pages-and-stopping-points-books
214
214
  if !amount && progress
215
- if progress.is_a? Float
215
+ if progress.is_a?(Float)
216
216
  total_length = Attributes::Shared.length(parsed_row[:length], format:)
217
- amount = total_length * progress
217
+ amount = total_length * progress if total_length
218
218
  else
219
219
  amount = progress
220
220
  end
221
221
  amount_from_progress = true
222
222
  end
223
223
 
224
+ # Change progress from absolute to relative (percentage) if amount is given.
225
+ if amount && progress && !progress.is_a?(Float)
226
+ amount_time = amount.is_a?(Item::TimeLength) ? amount : Item::TimeLength.from_pages(amount)
227
+ progress_time = progress.is_a?(Item::TimeLength) ? progress : Item::TimeLength.from_pages(progress)
228
+ progress = progress_time.percentage_of(amount_time)
229
+ end
230
+
224
231
  repetitions = entry[:repetitions]&.to_i
225
232
  frequency = entry[:frequency]
226
233
 
@@ -16,7 +16,7 @@ module Reading
16
16
  hash[:progress_percent]&.to_f&./(100) ||
17
17
  hash[:progress_pages]&.to_i ||
18
18
  hash[:progress_time]&.then { Item::TimeLength.parse(_1) } ||
19
- (0 if hash[:progress_dnf]) ||
19
+ (0.0 if hash[:progress_dnf]) ||
20
20
  (1.0 if hash[:progress_done]) ||
21
21
  (0.0 if no_end_date) ||
22
22
  nil
@@ -1,3 +1,3 @@
1
1
  module Reading
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reading
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felipe Vogel