Class: GEPUB::ResourceBuilder

Inherits:
Object
  • Object
show all
Includes:
BuilderMixin
Defined in:
lib/gepub/resource_builder.rb

Defined Under Namespace

Classes: ResourceItem

Instance Method Summary collapse

Methods included from BuilderMixin

#method_missing

Constructor Details

#initialize(book, attributes = {}, &block) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gepub/resource_builder.rb', line 23

def initialize(book, attributes = {},  &block)
  @last_defined_item = nil
  @book = book
  @dir_prefix = ""
  @file_postprocess = {}
  @file_preprocess = {}
  @files_postprocess = {}
  @files_preprocess = {}
  current_wd = Dir.getwd
  Dir.chdir(attributes[:workdir]) unless attributes[:workdir].nil?
  instance_eval &block
  Dir.chdir current_wd
  true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GEPUB::BuilderMixin

Instance Method Details

#cover_image(val) ⇒ Object



99
100
101
102
# File 'lib/gepub/resource_builder.rb', line 99

def cover_image(val)
  file(val)
  @last_defined_item.cover_image
end

#fallback_chain_files(*arg) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/gepub/resource_builder.rb', line 145

def fallback_chain_files(*arg)
  files(*arg)
  @last_defined_item.inject(nil) {
    |item1, item2|
    if !item1.nil?
      item1.item.set_fallback(item2.item.id)
    end
    item2
  }
end

#fallback_groupObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gepub/resource_builder.rb', line 122

def fallback_group
  raise 'fallback_group needs block.' unless block_given?
  count = 0
  before = nil

  @files_preprocess['fallback_group'] = Proc.new { raise "can't use files within fallback_group" }

  @file_preprocess['fallback_group'] = Proc.new {
    before = @last_defined_item
  }

  @file_postprocess['fallback_group'] = Proc.new {
    if count > 0
      before.item.set_fallback(@last_defined_item.item.id)
    end
    count += 1
  }
  yield
  @files_preprocess.delete('fallback_group')
  @file_postprocess.delete('fallback_group')
  @file_preprocess.delete('fallback_group')
end

#file(val) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gepub/resource_builder.rb', line 44

def file(val)
  raise "can't specify multiple files on file keyword" if Hash === val && val.length > 1

  @file_preprocess.each {
    |k,p|
    p.call
  }
  @last_defined_item = ResourceItem.new(create_one_file(val))
  @file_postprocess.each {
    |k,p|
    p.call
  }
end

#files(*arg) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gepub/resource_builder.rb', line 58

def files(*arg)
  arg = arg[0] if arg.size == 1 && Hash === arg[0]
  @files_preprocess.each {
    |k,p|
    p.call
  }
  @last_defined_item = arg.map {
    |val|
    ResourceItem.new(create_one_file(val))
  }
  @files_postprocess.each {
    |k,p|
    p.call
  }
end

#glob(arg) ⇒ Object



86
87
88
# File 'lib/gepub/resource_builder.rb', line 86

def glob(arg)
  files(*Dir.glob(arg))
end

#handles(media_type) ⇒ Object



156
157
158
# File 'lib/gepub/resource_builder.rb', line 156

def handles(media_type)
  @last_defined_item.is_handler_of(media_type)
end

#heading(text, id = nil) ⇒ Object



109
110
111
# File 'lib/gepub/resource_builder.rb', line 109

def heading(text, id = nil)
  @last_defined_item.toc_text_with_id(text, id)
end

#import(conf, args = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/gepub/resource_builder.rb', line 90

def import(conf, args = {})
  dir_prefix_org = @dir_prefix
  @dir_prefix = args[:dir_prefix] || ""
  Dir.chdir(File.dirname(conf)) {
    instance_eval(File.new(File.basename(conf)).read)
  }
  @dir_prefix = dir_prefix_org
end


104
105
106
107
# File 'lib/gepub/resource_builder.rb', line 104

def nav(val)
  file(val)
  @last_defined_item.nav
end

#ordered(&block) ⇒ Object



38
39
40
41
42
# File 'lib/gepub/resource_builder.rb', line 38

def ordered(&block)
  @book.ordered {
    instance_eval &block
  }
end

#page_spread_leftObject



74
75
76
77
78
# File 'lib/gepub/resource_builder.rb', line 74

def page_spread_left
  itemref = @book.spine.itemref_by_id[@last_defined_item.item.id]
  raise 'page_spread_left should be called inside ordered' if (itemref.nil?)
  itemref.page_spread_left
end

#page_spread_rightObject



80
81
82
83
84
# File 'lib/gepub/resource_builder.rb', line 80

def page_spread_right
  itemref = @book.spine.itemref_by_id[@last_defined_item.item.id]
  raise 'page_spread_right should be called inside ordered' if (itemref.nil?)
  itemref.page_spread_right
end

#with_media_type(*type) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/gepub/resource_builder.rb', line 113

def with_media_type(*type)
  raise 'with_media_type needs block.' unless block_given?
  @file_postprocess['with_media_type'] = Proc.new { media_type(*type) }
  @files_postprocess['with_media_type'] = Proc.new { media_type(*type) }
  yield 
  @file_postprocess.delete('with_media_type')
  @files_postprocess.delete('with_media_type')
end