Class: Epuber::Book::Target

Inherits:
DSL::TreeObject show all
Defined in:
lib/epuber/book/target.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, #root?, #validate

Methods inherited from DSL::Object

from_file, #from_file?, from_string, #to_s, #validate

Methods included from DSL::AttributeSupport

#attribute, #define_method_attr, #dsl_attributes, #find_root

Constructor Details

#initialize(name, parent: nil) ⇒ Target



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epuber/book/target.rb', line 15

def initialize(name, parent: nil)
  super(parent)

  @name      = name
  @is_ibooks = nil
  @book      = nil
  @files     = []
  @constants = {}
  @root_toc  = TocItem.new

  @default_styles = []
  @default_scripts = []
  @plugins = []
end

Dynamic Method Handling

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

Instance Attribute Details

#book ⇒ Epuber::Book



66
67
68
# File 'lib/epuber/book/target.rb', line 66

def book
  @book || parent&.book
end

#name ⇒ String, Symbol (readonly)



50
51
52
# File 'lib/epuber/book/target.rb', line 50

def name
  @name
end

#root_toc ⇒ Epuber::Book::TocItem (readonly)



58
59
60
# File 'lib/epuber/book/target.rb', line 58

def root_toc
  @root_toc
end

Instance Method Details

#add_const(key, value = nil) ⇒ void



257
258
259
260
261
262
263
# File 'lib/epuber/book/target.rb', line 257

def add_const(key, value = nil)
  if key.is_a?(Hash) && value.nil?
    @constants.merge!(key)
  else
    @constants[key] = value
  end
end

#add_default_script(*file_paths) ⇒ void



298
299
300
301
302
303
304
305
# File 'lib/epuber/book/target.rb', line 298

def add_default_script(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :script)
    file_obj.only_one = true

    @default_scripts << file_obj unless @default_scripts.include?(file_obj)
  end
end

#add_default_scripts(*file_paths) ⇒ void

This method returns an undefined value.

Add default scripts to target, default scripts will be automatically added to xhtml document

Only difference with #add_default_script is it adds multiple files with one pattern



314
315
316
317
318
319
320
321
# File 'lib/epuber/book/target.rb', line 314

def add_default_scripts(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :script)
    file_obj.only_one = false

    @default_scripts << file_obj unless @default_scripts.include?(file_obj)
  end
end

#add_default_style(*file_paths) ⇒ void



269
270
271
272
273
274
275
276
# File 'lib/epuber/book/target.rb', line 269

def add_default_style(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :style)
    file_obj.only_one = true

    @default_styles << file_obj unless @default_styles.include?(file_obj)
  end
end

#add_default_styles(*file_paths) ⇒ void

This method returns an undefined value.

Add default styles to default target, default styles will be automatically added to xhtml document

Only difference with #add_default_style is it adds multiple files with one pattern



285
286
287
288
289
290
291
292
# File 'lib/epuber/book/target.rb', line 285

def add_default_styles(*file_paths)
  file_paths.map do |file_path|
    file_obj          = add_file(file_path, group: :style)
    file_obj.only_one = false

    @default_styles << file_obj unless @default_styles.include?(file_obj)
  end
end

#add_file(file_path, group: nil) ⇒ Epuber::Book::File



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/epuber/book/target.rb', line 223

def add_file(file_path, group: nil)
  file = if file_path.is_a?(FileRequest)
           file_path
         else
           FileRequest.new(file_path, group: group)
         end


  old_file = @files.find { |f| f == file }

  if old_file.nil?
    @files << file
    file
  else
    old_file
  end
end

#add_files(*file_paths) ⇒ void



245
246
247
248
249
250
# File 'lib/epuber/book/target.rb', line 245

def add_files(*file_paths)
  file_paths.each do |file_path|
    file_obj          = add_file(file_path)
    file_obj.only_one = false
  end
end

#constants ⇒ Hash<String, Object>

Returns all constants



128
129
130
# File 'lib/epuber/book/target.rb', line 128

def constants
  (parent&.constants || {}).merge(@constants)
end

#cover_image ⇒ FileRequest



192
193
194
195
196
197
# File 'lib/epuber/book/target.rb', line 192

attribute :cover_image,
                types: [FileRequest],
                inherited: true,
                auto_convert: { [String] => lambda { |value|
  FileRequest.new(value, group: :image, properties: [:cover_image])
} }

#create_mobi ⇒ Bool



207
208
209
# File 'lib/epuber/book/target.rb', line 207

attribute :create_mobi,
types: [TrueClass, FalseClass],
inherited: true

#custom_fonts ⇒ String



174
175
176
# File 'lib/epuber/book/target.rb', line 174

attribute :custom_fonts,
types: [TrueClass, FalseClass],
inherited: true

#default_scripts ⇒ Array<Epuber::Book::FileRequest>



140
141
142
# File 'lib/epuber/book/target.rb', line 140

def default_scripts
  ((parent && parent.default_scripts) || []) + @default_scripts
end

#default_styles ⇒ Array<Epuber::Book::FileRequest>



134
135
136
# File 'lib/epuber/book/target.rb', line 134

def default_styles
  ((parent && parent.default_styles) || []) + @default_styles
end

#default_viewport ⇒ Size



201
202
203
# File 'lib/epuber/book/target.rb', line 201

attribute :default_viewport,
types: [Size],
inherited: true

#epub_version ⇒ String



156
157
158
159
160
# File 'lib/epuber/book/target.rb', line 156

attribute :epub_version,
required: true,
inherited: true,
auto_convert: { [String, Integer, Float] => Version },
default_value: 3.0

#files ⇒ Array<Epuber::Book::FileRequest>

Returns all files



116
117
118
119
120
121
122
123
# File 'lib/epuber/book/target.rb', line 116

def files
  # parent files plus our files
  all_files = ((parent && parent.files) || []) + @files + @default_styles + @default_scripts

  all_files << @attributes_values[:cover_image] unless @attributes_values[:cover_image].nil?

  all_files
end

#fixed_layout ⇒ Bool



186
187
188
# File 'lib/epuber/book/target.rb', line 186

attribute :fixed_layout,
types: [TrueClass, FalseClass],
inherited: true

#freeze ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/epuber/book/target.rb', line 30

def freeze
  super
  @files.freeze
  @files.each(&:freeze)

  @default_styles.freeze
  @default_styles.each(&:freeze)

  @default_scripts.freeze
  @default_scripts.each(&:freeze)

  @plugins.freeze
  @plugins.each(&:freeze)

  @root_toc.freeze
  @constants.freeze
end

#ibooks? ⇒ Bool



105
106
107
108
109
110
111
# File 'lib/epuber/book/target.rb', line 105

def ibooks?
  if is_ibooks.nil?
    name.to_s.include?('ibooks')
  else
    is_ibooks
  end
end

#identifier ⇒ String



169
170
# File 'lib/epuber/book/target.rb', line 169

attribute :identifier,
inherited: true

#is_abstract ⇒ Bool



213
214
215
# File 'lib/epuber/book/target.rb', line 213

attribute :is_abstract,
types: [TrueClass, FalseClass],
inherited: false

#is_ibooks ⇒ Bool



180
181
182
# File 'lib/epuber/book/target.rb', line 180

attribute :is_ibooks,
types: [TrueClass, FalseClass],
inherited: true

#isbn ⇒ String



164
165
# File 'lib/epuber/book/target.rb', line 164

attribute :isbn,
inherited: true

#plugins ⇒ Array<String>



146
147
148
# File 'lib/epuber/book/target.rb', line 146

def plugins
  ((parent && parent.plugins) || []) + @plugins
end

#sub_abstract_target(name) {|child| ... } ⇒ Target

Create new sub_target with name

Yields:

  • (child)


91
92
93
94
95
96
97
98
99
# File 'lib/epuber/book/target.rb', line 91

def sub_abstract_target(name)
  child = create_child_item(name)
  child.book = book
  child.is_abstract = true

  yield child if block_given?

  child
end

#sub_target(name) {|child| ... } ⇒ Target

Create new sub_target with name

Yields:

  • (child)


76
77
78
79
80
81
82
83
# File 'lib/epuber/book/target.rb', line 76

def sub_target(name)
  child = create_child_item(name)
  child.book = book

  yield child if block_given?

  child
end

#toc {|toc_item, target| ... } ⇒ Object

Returns nil.

Yields:

Yield Parameters:

  • toc_item (TocItem) —

    root toc item

  • target (self) —

    current target



329
330
331
# File 'lib/epuber/book/target.rb', line 329

def toc
  yield(@root_toc, self) if block_given?
end

#use(path) ⇒ nil



337
338
339
# File 'lib/epuber/book/target.rb', line 337

def use(path)
  @plugins << path
end