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

#add_resource_dir(name) ⇒ Object


121
122
123
# File 'lib/gepub/resource_builder.rb', line 121

def add_resource_dir(name)
  import "#{name}/resources.conf", :dir_prefix => name
end

#add_resource_dirs(dirs) ⇒ Object


125
126
127
128
129
130
# File 'lib/gepub/resource_builder.rb', line 125

def add_resource_dirs(dirs)
  dirs.each do
    |dir|
    add_resource_dir dir
  end
end

#cover_image(val) ⇒ Object


132
133
134
135
# File 'lib/gepub/resource_builder.rb', line 132

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

#fallback_chain_files(*arg) ⇒ Object


182
183
184
185
186
187
188
189
190
191
# File 'lib/gepub/resource_builder.rb', line 182

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


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gepub/resource_builder.rb', line 159

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


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

def glob(arg)
  files(*Dir.glob(arg).select{|x| !File.directory?(x)} )
end

#handles(media_type) ⇒ Object


193
194
195
# File 'lib/gepub/resource_builder.rb', line 193

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

#heading(text, id = nil) ⇒ Object


142
143
144
# File 'lib/gepub/resource_builder.rb', line 142

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

#id(the_id) ⇒ Object


146
147
148
# File 'lib/gepub/resource_builder.rb', line 146

def id(the_id)
  @last_defined_item.id = the_id
end

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


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

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

#linear(val) ⇒ Object


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

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

137
138
139
140
# File 'lib/gepub/resource_builder.rb', line 137

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

#rendition_layout(val) ⇒ Object


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

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

#rendition_orientation(val) ⇒ Object


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

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

#rendition_spread(val) ⇒ Object


96
97
98
99
100
# File 'lib/gepub/resource_builder.rb', line 96

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

#with_media_type(*type) ⇒ Object


150
151
152
153
154
155
156
157
# File 'lib/gepub/resource_builder.rb', line 150

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