Class: Epuber::Transformer::BookTransformer

Inherits:
Epuber::Transformer show all
Defined in:
lib/epuber/transformer/book_transformer.rb

Instance Attribute Summary collapse

Attributes inherited from CheckerTransformerBase

#block, #options, #source_type

Instance Method Summary collapse

Methods inherited from Epuber::Transformer

map_source_type__class

Methods inherited from CheckerTransformerBase

class_for_source_type, #initialize, map_source_type__class, #valid_options

Constructor Details

This class inherits a constructor from Epuber::CheckerTransformerBase

Instance Attribute Details

#bookBook

Returns:



10
11
12
# File 'lib/epuber/transformer/book_transformer.rb', line 10

def book
  @book
end

#compilation_contextCompilationContext

Returns:

  • (CompilationContext)


14
15
16
# File 'lib/epuber/transformer/book_transformer.rb', line 14

def compilation_context
  @compilation_context
end

Instance Method Details

#call(book, compilation_context) ⇒ Object

Parameters:



19
20
21
22
23
24
25
26
27
# File 'lib/epuber/transformer/book_transformer.rb', line 19

def call(book, compilation_context)
  @book = book
  @compilation_context = compilation_context

  @block.call(self, @book, @compilation_context)

  @book = nil
  @compilation_context = nil
end

#find_destination_files(pattern, context_path: nil, groups: nil, location: caller_locations.first) ⇒ Array<Epuber::Compiler::FileTypes::AbstractFile>

Find files destination paths

Parameters:

  • pattern (String)
  • context_path (String) (defaults to: nil)

    path to directory that is used as context for finding

  • groups (Array<Symbol>, Symbol) (defaults to: nil)
  • location (Thread::Backtrace::Location) (defaults to: caller_locations.first)

Returns:



69
70
71
72
73
74
75
# File 'lib/epuber/transformer/book_transformer.rb', line 69

def find_destination_files(pattern, context_path: nil, groups: nil, location: caller_locations.first)
  UI.error!("Pattern for finding file can't be nil", location: location) if pattern.nil?

  paths = @compilation_context.file_resolver.dest_finder.find_files(pattern, context_path: context_path,
                                                                             groups: groups)
  paths.map { |path| @compilation_context.file_resolver.file_with_destination_path(path) }
end

#find_file(pattern, context_path: nil, groups: nil, location: caller_locations.first) ⇒ Epuber::Compiler::FileTypes::AbstractFile?

Find file in source or destination paths

Parameters:

  • pattern (String)
  • context_path (String) (defaults to: nil)

    path to directory that is used as context for finding

  • groups (Array<Symbol>, Symbol) (defaults to: nil)
  • location (Thread::Backtrace::Location) (defaults to: caller_locations.first)

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/epuber/transformer/book_transformer.rb', line 38

def find_file(pattern, context_path: nil, groups: nil, location: caller_locations.first)
  return pattern if pattern.is_a?(Compiler::FileTypes::AbstractFile)

  UI.error!("Pattern for finding file can't be nil", location: location) if pattern.nil?

  begin
    path = @compilation_context.file_resolver.source_finder.find_file(pattern, context_path: context_path,
                                                                               groups: groups)
    path = File.join(context_path, path) if context_path
    @compilation_context.file_resolver.file_with_source_path(path)
  rescue Compiler::FileFinders::FileNotFoundError
    begin
      path = @compilation_context.file_resolver.dest_finder.find_file(pattern, context_path: context_path,
                                                                               groups: groups)
      path = File.join(context_path, path) if context_path
      @compilation_context.file_resolver.file_with_destination_path(path)
    rescue Compiler::FileFinders::FileNotFoundError
      nil
    end
  end
end

#get_file(pattern, context_path: nil, groups: nil, location: caller_locations.first) ⇒ Epuber::Compiler::FileTypes::AbstractFile

Find file and raise exception if not found

Parameters:

  • pattern (String)
  • context_path (String) (defaults to: nil)

    path to directory that is used as context for finding

  • groups (Array<Symbol>, Symbol) (defaults to: nil)
  • location (Thread::Backtrace::Location) (defaults to: caller_locations.first)

Returns:



86
87
88
89
90
91
92
93
94
# File 'lib/epuber/transformer/book_transformer.rb', line 86

def get_file(pattern, context_path: nil, groups: nil, location: caller_locations.first)
  file = find_file(pattern, context_path: context_path, groups: groups, location: location)
  unless file
    UI.error!("File with pattern #{pattern} not found (context path: #{context_path || 'nil'})",
              location: location)
  end

  file
end

#read_destination_file(pattern, context_path: nil, groups: nil, location: caller_locations.first) ⇒ String

Read file from destination path (raises exception if not found)

Parameters:

  • pattern (String)
  • context_path (String) (defaults to: nil)

    path to directory that is used as context for finding

  • groups (Array<Symbol>, Symbol) (defaults to: nil)
  • location (Thread::Backtrace::Location) (defaults to: caller_locations.first)

Returns:

  • (String)


105
106
107
108
# File 'lib/epuber/transformer/book_transformer.rb', line 105

def read_destination_file(pattern, context_path: nil, groups: nil, location: caller_locations.first)
  file = get_file(pattern, context_path: context_path, groups: groups, location: location)
  File.read(file.final_destination_path)
end

#write_destination_file(pattern, content, context_path: nil, groups: nil, location: caller_locations.first) ⇒ void

This method returns an undefined value.

Write file to destination path (raises exception if not found)

Parameters:

  • pattern (String)
  • content (String)
  • context_path (String) (defaults to: nil)

    path to directory that is used as context for finding

  • groups (Array<Symbol>, Symbol) (defaults to: nil)
  • location (Thread::Backtrace::Location) (defaults to: caller_locations.first)


120
121
122
123
# File 'lib/epuber/transformer/book_transformer.rb', line 120

def write_destination_file(pattern, content, context_path: nil, groups: nil, location: caller_locations.first)
  file = get_file(pattern, context_path: context_path, groups: groups, location: location)
  Compiler::FileTypes::AbstractFile.write_to_file(content, file.final_destination_path)
end