Class: ComicInfo::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/comicinfo/page.rb

Overview

Represents a single page in a comic book with metadata Maps to ComicPageInfo type in the ComicInfo XSD schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Page

Returns a new instance of Page.



11
12
13
14
15
16
17
18
19
20
# File 'lib/comicinfo/page.rb', line 11

def initialize attributes = {}
  @image        = parse_image(attributes['Image'] || attributes[:image])
  @type         = Enums::Validators.validate_comic_page_type(attributes['Type'] || attributes[:type])
  @double_page  = parse_boolean(attributes['DoublePage'] || attributes[:double_page])
  @image_size   = parse_image_size(attributes['ImageSize'] || attributes[:image_size])
  @key          = (attributes['Key'] || attributes[:key] || Enums::DEFAULT_STRING).to_s
  @bookmark     = (attributes['Bookmark'] || attributes[:bookmark] || Enums::DEFAULT_STRING).to_s
  @image_width  = parse_image_dimension(attributes['ImageWidth'] || attributes[:image_width], 'ImageWidth')
  @image_height = parse_image_dimension(attributes['ImageHeight'] || attributes[:image_height], 'ImageHeight')
end

Instance Attribute Details

#bookmarkObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def bookmark
  @bookmark
end

#double_pageObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def double_page
  @double_page
end

#imageObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def image
  @image
end

#image_heightObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def image_height
  @image_height
end

#image_sizeObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def image_size
  @image_size
end

#image_widthObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def image_width
  @image_width
end

#keyObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def key
  @key
end

#typeObject (readonly)

Page attributes from XSD schema



9
10
11
# File 'lib/comicinfo/page.rb', line 9

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Equality comparison



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/comicinfo/page.rb', line 119

def == other
  return false unless other.is_a?(Page)

  @image == other.image &&
    @type == other.type &&
    @double_page == other.double_page &&
    @image_size == other.image_size &&
    @key == other.key &&
    @bookmark == other.bookmark &&
    @image_width == other.image_width &&
    @image_height == other.image_height
end

#aspect_ratioObject

Get aspect ratio if dimensions are available



66
67
68
69
70
# File 'lib/comicinfo/page.rb', line 66

def aspect_ratio
  return nil unless dimensions_available? && @image_height != 0

  @image_width.to_f / @image_height
end

#bookmarked?Boolean

Check if this page has a bookmark

Returns:

  • (Boolean)


73
74
75
# File 'lib/comicinfo/page.rb', line 73

def bookmarked?
  !@bookmark.empty?
end

#cover?Boolean

Check if this page is a cover page

Returns:

  • (Boolean)


23
24
25
# File 'lib/comicinfo/page.rb', line 23

def cover?
  @type == 'FrontCover' || @type == 'BackCover' || @type == 'InnerCover'
end

#deleted?Boolean

Check if this page should be deleted/hidden

Returns:

  • (Boolean)


33
34
35
# File 'lib/comicinfo/page.rb', line 33

def deleted?
  @type == 'Deleted'
end

#dimensionsObject

Get image dimensions as a hash



53
54
55
56
57
58
# File 'lib/comicinfo/page.rb', line 53

def dimensions
  {
    width:  @image_width == Enums::DEFAULT_INTEGER ? nil : @image_width,
    height: @image_height == Enums::DEFAULT_INTEGER ? nil : @image_height
  }
end

#dimensions_available?Boolean

Check if image dimensions are available

Returns:

  • (Boolean)


61
62
63
# File 'lib/comicinfo/page.rb', line 61

def dimensions_available?
  @image_width != Enums::DEFAULT_INTEGER && @image_height != Enums::DEFAULT_INTEGER
end

#double_page?Boolean

Check if this is a double-page spread

Returns:

  • (Boolean)


38
39
40
# File 'lib/comicinfo/page.rb', line 38

def double_page?
  @double_page
end

#hashObject



134
135
136
# File 'lib/comicinfo/page.rb', line 134

def hash
  [@image, @type, @double_page, @image_size, @key, @bookmark, @image_width, @image_height].hash
end

#include_type?(type) ⇒ Boolean

Check if page has a specific type

Returns:

  • (Boolean)


48
49
50
# File 'lib/comicinfo/page.rb', line 48

def include_type? type
  types.include?(type)
end

#inspectObject

Detailed inspection



114
115
116
# File 'lib/comicinfo/page.rb', line 114

def inspect
  "#<ComicInfo::Page #{self}>"
end

#story?Boolean

Check if this page is a story page

Returns:

  • (Boolean)


28
29
30
# File 'lib/comicinfo/page.rb', line 28

def story?
  @type == 'Story'
end

#to_hObject

Convert to hash representation



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/comicinfo/page.rb', line 78

def to_h
  {
    image:        @image,
    type:         @type,
    double_page:  @double_page,
    image_size:   @image_size,
    key:          @key,
    bookmark:     @bookmark,
    image_width:  @image_width,
    image_height: @image_height
  }
end

#to_sObject

String representation



105
106
107
108
109
110
111
# File 'lib/comicinfo/page.rb', line 105

def to_s
  parts = ["Page #{@image}"]
  parts << "Type: #{@type}" unless @type == Enums::DEFAULT_PAGE_TYPE
  parts << 'Double' if @double_page
  parts << "Bookmark: #{@bookmark}" unless @bookmark.empty?
  parts.join(', ')
end

#to_xml_attributesObject

Convert to XML attributes hash (for XML generation)



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/comicinfo/page.rb', line 92

def to_xml_attributes
  attrs = { 'Image' => @image.to_s }
  attrs['Type']        = @type unless @type == Enums::DEFAULT_PAGE_TYPE
  attrs['DoublePage']  = @double_page.to_s unless @double_page == Enums::DEFAULT_DOUBLE_PAGE
  attrs['ImageSize']   = @image_size.to_s unless @image_size == Enums::DEFAULT_IMAGE_SIZE
  attrs['Key']         = @key unless @key.empty?
  attrs['Bookmark']    = @bookmark unless @bookmark.empty?
  attrs['ImageWidth']  = @image_width.to_s unless @image_width == Enums::DEFAULT_INTEGER
  attrs['ImageHeight'] = @image_height.to_s unless @image_height == Enums::DEFAULT_INTEGER
  attrs
end

#typesObject

Get page types as an array (handles space-separated values)



43
44
45
# File 'lib/comicinfo/page.rb', line 43

def types
  @type.split(/\s+/)
end