Skip to content

Commit

Permalink
Make inspect console friendly
Browse files Browse the repository at this point in the history
- Omit long strings
- Print only class name for non standard types
  • Loading branch information
lulalala committed Sep 2, 2021
1 parent 4d02448 commit 0f7f1ca
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/gepub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def ruby2_keywords(*)
require 'gepub/version'
require 'gepub/dsl_util'
require 'gepub/xml_util'
require 'gepub/inspect_mixin'
require 'gepub/meta'
require 'gepub/datemeta'
require 'gepub/meta_array'
Expand Down
2 changes: 2 additions & 0 deletions lib/gepub/bindings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module GEPUB
class Bindings
include XMLUtil
include InspectMixin

class MediaType
attr_accessor :handler, :media_type
def initialize(handler, media_type)
Expand Down
2 changes: 2 additions & 0 deletions lib/gepub/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ module GEPUB
# set page-proression-direction attribute to spine.

class Book
include InspectMixin

MIMETYPE='mimetype'
MIMETYPE_CONTENTS='application/epub+zip'
CONTAINER='META-INF/container.xml'
Expand Down
26 changes: 26 additions & 0 deletions lib/gepub/inspect_mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module GEPUB
module InspectMixin
def inspect
result = instance_variables.each
.with_object({}) { |name, h| h[name] = instance_variable_get(name) }
.reject { |name, value| value.nil? }
.map { |name, value|
case value
when true, false, Symbol, Numeric, Array, Hash
"#{name}=#{value.inspect}"
when String
if value.length > 80
"#{name}=(omitted)"
else
"#{name}=#{value.inspect}"
end
else
"#{name}=#<#{value.class.name}>"
end
}
.join(' ')

"#<#{self.class.name} " + result + '>'
end
end
end
2 changes: 2 additions & 0 deletions lib/gepub/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module GEPUB
# #id, #id=, #set_id, #href, #href=, #set_href, #media_type, #media_type=, #set_media_type,
# #fallback, #fallback=, #set_fallback, #media_overlay, #media_overlay=, #set_media_overlay
class Item
include InspectMixin

attr_accessor :content
def self.create(parent, attributes = {})
Item.new(attributes['id'], attributes['href'], attributes['media-type'], parent,
Expand Down
2 changes: 2 additions & 0 deletions lib/gepub/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module GEPUB
class Manifest
include XMLUtil
include InspectMixin

attr_accessor :opf_version
def self.parse(manifest_xml, opf_version = '3.0', id_pool = Package::IDPool.new)
Manifest.new(opf_version, id_pool) {
Expand Down
2 changes: 2 additions & 0 deletions lib/gepub/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def self.content
end
end
include XMLUtil, DSLUtil
include InspectMixin

attr_accessor :opf_version
# parse metadata element. metadata_xml should be Nokogiri::XML::Node object.
def self.parse(metadata_xml, opf_version = '3.0', id_pool = Package::IDPool.new)
Expand Down
2 changes: 2 additions & 0 deletions lib/gepub/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module GEPUB
# Holds data in opf file.
class Package
include XMLUtil, DSLUtil
include InspectMixin

extend Forwardable
attr_accessor :path, :metadata, :manifest, :spine, :bindings, :epub_backward_compat, :contents_prefix, :prefixes
def_delegators :@manifest, :item_by_href
Expand Down
2 changes: 2 additions & 0 deletions lib/gepub/spine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module GEPUB
class Spine
include XMLUtil
include InspectMixin

attr_accessor :opf_version
class Itemref
def self.create(parent, attributes = {})
Expand Down
1 change: 1 addition & 0 deletions tools/generate_function.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../lib/gepub/item.rb'
require_relative '../lib/gepub/inspect_mixin.rb'
attrs = GEPUB::Item::ATTRIBUTES.select do |attr|
attr != 'href'
end.map do |attr|
Expand Down

0 comments on commit 0f7f1ca

Please sign in to comment.