Class: Epuber::Book::TocItem

Inherits:
DSL::TreeObject show all
Defined in:
lib/epuber/book/toc_item.rb

Instance Attribute Summary collapse

Attributes inherited from DSL::TreeObject

#parent, #sub_items

Attributes inherited from DSL::Object

#file_path

Instance Method Summary collapse

Methods inherited from DSL::TreeObject

#create_child_item, #create_child_items, #flat_sub_items, #freeze, #initialize, #root?, #validate

Methods inherited from DSL::Object

#freeze, from_file, #from_file?, from_string, #initialize, #to_s, #validate

Methods included from DSL::AttributeSupport

#attribute, #define_method_attr, #dsl_attributes, #find_root

Constructor Details

This class inherits a constructor from Epuber::DSL::TreeObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Epuber::DSL::Object

Instance Attribute Details

#file_fragmentString

Returns:

  • (String)


27
28
29
# File 'lib/epuber/book/toc_item.rb', line 27

def file_fragment
  @file_fragment
end

Instance Method Details

#file(file_path, title = nil, *opts) ⇒ Object

Creating sub item from file

Examples:

toc.file 'ch01', 'Chapter 1', :landmark_start_page
toc.file 'ch02', :landmark_copyright
toc.file 'ch03', :linear => false
toc.file 'ch04', linear: false

Parameters:

  • file_path (String)

    pattern describing path to file

  • title (String) (defaults to: nil)

    title of this item



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/epuber/book/toc_item.rb', line 84

def file(file_path, title = nil, *opts)
  create_child_item do |item|
    unless file_path.nil?
      file_pattern, file_fragment = file_path.split('#')

      unless file_pattern.nil? || file_pattern.empty?
        file_obj = FileRequest.new(file_pattern, group: :text)
        item.file_request = file_obj
      end

      item.file_fragment = file_fragment unless file_fragment.nil? || file_fragment.empty?
    end

    if title.is_a?(String)
      item.title = title
    else
      opts.unshift(title)
    end

    item.options = opts.map do |i|
      if i.is_a?(Hash)
        i.map do |j_key, j_value|
          { j_key => j_value }
        end
      else
        i
      end
    end.flatten

    yield item if block_given?
  end
end

#file_requestEpuber::Book::FileRequest



12
13
14
# File 'lib/epuber/book/toc_item.rb', line 12

attribute :file_request,
auto_convert: { String => FileRequest },
inherited: true

#full_source_patternString

Returns:

  • (String)


56
57
58
# File 'lib/epuber/book/toc_item.rb', line 56

def full_source_pattern
  [file_request.source_pattern, file_fragment].compact.join('#')
end

#item(title, *opts) ⇒ Object

Creating sub item without reference to file

Parameters:

  • title (String)


121
122
123
# File 'lib/epuber/book/toc_item.rb', line 121

def item(title, *opts)
  file(nil, title, *opts)
end

#landmarksArray<Symbol>

Returns:

  • (Array<Symbol>)


34
35
36
37
38
# File 'lib/epuber/book/toc_item.rb', line 34

def landmarks
  options.select do |item|
    item.is_a?(Symbol) && item.to_s.start_with?('landmark')
  end
end

#linear?Bool

Returns:

  • (Bool)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/epuber/book/toc_item.rb', line 42

def linear?
  first = options.select do |item|
    item.is_a?(Hash) && (item.include?(:linear) || item.include?('linear'))
  end.first

  if first.nil?
    true
  else
    first.values.first
  end
end

#local_source_patternString

Returns:

  • (String)


62
63
64
65
66
67
68
# File 'lib/epuber/book/toc_item.rb', line 62

def local_source_pattern
  file_request = attributes_values[:file_request]

  return "##{file_fragment}" if file_request.nil?

  [file_request.source_pattern, file_fragment].compact.join('#')
end

#optionsArray<Symbol | Hash<Symbol, Object>>

Returns:

  • (Array<Symbol | Hash<Symbol, Object>>)


22
23
# File 'lib/epuber/book/toc_item.rb', line 22

attribute :options,
default_value: []

#titleString

Returns:

  • (String)


18
# File 'lib/epuber/book/toc_item.rb', line 18

attribute :title