comicinfo 2.0.0 → 2.1.0

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: ed9c12d2685a70526b85e3be44e9524720b7f90bba6548a09541290df18bdbf7
4
- data.tar.gz: 9856df6a2adbc6a7e81381992b7ae7c1fae68f184b47400f68a95fbe9ab7ac63
3
+ metadata.gz: cf3f8e761a19b3c54cba4a86714dbe1ee56de99fa246d631f69a71e7e80960e6
4
+ data.tar.gz: 8a4f1f896f244b4460e8b94c8d9659065e2c150ec5096e2abe26e9362b95d3f9
5
5
  SHA512:
6
- metadata.gz: 6ca41d4b00d7413aec6d2d78297a17c828fcd2ae79f9309b89fe7aebbf575ed2ed4970f09c620d558d89befde9991092a4d629b168675a5724f6602a6dcff49a
7
- data.tar.gz: 329402c1d139d5668651834900a2ba08c463cda8d258b3161fc2a1b73e4b34dcb76d3ea5c6449a1310b8449326c098e0206da750570a4797dc86e0502945fc4b
6
+ metadata.gz: f0d20e28c25196f01aa19619d567cfee972a720e3836091d350af7199b5ca1938f22221de62a902a98ad417844fbb6d19fbaf02600e647e770c4b37289d5f8ec
7
+ data.tar.gz: 7ca01c77acae36624abbc261b3365c1486383e4c893dfe319c02d79ea2a78a44f9e3526e5563377183ed81cb117e6086f1d2d3a03038d16062a328bf75723c3d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.4.6
1
+ 4.0.9
data/CHANGELOG.md CHANGED
@@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [Unreleased]
9
+
10
+ ## [2.1.0] - 2026-11-19
11
+
12
+ ### Added
13
+ - `ComicInfo::Issue#rearranging_pages(order)`: a copy of the issue with its `<Page>` entries and `PageCount` brought up to date after page images were reordered, deleted, or added. Entries follow their image to its new index, deleted pages lose theirs, and new pages get a default entry when the issue has page entries at all
14
+ - `ComicInfo::Issue#page_type(at:)` and `ComicInfo::Issue#setting_page_type(type, at:)`: read and set the type of a page image by its index. A page with no `<Page>` entry is a Story page; setting another type on it adds an entry, and an existing entry keeps its other details
15
+ - `ComicInfo::Page#with_image(image)` and `ComicInfo::Page#with_type(type)`: a copy of a page entry pointing at another image, or with another type
16
+ - `ComicInfo::Issue#filename(template:)`: a file name for the issue from a template such as `{series} #{number} ({year})` ("Saga #1 (2012)"). Missing values are left out along with the decoration around them, `/` and `:` in values become `-`, and `{number:3}` pads the issue number with zeros ("Saga #001")
17
+ - `ComicInfo::Issue#page_filename(template:, page:, of:)`: a file name for one page image, from a template such as `page_{page:3}` or `{series}_{number:3}_{page:4}`. The page number is always at least as wide as the page count, and a template without one gets it appended, so the names keep pages apart and in order
18
+ - `ComicInfo::FilenameTemplate`, which does the filling in, with the tokens it knows in `TOKENS` and `PAGE_TOKENS`
19
+
20
+ ### Changed
21
+ - Bump Ruby version from 3.4.6 to 4.0.9 (`.ruby-version`, `required_ruby_version`, rubocop `TargetRubyVersion`, CI matrix)
22
+ - Update RuboCop to 1.87.0 (supports Ruby 4.0 `TargetRubyVersion`)
23
+ - Corrected the README and `doc/development.md` where they described methods that do not exist (`#character`, `#team`) or the wrong return types (`#genre` returns an array, `CommunityRating` is a `Float`), and dropped already-shipped features from the future list
24
+
8
25
  ## [2.0.0] - 2025-11-10
9
26
 
10
27
  ### Added
data/README.md CHANGED
@@ -4,7 +4,7 @@ A Ruby gem that provides an idiomatic interface for reading and writing ComicInf
4
4
  following the official ComicInfo schema specifications from the
5
5
  [Anansi Project](https://github.com/anansi-project/comicinfo).
6
6
 
7
- ![Ruby](https://img.shields.io/badge/ruby-%3E%3D%203.4.6-ruby.svg)
7
+ ![Ruby](https://img.shields.io/badge/ruby-%3E%3D%204.0.9-ruby.svg)
8
8
  ![Gem Version](https://badge.fury.io/rb/comicinfo.svg)
9
9
  ![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
10
10
 
@@ -78,7 +78,7 @@ puts comic.cover_artist #=> "Ryan Ottley"
78
78
 
79
79
  # Content details
80
80
  puts comic.page_count #=> 20
81
- puts comic.genre #=> "Superhero, Action, Adventure"
81
+ puts comic.genres #=> ["Superhero", "Action", "Adventure"]
82
82
  puts comic.language_iso #=> "en-US"
83
83
  puts comic.format #=> "Digital"
84
84
 
@@ -121,6 +121,26 @@ story_arc = comic.story_arc #=> ["Brand New Day", "Spider-Island"] (sa
121
121
  story_arc_num = comic.story_arc_number #=> ["1", "5"] (same as story_arc_numbers)
122
122
  ```
123
123
 
124
+ ### File Names from Templates
125
+
126
+ Name a comic's file, or its page image files, from its details. The result has no extension.
127
+
128
+ ```ruby
129
+ comic.filename template: '{series} #{number} ({year})' #=> "Saga #1 (2012)"
130
+ comic.filename template: '{series} #{number:3}' #=> "Saga #001"
131
+
132
+ comic.page_filename template: 'page_{page:3}', page: 1, of: 22 #=> "page_001"
133
+ comic.page_filename template: '{series}_{number:3}_{page:4}', page: 12, of: 22 #=> "Saga_001_0012"
134
+ ```
135
+
136
+ Write templates in single quotes: in double quotes Ruby reads `#{number}` as interpolation.
137
+
138
+ - **Tokens**: `{number}` `{publisher}` `{series}` `{title}` `{volume}` `{year}`, and `{page}` in a page template. They are listed in `ComicInfo::FilenameTemplate::TOKENS` and `PAGE_TOKENS`.
139
+ - **Padding**: `{number:3}` and `{page:3}` pad with zeros to that many digits. Only the whole number a value starts with is padded: `1.5` becomes `001.5` and `12AU` becomes `012AU`.
140
+ - **Missing values** are left out along with the decoration around them: `Saga # ()` becomes `Saga`.
141
+ - **`/` and `:`** in a value become `-`, since they cannot be part of a file name.
142
+ - **Page names always sort in page order**: the page number is at least as wide as the page count (page 2 of 10 is `02`), and a template without `{page}` gets the number appended.
143
+
124
144
  ### Working with Pages
125
145
 
126
146
  ```ruby
@@ -136,6 +156,29 @@ page = comic.pages.first
136
156
  puts page.cover? #=> true for FrontCover, BackCover, InnerCover
137
157
  puts page.story? #=> true for Story pages
138
158
  puts page.deleted? #=> true for Deleted pages
159
+
160
+ # A page can carry more than one type (space-separated in the XML)
161
+ puts page.type #=> "FrontCover"
162
+ puts page.types #=> ["FrontCover"]
163
+ puts page.include_type? 'Story' #=> false
164
+ ```
165
+
166
+ ### Changing Pages
167
+
168
+ An issue is not changed in place. These return a changed copy, which you can `save` or turn `to_xml`.
169
+
170
+ ```ruby
171
+ # A page's type, by its image index. A page with no <Page> entry is a Story page.
172
+ comic.page_type at: 0 #=> "FrontCover"
173
+
174
+ # Give a page a type. Its entry keeps everything else; a page with no entry gets one.
175
+ updated = comic.setting_page_type 'Advertisement', at: 4
176
+
177
+ # Bring the <Page> entries and PageCount up to date after the page images were reordered,
178
+ # deleted, or added to. Each item is the index that image had before, or nil for a new image.
179
+ # Here the third page moves to the front, the second is deleted, and a new page is added.
180
+ updated = comic.rearranging_pages [2, 0, nil]
181
+ updated.save 'ComicInfo.xml'
139
182
  ```
140
183
 
141
184
  ### Manga Support
@@ -200,7 +243,10 @@ This gem fully supports the ComicInfo v2.0 XSD schema with all field types:
200
243
  - AgeRating: Various ESRB and international ratings
201
244
 
202
245
  ### Decimal Fields
203
- - CommunityRating: 0.0 to 5.0 range with validation
246
+ - CommunityRating: 0.0 to 5.0 range with validation (a `Float`, `nil` when absent)
247
+
248
+ ### Default Values
249
+ - Missing string fields are `""`, missing integer fields are `-1`, missing enum fields are `"Unknown"`
204
250
 
205
251
  ### Complex Fields
206
252
  - Pages: Array of ComicPageInfo objects with full attribute support
@@ -292,12 +338,13 @@ bundle exec rubocop --autocorrect
292
338
  - **Schema Compliance**: Full ComicInfo v2.0 support
293
339
  - **Field Types**: String, Integer, Decimal, Enum, Boolean, Arrays
294
340
  - **Multi-value Fields**: Both string and array access methods
295
- - **Issue &Page Support**: Complete Issue and nested Page objects
341
+ - **Issue & Page Support**: Complete Issue and nested Page objects
296
342
  - **Error Handling**: Custom exceptions with detailed messages
297
343
  - **Data Export**: JSON, YAML, and XML serialization
298
344
  - **Validation**: Enum values, ranges, type coercion
299
345
  - **Unicode Support**: International characters and special symbols
300
- - **Test Coverage**: 178 comprehensive test cases
346
+ - **File Names**: Comic and page file names from templates
347
+ - **Page Changes**: Set a page's type, and keep page entries in step with reordered, deleted, and added pages
301
348
 
302
349
  ### Planned Features 🚧
303
350
  - **CLI Tool**: Command-line interface for file manipulation
data/doc/development.md CHANGED
@@ -13,13 +13,13 @@ following the official ComicInfo schema specifications from the Anansi Project.
13
13
  ## Code Standards
14
14
 
15
15
  ### Ruby Version
16
- - Minimum required: Ruby 3.4.6+
16
+ - Minimum required: Ruby 4.0.9+
17
17
  - Use modern Ruby features and syntax patterns
18
18
 
19
19
  ### Dependencies
20
- - **nokogiri** (>= 1.18.10): XML parsing and generation
21
- - **rspec** (>= 3.13.1): Testing framework
22
- - **rubocop** (>= 1.81.1): Code linting and formatting
20
+ - **nokogiri** (>= 1.19.3): XML parsing and generation (the only runtime dependency)
21
+ - **rspec**: Testing framework
22
+ - **rubocop** (with `rubocop-performance`, `rubocop-rake`, `rubocop-rspec`): Code linting and formatting
23
23
 
24
24
  ### Code Style
25
25
  - Follow RuboCop rules (configured in `.rubocop.yml`)
@@ -41,6 +41,7 @@ lib/
41
41
  ├─ comicinfo.rb # Main module and autoloads
42
42
  ├─ comicinfo/
43
43
  │ ├─ version.rb # Gem version constant
44
+ │ ├─ filename_template.rb # File names from templates, for Issue#filename and #page_filename
44
45
  │ ├─ issue.rb # Main ComicInfo::Issue class
45
46
  │ ├─ page.rb # ComicInfo::Page class
46
47
  │ ├─ enums.rb # Schema enum definitions
@@ -49,13 +50,21 @@ spec/
49
50
  ├─ spec_helper.rb # RSpec configuration with FixtureHelpers
50
51
  ├─ comic_info_spec.rb # Main module specs
51
52
  ├─ comic_info/
53
+ │ ├─ filename_template_spec.rb # File name template specs
54
+ │ ├─ issue_pages_spec.rb # Changing an issue's page entries
52
55
  │ ├─ issue_spec.rb # ComicInfo::Issue class specs
53
56
  │ └─ page_spec.rb # ComicInfo::Page class specs
54
57
  └─ fixtures/ # XML test fixtures
55
- │ ├─ valid_minimal.xml
56
- │ ├─ valid_complete.xml
57
- │ ├─ invalid_xml.xml
58
- │ └─ edge_cases/
58
+ ├─ valid_minimal.xml
59
+ ├─ valid_complete.xml
60
+ ├─ invalid_xml.xml
61
+ ├─ edge_cases/
62
+ └─ output/ # Scratch space for write specs
63
+ schemas/ # ComicInfo XSD files (1.0, 2.0, 2.1-draft)
64
+ script/
65
+ └─ schema # Downloads the XSD files into schemas/
66
+ sig/
67
+ └─ comicinfo.rbs # RBS signatures
59
68
  ```
60
69
 
61
70
  ## API Design Principles
@@ -73,13 +82,13 @@ comic.count #=> Integer
73
82
  comic.pages #=> Array<ComicInfo::Page>
74
83
  comic.black_and_white #=> String (enum value)
75
84
 
76
- # Multi-value fields have both singular and plural methods
77
- comic.character #=> String (comma-separated)
78
- comic.characters #=> Array<String>
79
- comic.genre #=> String (comma-separated)
80
- comic.genres #=> Array<String>
81
- comic.web #=> String (space-separated URLs)
82
- comic.web_urls #=> Array<String>
85
+ # Multi-value fields have a plural method and a raw data method
86
+ comic.characters #=> Array<String>
87
+ comic.characters_raw_data #=> String (comma-separated)
88
+ comic.genres #=> Array<String>
89
+ comic.genres_raw_data #=> String (comma-separated)
90
+ comic.web_urls #=> Array<String>
91
+ comic.web #=> String (space-separated URLs)
83
92
  ```
84
93
 
85
94
  ### Naming Conventions
@@ -90,10 +99,12 @@ comic.web_urls #=> Array<String>
90
99
  - XML `<BlackAndWhite>` → Ruby `#black_and_white`
91
100
  - XML `<CommunityRating>` → Ruby `#community_rating`
92
101
  - Multi-value fields:
93
- - Singular method returns original string from XML
94
102
  - Plural method returns array of split values
95
- - XML `<Characters>` → Ruby `#character` (string) & `#characters` (array)
96
- - XML `<Teams>` → Ruby `#team` (string) & `#teams` (array)
103
+ - `_raw_data` method returns original string from XML
104
+ - XML `<Characters>` → Ruby `#characters` (array) & `#characters_raw_data` (string)
105
+ - XML `<Teams>` → Ruby `#teams` (array) & `#teams_raw_data` (string)
106
+ - Where the schema element is singular, the singular method is an alias of the plural:
107
+ `#genre`, `#story_arc`, `#story_arc_number` return arrays
97
108
 
98
109
  ### Error Handling
99
110
  - Define custom exception classes in `ComicInfo::Errors`
@@ -138,13 +149,13 @@ bundle exec rspec --format documentation
138
149
 
139
150
  ### Multi-value Fields
140
151
  - Handle comma-separated values in fields like Genre, Characters
141
- - Provide both string and array access methods
152
+ - Provide both string (`_raw_data`) and array (plural) access methods
142
153
  - Preserve original formatting when possible
143
154
 
144
155
  ### Type Coercion
145
156
  - String fields: preserve as-is from XML
146
157
  - Integer fields: convert with proper error handling
147
- - Decimal fields: use BigDecimal for precision
158
+ - Decimal fields: convert to Float with proper error handling
148
159
  - Boolean attributes: handle "true"/"false" strings
149
160
 
150
161
  ### Default Values
@@ -152,6 +163,7 @@ bundle exec rspec --format documentation
152
163
  - Empty strings for string fields
153
164
  - -1 for unset integer fields
154
165
  - "Unknown" for enum fields where applicable
166
+ - `nil` for an unset CommunityRating
155
167
 
156
168
  ## Testing Strategy
157
169
 
@@ -197,9 +209,9 @@ RSpec.describe ComicInfo::Issue do
197
209
  end
198
210
 
199
211
  describe 'multi-value fields' do
200
- describe 'singular methods (return strings)' do
201
- it 'returns character as comma-separated string' do
202
- expect(complete_comic.character).to eq 'Spider-Man, Peter Parker'
212
+ describe 'raw data methods (return strings)' do
213
+ it 'returns characters as comma-separated string' do
214
+ expect(complete_comic.characters_raw_data).to eq 'Spider-Man, Peter Parker'
203
215
  end
204
216
  end
205
217
 
@@ -235,21 +247,19 @@ end
235
247
  - Maintain comprehensive README with examples
236
248
  - Document all public methods with YARD comments
237
249
  - Include schema field descriptions in code comments
238
- - Support multiple schema versions (1.0, 2.0, 2.1-draft)
250
+ - Only schema 2.0 is implemented; the 1.0 and 2.1-draft XSD files are kept in `schemas/` for reference
239
251
 
240
252
  ## Implementation Status
241
253
  - ✅ Core reading functionality complete
254
+ - ✅ XML writing (`#to_xml`, `#save`)
255
+ - ✅ JSON, YAML and Hash export (`#to_json`, `#to_yaml`, `#to_h`)
242
256
  - ✅ All schema fields implemented and tested
243
- - ✅ Comprehensive test suite (156 test cases)
244
257
  - ✅ Error handling with custom exception classes
245
- - ✅ Multi-value field support (singular/plural methods)
258
+ - ✅ Multi-value field support (plural and `_raw_data` methods)
246
259
  - ✅ Page objects with full attribute support
247
260
  - ✅ Enum validation and type coercion
248
261
  - ✅ Unicode and special character handling
249
262
 
250
263
  ## Future Considerations
251
- - XML writing/generation capabilities (.to_xml method)
252
- - JSON export (.to_json method) - **Partially implemented**
253
- - YAML export (.to_yaml method) - **Partially implemented**
254
264
  - Schema version detection and migration
255
265
  - CLI tool for ComicInfo manipulation
@@ -0,0 +1,103 @@
1
+ require_relative 'enums'
2
+
3
+ module ComicInfo
4
+ # Fills in a file name template, such as "{series} #{number} ({year})", from an issue.
5
+ #
6
+ # A value the issue lacks is left out, along with decoration that would be left empty around it:
7
+ # "Saga # ()" becomes "Saga". "/" and ":" in a value become "-", since they cannot be part of a
8
+ # file name. Anything else in the template, including a {name} that is not a token, is kept as
9
+ # written. The result has no file extension.
10
+ class FilenameTemplate
11
+ # The names #filename fills in, written in a template as {name}.
12
+ TOKENS = %w[number publisher series title volume year].freeze
13
+
14
+ # The names #page_filename fills in: TOKENS and page.
15
+ PAGE_TOKENS = [*TOKENS, 'page'].freeze
16
+
17
+ EMPTY_DECORATION = ['()', '[]'].freeze
18
+
19
+ def initialize template
20
+ @template = template.to_s
21
+ end
22
+
23
+ # A file name for the issue.
24
+ #
25
+ # Write {number:3} to pad the issue number with zeros to that many digits (1 to 99). Only the
26
+ # whole number it starts with is padded: "1.5" becomes "001.5" and "12AU" becomes "012AU".
27
+ def filename issue
28
+ fill template, issue
29
+ end
30
+
31
+ # A file name for one of the issue's page images.
32
+ #
33
+ # {page} is the page number, and {page:3} pads it with zeros to that many digits (1 to 99).
34
+ # Page files are read in name order, so the result always keeps pages apart and in order:
35
+ # the page number is at least as wide as the page count, a template with no page number gets
36
+ # one appended after "_", and a name that would be empty is the page number alone.
37
+ # "/" and ":" anywhere in the result become "-".
38
+ def page_filename issue, page:, of:
39
+ minimum_width = [page, of].max.to_s.length
40
+ number = zero_padded page.to_s, minimum_width
41
+ numbered = fill_padded('page', number, template).gsub('{page}', number)
42
+ name = fill numbered, issue
43
+
44
+ complete = if numbered != template then name
45
+ elsif name.empty? then number
46
+ else
47
+ "#{name}_#{number}"
48
+ end
49
+
50
+ complete.tr '/:', '-'
51
+ end
52
+
53
+ private
54
+
55
+ attr_reader :template
56
+
57
+ def fill text, issue
58
+ name = fill_padded 'number', value(issue, 'number'), text
59
+ TOKENS.each { name = name.gsub("{#{it}}", value(issue, it)) }
60
+ tidy name
61
+ end
62
+
63
+ # Fills in each {token:width} with the value padded to that width, leaving one whose width is
64
+ # not 1 to 99 as written.
65
+ def fill_padded token, value, text
66
+ text.gsub(/\{#{token}:(\d{1,2})\}/) do |written|
67
+ width = Regexp.last_match(1).to_i
68
+ width.zero? ? written : zero_padded(value, width)
69
+ end
70
+ end
71
+
72
+ # Pads the whole number the value starts with: "1.5" to "001.5" for a width of 3.
73
+ def zero_padded value, width
74
+ whole = value[/\A\d+/]
75
+ return value if whole.nil? || whole.length >= width
76
+
77
+ ('0' * (width - whole.length)) + value
78
+ end
79
+
80
+ def value issue, token
81
+ raw = case token
82
+ when 'number' then issue.number
83
+ when 'publisher' then issue.publisher
84
+ when 'series' then issue.series
85
+ when 'title' then issue.title
86
+ when 'volume' then integer issue.volume
87
+ when 'year' then integer issue.year
88
+ end
89
+
90
+ raw.to_s.tr('/:', '-').strip
91
+ end
92
+
93
+ def integer value
94
+ value == Enums::DEFAULT_INTEGER ? nil : value
95
+ end
96
+
97
+ # Removes decoration left empty by missing values, then collapses whitespace.
98
+ def tidy name
99
+ tidied = EMPTY_DECORATION.reduce(name) { |text, empty| text.gsub empty, '' }
100
+ tidied.split.reject { it == '#' }.join ' '
101
+ end
102
+ end
103
+ end
@@ -5,6 +5,7 @@ require 'yaml'
5
5
  require 'fileutils'
6
6
  require_relative 'enums'
7
7
  require_relative 'errors'
8
+ require_relative 'filename_template'
8
9
  require_relative 'page'
9
10
 
10
11
  module ComicInfo
@@ -120,6 +121,76 @@ module ComicInfo
120
121
  @pages.select(&:story?)
121
122
  end
122
123
 
124
+ # A file name for this issue, without an extension, made by filling in a template such as
125
+ # "{series} #{number} ({year})". See FilenameTemplate for the tokens and rules.
126
+ def filename template:
127
+ FilenameTemplate.new(template).filename self
128
+ end
129
+
130
+ # A file name for one of this issue's page images, without an extension, made by filling in a
131
+ # template such as "page_{page:3}". See FilenameTemplate for the tokens and rules.
132
+ def page_filename template:, page:, of:
133
+ FilenameTemplate.new(template).page_filename self, page:, of:
134
+ end
135
+
136
+ # The same issue after its page images were reordered, deleted, or added to.
137
+ #
138
+ # A <Page> entry points at its image by index, so when the images change the entries must
139
+ # follow. `order` describes the new images: order[i] is the index the image now at position i
140
+ # had before, or nil for an image that is new.
141
+ #
142
+ # # Three pages; the last is moved to the front and a new page is appended.
143
+ # updated = issue.rearranging_pages [2, 0, 1, nil]
144
+ #
145
+ # - A page keeps its entry (type, bookmark, sizes) under its new index.
146
+ # - A deleted page loses its entry.
147
+ # - A page that had no entry still has none.
148
+ # - A new page gets a default entry, but only if the issue has page entries at all.
149
+ # - page_count becomes the new number of pages, but only if it was set.
150
+ #
151
+ # Returns a copy; this issue is not changed.
152
+ def rearranging_pages order
153
+ rearranged = rearranged_pages order
154
+ count = @page_count == Enums::DEFAULT_PAGE_COUNT ? @page_count : order.length
155
+
156
+ with_pages rearranged, page_count: count
157
+ end
158
+
159
+ # The type of the page image at an index (0-based, as in Page#image).
160
+ #
161
+ # This is the first type of that image's <Page> entry. A page with no entry is a Story page,
162
+ # as the schema says.
163
+ def page_type at:
164
+ entry = @pages.find { it.image == at }
165
+ entry ? entry.types.first : Enums::DEFAULT_PAGE_TYPE
166
+ end
167
+
168
+ # The same issue with the page image at an index given a type.
169
+ #
170
+ # updated = issue.setting_page_type 'FrontCover', at: 0
171
+ #
172
+ # - The page's entry keeps everything else it has (bookmark, sizes); the type replaces all of
173
+ # the types it had.
174
+ # - A page with no entry gets one, placed in image order.
175
+ # - Setting Story on a page with no entry changes nothing, since that is already its type.
176
+ #
177
+ # Returns a copy; this issue is not changed. Raises Errors::InvalidEnumError for a type the
178
+ # schema does not have.
179
+ def setting_page_type type, at:
180
+ Enums::Validators.validate_comic_page_type type
181
+ position = @pages.index { it.image == at }
182
+
183
+ if position
184
+ updated = @pages.dup
185
+ updated[position] = @pages[position].with_type type
186
+ return with_pages updated, page_count: @page_count
187
+ end
188
+ return self if type == Enums::DEFAULT_PAGE_TYPE
189
+
190
+ added = [*@pages, Page.new(image: at, type:)]
191
+ with_pages added.sort_by(&:image), page_count: @page_count
192
+ end
193
+
123
194
  # Get publication date as Date object if available
124
195
  def publication_date
125
196
  return nil if @year == Enums::DEFAULT_INTEGER
@@ -315,8 +386,30 @@ module ComicInfo
315
386
  }.compact
316
387
  end
317
388
 
389
+ protected
390
+
391
+ attr_writer :pages, :page_count
392
+
318
393
  private
319
394
 
395
+ # The same issue with different page entries and page count
396
+ def with_pages pages, page_count:
397
+ copy = dup
398
+ copy.pages = pages
399
+ copy.page_count = page_count
400
+ copy
401
+ end
402
+
403
+ def rearranged_pages order
404
+ return [] if @pages.empty?
405
+
406
+ order.each_with_index.filter_map do |previous_index, new_index|
407
+ next Page.new(image: new_index) if previous_index.nil?
408
+
409
+ @pages.find { it.image == previous_index }&.with_image new_index
410
+ end
411
+ end
412
+
320
413
  def parse_fields
321
414
  # String fields
322
415
  @title = get_string_field('Title')
@@ -49,6 +49,16 @@ module ComicInfo
49
49
  types.include?(type)
50
50
  end
51
51
 
52
+ # The same page entry pointing at a different image index
53
+ def with_image image
54
+ Page.new to_h.merge(image:)
55
+ end
56
+
57
+ # The same page entry with a different type, which replaces every type it had
58
+ def with_type type
59
+ Page.new to_h.merge(type:)
60
+ end
61
+
52
62
  # Get image dimensions as a hash
53
63
  def dimensions
54
64
  {
@@ -1,3 +1,3 @@
1
1
  module ComicInfo
2
- VERSION = '2.0.0'.freeze
2
+ VERSION = '2.1.0'.freeze
3
3
  end
data/lib/comicinfo.rb CHANGED
@@ -7,6 +7,7 @@ module ComicInfo
7
7
  autoload :Page, 'comicinfo/page'
8
8
  autoload :Enums, 'comicinfo/enums'
9
9
  autoload :Errors, 'comicinfo/errors'
10
+ autoload :FilenameTemplate, 'comicinfo/filename_template'
10
11
 
11
12
  # Convenience method for loading ComicInfo files
12
13
  def self.load file_path_or_xml_string
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comicinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Becker
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 1.18.10
18
+ version: 1.19.3
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: 1.18.10
25
+ version: 1.19.3
26
26
  description: |
27
27
  An idiomatic Ruby interface for reading and writing ComicInfo.xml files,
28
28
  following the official ComicInfo schema specifications from the Anansi Project
@@ -42,6 +42,7 @@ files:
42
42
  - lib/comicinfo.rb
43
43
  - lib/comicinfo/enums.rb
44
44
  - lib/comicinfo/errors.rb
45
+ - lib/comicinfo/filename_template.rb
45
46
  - lib/comicinfo/issue.rb
46
47
  - lib/comicinfo/page.rb
47
48
  - lib/comicinfo/version.rb
@@ -66,14 +67,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
67
  requirements:
67
68
  - - ">="
68
69
  - !ruby/object:Gem::Version
69
- version: 3.4.6
70
+ version: 4.0.9
70
71
  required_rubygems_version: !ruby/object:Gem::Requirement
71
72
  requirements:
72
73
  - - ">="
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubygems_version: 3.7.2
77
+ rubygems_version: 4.0.20
77
78
  specification_version: 4
78
79
  summary: Ruby interface for working with ComicInfo.xml files
79
80
  test_files: []