-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Publication ranges #5
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class Lisbn < String | ||
class PublicationRange | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mechanism is a bit hard to understand, but I think
and
Is it easier to implement and think about by manipulating the parts (as a string), rather than doing the integer multiplication approach? |
||
include Enumerable | ||
def initialize(seed_isbn13) | ||
@seed_isbn13 = Lisbn.new(seed_isbn13) | ||
end | ||
|
||
def parts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming of this method is confusing. Maybe |
||
seed_isbn13.parts[0..2] | ||
end | ||
|
||
def prefix | ||
parts.join | ||
end | ||
|
||
def to_s | ||
"#{Lisbn.new(from).isbn_with_dash}..#{Lisbn.new(to).isbn_with_dash}" | ||
end | ||
|
||
def number_of_publications | ||
10 ** (13 - prefix.size - 1) | ||
end | ||
|
||
def publication_numbers | ||
(0..(number_of_publications-1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be best to add spaces around the |
||
end | ||
|
||
def each | ||
(from_prefix..to_prefix).each do |isbn| | ||
yield Lisbn.new((isbn*10).to_s).isbn13_checksum_corrected | ||
end | ||
end | ||
|
||
private | ||
attr_reader :seed_isbn13 | ||
|
||
def multiplier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method seems to have the same return value as the |
||
(10 ** (12 - prefix.size)) | ||
end | ||
|
||
def from_prefix | ||
prefix.to_i * multiplier | ||
end | ||
|
||
def to_prefix | ||
(prefix.to_i+1) * multiplier - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around operator here too. |
||
end | ||
|
||
def from | ||
Lisbn.new((from_prefix*10).to_s).isbn13_checksum_corrected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around operator here too. |
||
end | ||
|
||
def to | ||
Lisbn.new((to_prefix*10).to_s).isbn13_checksum_corrected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around operator here too. |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like most of the methods use
isbn_13
rather thanisbn13
. My preference would be to standardize onisbn_13
(andisbn_10
), and to open a separate PR to rename the anomalousisbn13
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better name for this might also be something like
isbn_13_with_corrected_checksum
that makes clear that an ISBN is being returned, and not a checksum.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed