clamsy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/HISTORY.txt +7 -0
  2. data/README.rdoc +56 -15
  3. data/VERSION +1 -1
  4. data/clamsy.gemspec +51 -16
  5. data/clamsy.png +0 -0
  6. data/lib/clamsy.rb +26 -123
  7. data/lib/clamsy.yml +8 -0
  8. data/lib/clamsy/base_printer.rb +64 -0
  9. data/lib/clamsy/configuration.rb +128 -0
  10. data/lib/clamsy/cups_pdf_printer.rb +26 -0
  11. data/lib/clamsy/file_system_support.rb +36 -0
  12. data/lib/clamsy/template_open_doc.rb +122 -0
  13. data/spec/clamsy/base_printer_spec.rb +19 -0
  14. data/spec/clamsy/configuration_spec.rb +290 -0
  15. data/spec/clamsy/cups_pdf_printer_spec.rb +40 -0
  16. data/spec/{data → clamsy/data}/clamsy.png +0 -0
  17. data/spec/{data → clamsy/data}/clamsy2.png +0 -0
  18. data/spec/clamsy/data/embedded_ruby_after.odt +0 -0
  19. data/spec/clamsy/data/embedded_ruby_before.odt +0 -0
  20. data/spec/clamsy/data/escaped_text_after.odt +0 -0
  21. data/spec/{data/escaped_text_example.odt → clamsy/data/escaped_text_before.odt} +0 -0
  22. data/spec/clamsy/data/invalid_content_example.odt +0 -0
  23. data/spec/clamsy/data/invalid_zip_example.odt +0 -0
  24. data/spec/{data/multiple_contexts_example.odt → clamsy/data/multiple_contexts_after.odt} +0 -0
  25. data/spec/{data/plain_text_example.odt → clamsy/data/multiple_contexts_before.odt} +0 -0
  26. data/spec/clamsy/data/picture_after.odt +0 -0
  27. data/spec/{data/picture_example.odt → clamsy/data/picture_before.odt} +0 -0
  28. data/spec/clamsy/data/plain_text_after.odt +0 -0
  29. data/spec/clamsy/data/plain_text_before.odt +0 -0
  30. data/spec/clamsy/file_system_support_spec.rb +93 -0
  31. data/spec/clamsy/invalid_printer_spec.rb +21 -0
  32. data/spec/clamsy/template_open_doc_spec.rb +95 -0
  33. data/spec/{clamsy_spec.rb → integration/cups_pdf_printer_spec.rb} +12 -6
  34. data/spec/{data → integration/data}/embedded_ruby_example.odt +0 -0
  35. data/spec/{data → integration/data}/embedded_ruby_example.pdf +0 -0
  36. data/spec/integration/data/escaped_text_example.odt +0 -0
  37. data/spec/{data → integration/data}/escaped_text_example.pdf +0 -0
  38. data/spec/integration/data/multiple_contexts_example.odt +0 -0
  39. data/spec/{data → integration/data}/multiple_contexts_example.pdf +0 -0
  40. data/spec/integration/data/norm_clamsy.png +0 -0
  41. data/spec/integration/data/picture_example.odt +0 -0
  42. data/spec/integration/data/picture_example.pdf +0 -0
  43. data/spec/integration/data/plain_text_example.odt +0 -0
  44. data/spec/{data → integration/data}/plain_text_example.pdf +0 -0
  45. data/spec/integration/data/sunny_clamsy.png +0 -0
  46. data/spec/integration/has_stardand_integration_support_shared_spec.rb +27 -0
  47. data/spec/spec_helper.rb +1 -25
  48. metadata +52 -17
  49. data/spec/data/picture_example.pdf +0 -0
@@ -0,0 +1,128 @@
1
+ require 'yaml'
2
+
3
+ module Clamsy
4
+
5
+ class ConfigFileSettingNotSupportedError < Exception ; end
6
+
7
+ module Configuration
8
+
9
+ ENV_REPLACE_PATTERN = /(\$\{(.*?)\})/
10
+
11
+ def self.new(file, is_base_config=false)
12
+ if (config = YAML.load_file(path = File.expand_path(file)) rescue nil)
13
+ config = replace_with_env_vars(config).merge(:config_src => path)
14
+ is_base_config ? BundledFileConfig.new(config) : UserFileConfig.new(config)
15
+ end
16
+ end
17
+
18
+ def method_missing(method, *args)
19
+ !"#{method}".include?('=') ? get_config_var(method) :
20
+ set_config_var("#{method}".sub('=','').to_sym, args[0])
21
+ end
22
+
23
+ protected
24
+
25
+ def initialize_config_vars(config)
26
+ @config_vars = {
27
+ :printer => (printer = config['printer']),
28
+ :printer_specific => (config[printer] || {}).
29
+ inject({}) {|memo, args| memo.merge(args[0].to_sym => args[1]) }
30
+ }
31
+ end
32
+
33
+ def set_config_var(name, value)
34
+ case name
35
+ when :printer then @config_vars[name] = value
36
+ else (@config_vars[:printer_specific] ||= {})[name] = value
37
+ end
38
+ end
39
+
40
+ def get_config_var(name)
41
+ case name
42
+ when :printer then @config_vars[name]
43
+ else (@config_vars[:printer_specific] ||= {})[name]
44
+ end
45
+ end
46
+
47
+ def self.replace_with_env_vars(hash)
48
+ hash.inject({}) do |memo, args|
49
+ key, val = args
50
+ if val.is_a?(Hash)
51
+ memo.merge(key => replace_with_env_vars(val))
52
+ elsif !val.nil?
53
+ while val[ENV_REPLACE_PATTERN,1]
54
+ (m = val.match(ENV_REPLACE_PATTERN)) && val.sub!(m[1], ENV[m[2]] || '')
55
+ end
56
+ memo.merge(key => val)
57
+ else
58
+ memo
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ private
66
+
67
+ class BundledFileConfig
68
+
69
+ include Configuration
70
+ include FileSystemSupport
71
+ attr_reader :config_file
72
+
73
+ def initialize(config)
74
+ initialize_config_vars(config)
75
+ @config_file = config['config_file']
76
+ @configs = {
77
+ :default => self, :user_proc => UserProcConfig.new({}),
78
+ :user_config => Configuration.new(@config_file)
79
+ }
80
+ end
81
+
82
+ def config_file=(file)
83
+ file_must_exist!(@config_file = file)
84
+ @configs[:user_config] = Configuration.new(file)
85
+ end
86
+
87
+ private
88
+
89
+ def set_config_var(name, value)
90
+ @configs[:user_proc].send(:"#{name}=", value)
91
+ end
92
+
93
+ def get_config_var(name)
94
+ key = [:user_proc, :user_config].find do |k|
95
+ @configs[k] && (val = @configs[k].send(name)) && !"#{val}".strip.empty?
96
+ end
97
+ key ? @configs[key].send(name) : super(name)
98
+ end
99
+
100
+ end
101
+
102
+ class UserFileConfig
103
+
104
+ include Configuration
105
+
106
+ def initialize(config)
107
+ ensure_valid_config!(config)
108
+ initialize_config_vars(config)
109
+ end
110
+
111
+ private
112
+
113
+ def ensure_valid_config!(config)
114
+ if file = config['config_file']
115
+ raise ConfigFileSettingNotSupportedError.new \
116
+ "Specifying of 'config_file' setting in user config '%s' is not supported." %
117
+ config[:config_src]
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ class UserProcConfig
124
+ include Configuration
125
+ def initialize(config) ; initialize_config_vars(config) ; end
126
+ end
127
+
128
+ end
@@ -0,0 +1,26 @@
1
+ module Clamsy
2
+ class CupsPdfPrinter < BasePrinter
3
+ class << self
4
+
5
+ private
6
+
7
+ def doc_to_pdf(doc_path)
8
+ system("#{config.ooffice_cmd} #{doc_path}")
9
+ pdf_path = tmp_pdf_path(doc_path)
10
+ file_must_exist!(pdf_path, 10) ; pdf_path
11
+ end
12
+
13
+ def tmp_pdf_path(doc_path)
14
+ if output_file = config.cups_output_file
15
+ output_file.is_a?(Proc) ? output_file.call : output_file
16
+ else
17
+ # NOTE: we don't attempt to trim away the extension cos it is a hard
18
+ # fact that internally we are always working with paths derived from
19
+ # Tempfile instances, thus, no need for extension replacing.
20
+ File.join(config.cups_output_dir, File.basename(doc_path)) + '.pdf'
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'tempfile'
2
+ require 'digest/md5'
3
+
4
+ module Clamsy
5
+
6
+ class FileNotFoundError < Exception ; end
7
+
8
+ module FileSystemSupport
9
+
10
+ def file_must_exist!(path, timeout=0)
11
+ if timeout.zero?
12
+ File.exists?(path) or raise_file_not_found_error(path)
13
+ else
14
+ 0.upto(timeout.pred) {|i| File.exists?(path) ? (return true) : sleep(1) }
15
+ raise_file_not_found_error(path)
16
+ end
17
+ end
18
+
19
+ def trash_tmp_files
20
+ (@trashable_tmp_files || []).each {|f| f.path && f.unlink }
21
+ @trashable_tmp_files = nil
22
+ end
23
+
24
+ def tmp_file(file_name = nil)
25
+ file_name ||= Digest::MD5.hexdigest(Time.now.to_s)
26
+ ((@trashable_tmp_files ||= []) << Tempfile.new(file_name))[-1]
27
+ end
28
+
29
+ protected
30
+
31
+ def raise_file_not_found_error(path)
32
+ raise Clamsy::FileNotFoundError.new("File '#{path}' not found.")
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,122 @@
1
+ require 'digest/md5'
2
+ require 'nokogiri'
3
+ require 'zip/zip'
4
+ require 'ftools'
5
+
6
+ module Clamsy
7
+
8
+ class TemplateDocIsNotFoundError < Exception ; end
9
+ class TemplateDocIsCorruptedError < Exception ; end
10
+ class TemplateDocContentIsCorruptedError < Exception ; end
11
+ class RenderedDocContentIsCorruptedError < Exception ; end
12
+
13
+ class TemplateOpenDoc
14
+
15
+ include Clamsy::FileSystemSupport
16
+
17
+ def initialize(template_doc)
18
+ file_must_exist!(@template_doc = template_doc)
19
+ initialize_template_workers
20
+ end
21
+
22
+ def render(context)
23
+ File.copy(@template_doc, (file = tmp_file).path)
24
+ OpenDoc.new(file, @template_workers, context).transform
25
+ end
26
+
27
+ private
28
+
29
+ def initialize_template_workers
30
+ begin
31
+ OpenDoc.per_content_entry(@template_doc) \
32
+ {|@entry| (@template_workers_cache ||= {})[@entry.to_s] = template_worker }
33
+ @template_workers = lambda {|entry| @template_workers_cache[entry.to_s] }
34
+ rescue Zip::ZipError
35
+ raise TemplateDocIsCorruptedError.new \
36
+ "Template doc '#{@template_doc}' is corrupted."
37
+ end
38
+ end
39
+
40
+ def template_worker
41
+ file, content = tmp_file, @entry.get_input_stream.read
42
+ File.open(file.path, 'w') {|f| f.write(content) }
43
+ enhance_worker_with_picture_paths(Tenjin::Template.new(file.path), content)
44
+ end
45
+
46
+ def enhance_worker_with_picture_paths(worker, content)
47
+ begin
48
+ class << worker ; attr_accessor :picture_paths ; end
49
+ worker.picture_paths = OpenDoc.extract_picture_paths(content)
50
+ worker
51
+ rescue Nokogiri::XML::SyntaxError
52
+ raise TemplateDocContentIsCorruptedError.new \
53
+ "Template doc content '#{@template_doc}'/'#{@entry.to_s}' is corrupted."
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ private
60
+
61
+ class OpenDoc
62
+
63
+ def self.per_content_entry(file, &blk)
64
+ Zip::ZipFile.open(file) do |zip|
65
+ zip.select {|entry| entry.file? && entry.to_s =~ /\.xml$/ }.each do |entry|
66
+ class << entry ; attr_accessor :zip ; end
67
+ entry.zip = zip
68
+ yield(entry)
69
+ end
70
+ end
71
+ end
72
+
73
+ def self.string_to_xml_doc(string)
74
+ Nokogiri::XML(string.gsub(':','')) do |config|
75
+ config.options = Nokogiri::XML::ParseOptions::STRICT
76
+ end
77
+ end
78
+
79
+ def self.extract_picture_paths(content)
80
+ doc = string_to_xml_doc(content)
81
+ nodes = lambda {|path| doc.xpath(path) }
82
+ nodes['//drawframe/@drawname'].inject({}) do |memo, node|
83
+ path = nodes[%\//drawframe[@drawname="#{node.value}"]//drawimage/@xlinkhref\][0]
84
+ memo.merge(path ? {:"#{node.value}" => path.value} : {})
85
+ end
86
+ end
87
+
88
+ def initialize(file, workers, context)
89
+ @file, @workers, @context = file, workers, context
90
+ end
91
+
92
+ def transform
93
+ OpenDoc.per_content_entry(@file.path) do |@entry|
94
+ @entry.zip.get_output_stream(@entry.to_s) do |@io|
95
+ replace_texts ; replace_pictures
96
+ end
97
+ end
98
+ @file
99
+ end
100
+
101
+ private
102
+
103
+ def replace_texts
104
+ begin
105
+ @io.write(content = @workers[@entry].render(@context))
106
+ OpenDoc.string_to_xml_doc(content)
107
+ rescue Nokogiri::XML::SyntaxError
108
+ raise Clamsy::RenderedDocContentIsCorruptedError.new \
109
+ 'Rendered doc content is corrupted, use ${ ... } where text escaping is needed.'
110
+ end
111
+ end
112
+
113
+ def replace_pictures
114
+ (@context[:_pictures] || {}).each do |name, src_path|
115
+ (dest_path = @workers[@entry].picture_paths[:"#{name}"]) \
116
+ && @entry.zip.replace(dest_path, src_path)
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe 'Base printer' do
4
+
5
+ describe '> getting any printer' do
6
+
7
+ should 'raise Clamsy::PrinterNotFoundError when printer is not yet defined' do
8
+ lambda { Clamsy::BasePrinter.get('super_duper', 'dummy_config') }.
9
+ should.raise(Clamsy::PrinterNotFoundError).
10
+ message.should.equal("Printer 'super_duper' cannot be found.")
11
+ end
12
+
13
+ should "return Clamsy::CupsPdfPrinter when printer is 'cups_pdf'" do
14
+ Clamsy::BasePrinter.get('cups_pdf', 'dummy_config').should.equal Clamsy::CupsPdfPrinter
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,290 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ def Clamsy.unconfigure
4
+ @config = nil
5
+ end
6
+
7
+ describe "Clamsy configuration" do
8
+
9
+ before do
10
+ Clamsy.unconfigure
11
+ $bundled_config_file = File.join(File.dirname(__FILE__),'..','..','lib','clamsy.yml')
12
+ $user_config_file = Tempfile.new('clamsy.yml').path
13
+ @write_config = lambda do |data|
14
+ File.open($user_config_file,'w') {|io| YAML.dump(data, io) }
15
+ end
16
+ end
17
+
18
+ after do
19
+ class << Clamsy::Configuration
20
+ alias_method :new, :orig_new
21
+ end
22
+ end
23
+
24
+ describe '> default (w missing default user config file)' do
25
+
26
+ before do
27
+ class << Clamsy::Configuration
28
+ alias_method :orig_new, :new
29
+ def new(file, is_base_config=false)
30
+ file == '~/.clamsy.yml' ? nil : orig_new($bundled_config_file, true)
31
+ end
32
+ end
33
+ end
34
+
35
+ should "have :printer as 'cups_pdf'" do
36
+ Clamsy.configure do |config|
37
+ config.printer.should.equal 'cups_pdf'
38
+ end
39
+ end
40
+
41
+ should "have :config_file as '~/.clamsy.yml'" do
42
+ Clamsy.configure do |config|
43
+ config.config_file.should.equal '~/.clamsy.yml'
44
+ end
45
+ end
46
+
47
+ should "have :cups_output_dir (cups_pdf) as '/var/spool/cups-pdf/#{ENV['USER']}'" do
48
+ Clamsy.configure do |config|
49
+ config.cups_output_dir.should.equal "/var/spool/cups-pdf/#{ENV['USER']}"
50
+ end
51
+ end
52
+
53
+ should "have :cups_output_file (cups_pdf) be nil (unspecified)" do
54
+ Clamsy.configure do |config|
55
+ config.cups_output_file.should.equal nil
56
+ end
57
+ end
58
+
59
+ should "have :ooffice_cmd (cups_pdf) as " +
60
+ "'ooffice -norestore -nofirststartwizard -nologo -headless -pt Cups-PDF'" do
61
+ Clamsy.configure do |config|
62
+ config.ooffice_cmd.should.equal \
63
+ 'ooffice -norestore -nofirststartwizard -nologo -headless -pt Cups-PDF'
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ describe '> configuring (using default user config file)' do
70
+
71
+ before do
72
+ class << Clamsy::Configuration
73
+ alias_method :orig_new, :new
74
+ def new(file, is_base_config=false)
75
+ file != '~/.clamsy.yml' ?
76
+ orig_new($bundled_config_file, true) : orig_new($user_config_file)
77
+ end
78
+ end
79
+ end
80
+
81
+ should "use specified :printer if it has been specified" do
82
+ @write_config[{'printer' => 'dummy'}]
83
+ Clamsy.configure {|config| config.printer.should.equal 'dummy' }
84
+ end
85
+
86
+ should "use default :printer if it has not been specified" do
87
+ @write_config[{}]
88
+ Clamsy.configure {|config| config.printer.should.equal 'cups_pdf' }
89
+ end
90
+
91
+ should "raise Clamsy::ConfigFileSettingNotSupportedError if :config_file has been specified" do
92
+ @write_config[{'config_file' => 'ddd'}]
93
+ lambda { Clamsy.configure {|config| 'do nothing here' } }.
94
+ should.raise(Clamsy::ConfigFileSettingNotSupportedError).
95
+ message.should.equal(
96
+ "Specifying of 'config_file' setting in user config '#{$user_config_file}' is not supported."
97
+ )
98
+ end
99
+
100
+ should 'use specified printer-specific settings if they have been specified' do
101
+ @write_config[{'printer' => 'cups_pdf', 'cups_pdf' => {'cups_output_dir' => '/x/y/z'}}]
102
+ Clamsy.configure {|config| config.cups_output_dir.should.equal '/x/y/z' }
103
+ end
104
+
105
+ should 'use default printer-specific settings if they have not been specified' do
106
+ Clamsy.configure do |config|
107
+ config.cups_output_dir.should.equal "/var/spool/cups-pdf/#{ENV['USER']}"
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ describe '> configuring (using only config proc)' do
114
+
115
+ behaves_like 'has standard files support'
116
+
117
+ before do
118
+ class << Clamsy::Configuration
119
+ alias_method :orig_new, :new
120
+ def new(file, is_base_config=false)
121
+ file == '~/.clamsy.yml' ? nil : orig_new($bundled_config_file, true)
122
+ end
123
+ end
124
+ end
125
+
126
+ should 'use specified :printer if it has been specified' do
127
+ Clamsy.configure do |config|
128
+ config.printer = 'dummy'
129
+ config.printer.should.equal 'dummy'
130
+ end
131
+ end
132
+
133
+ should "use default :printer if it has not been specified" do
134
+ Clamsy.configure do |config|
135
+ config.printer.should.equal 'cups_pdf'
136
+ [nil, ''].each do |val|
137
+ config.printer = val
138
+ config.printer.should.equal 'cups_pdf'
139
+ end
140
+ end
141
+ end
142
+
143
+ should "use specified :config_file if it has been specified" do
144
+ Clamsy.configure do |config|
145
+ config.config_file = $user_config_file
146
+ config.config_file.should.equal $user_config_file
147
+ end
148
+ end
149
+
150
+ should "use raise Clamsy::FileNotFoundError if the specified config file does not exist" do
151
+ lambda {
152
+ Clamsy.configure {|config| config.config_file = "#{__FILE__}.clamsy.yml" }
153
+ }.should.raise(Clamsy::FileNotFoundError).message.should.equal(
154
+ "File '#{__FILE__}.clamsy.yml' not found."
155
+ )
156
+ end
157
+
158
+ should 'use specified printer-specific settings if they have been specified' do
159
+ Clamsy.configure do |config|
160
+ config.cups_output_dir = '/x/y/z'
161
+ config.cups_output_dir.should.equal '/x/y/z'
162
+ end
163
+ end
164
+
165
+ should 'use default printer-specific settings if they have not been specified' do
166
+ Clamsy.configure do |config|
167
+ config.cups_output_dir.should.equal "/var/spool/cups-pdf/#{ENV['USER']}"
168
+ end
169
+ end
170
+
171
+ end
172
+
173
+ describe '> configuring (using only specified user config file in config proc)' do
174
+
175
+ should 'use specified :printer if it has been specified' do
176
+ @write_config[{'printer' => 'dummy'}]
177
+ Clamsy.configure do |config|
178
+ config.config_file = $user_config_file
179
+ config.printer.should.equal 'dummy'
180
+ end
181
+ end
182
+
183
+ should 'use default :printer if it has not been specified' do
184
+ @write_config[{}]
185
+ Clamsy.configure do |config|
186
+ config.config_file = $user_config_file
187
+ config.printer.should.equal 'cups_pdf'
188
+ end
189
+ end
190
+
191
+ should "raise Clamsy::ConfigFileSettingNotSupportedError if :config_file has been specified" do
192
+ @write_config[{'config_file' => 'ddd'}]
193
+ lambda {
194
+ Clamsy.configure {|config| config.config_file = $user_config_file }
195
+ }.should.raise(Clamsy::ConfigFileSettingNotSupportedError).
196
+ message.should.equal(
197
+ "Specifying of 'config_file' setting in user config '#{$user_config_file}' is not supported."
198
+ )
199
+ end
200
+
201
+ should 'use specified printer-specific settings if they have been specified' do
202
+ @write_config[{'printer' => 'cups_pdf', 'cups_pdf' => {'cups_output_dir' => '/x/y/z'}}]
203
+ Clamsy.configure do |config|
204
+ config.config_file = $user_config_file
205
+ config.cups_output_dir.should.equal '/x/y/z'
206
+ end
207
+ end
208
+
209
+ should 'use default printer-specific settings if they have not been specified' do
210
+ @write_config[{}]
211
+ Clamsy.configure do |config|
212
+ config.config_file = $user_config_file
213
+ config.cups_output_dir.should.equal "/var/spool/cups-pdf/#{ENV['USER']}"
214
+ end
215
+ end
216
+
217
+ end
218
+
219
+ describe '> configuring (with clashes in specified user config file & config proc)' do
220
+
221
+ should 'use proc specified :printer if it is specified in both config proc & file' do
222
+ @write_config[{'printer' => 'dummy'}]
223
+ Clamsy.configure do |config|
224
+ # NOTE: it doesn't matter how we order the declaration !!
225
+ config.config_file = $user_config_file
226
+ config.printer = 'mummy'
227
+ config.printer.should.equal 'mummy'
228
+ config.config_file = $user_config_file
229
+ config.printer.should.equal 'mummy'
230
+ end
231
+ end
232
+
233
+ should 'use proc specified printer-specific settings if they are specified in both config proc & file' do
234
+ @write_config[{'printer' => 'cups_pdf', 'cups_pdf' => {'cups_output_dir' => '/x/y/x'}}]
235
+ Clamsy.configure do |config|
236
+ # NOTE: it doesn't matter how we order the declaration !!
237
+ config.config_file = $user_config_file
238
+ config.cups_output_dir = '/a/b/c'
239
+ config.cups_output_dir.should.equal '/a/b/c'
240
+ config.config_file = $user_config_file
241
+ config.cups_output_dir.should.equal '/a/b/c'
242
+ end
243
+ end
244
+
245
+ end
246
+
247
+ describe '> configuring can also be done while doing processing' do
248
+
249
+ before do
250
+ class << Clamsy
251
+ def generate_pdf(*args) ; end
252
+ end
253
+ end
254
+
255
+ should 'use specified :printer if it has been specified' do
256
+ Clamsy.process([], '/dummy/template/doc', '/dummy/final/pdf') do |config|
257
+ config.printer = 'dummy'
258
+ end
259
+ Clamsy.configure {|config| config.printer.should.equal 'dummy' }
260
+ end
261
+
262
+ should 'use default :printer if it has not been specified' do
263
+ Clamsy.process([], '/dummy/template/doc', '/dummy/final/pdf')
264
+ Clamsy.configure {|config| config.printer.should.equal 'cups_pdf' }
265
+ end
266
+
267
+ end
268
+
269
+ describe '> retrieving of config values' do
270
+
271
+ should 'replace any ${...} with matching environment variable value' do
272
+ @write_config[{'printer' => 'dummy', 'dummy' => {'output_dir' => '/home/${USER}'}}]
273
+ Clamsy.configure do |config|
274
+ config.config_file = $user_config_file
275
+ config.output_dir.should.equal "/home/#{ENV['USER']}"
276
+ end
277
+ end
278
+
279
+ should "replace any ${...} wo matching environment variable with blank string" do
280
+ @write_config[{'printer' => 'dummy', 'dummy' => {'output_dir' => '/home/${WHATEVER}'}}]
281
+ Clamsy.configure do |config|
282
+ config.config_file = $user_config_file
283
+ config.output_dir.should.equal "/home/"
284
+ end
285
+ end
286
+
287
+ end
288
+
289
+ end
290
+