Class: Configurator

Inherits:
Object
  • Object
show all
Includes:
File_Checking, Logging
Defined in:
lib/configurator.rb

Overview

This class reads and writes configuration data.

Direct Known Subclasses

Single_Configurator

Constant Summary collapse

CONFIG =

default configuration will be overwritten with user-defined values.

There shall be no instance-variables!

format("%shtml2slideshow.cfg", RD)

Instance Method Summary collapse

Methods included from Logging

#init_logger, #log_level=, #log_target=

Methods included from File_Checking

file_check, #file_check, last_mime_type, magic_check, mime_check

Constructor Details

#initialize(opts = nil) ⇒ Configurator

Returns a new instance of Configurator.



45
46
47
48
49
50
# File 'lib/configurator.rb', line 45

def initialize(opts = nil)
	init_logger()
	@configurations = Hash.new
	init_config(opts) 
	@log.debug(format("opts are of type %s", opts.class)) if @log
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args = nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/configurator.rb', line 52

def method_missing(method, args = nil)
	@log.debug("requested value for #{method}: #{@configurations[method]}") if @log
	if(@configurations.has_key?(method))
		@configurations[method]
	else
		super
	end
end

Instance Method Details

#complete_configuration(cfg) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/configurator.rb', line 69

def complete_configuration(cfg)
	if(cfg.respond_to?(:each_key))
		@configurations.each_key do |k|
			if(! cfg.has_key?(k) )
				cfg[k] = @configurations[k]
			end
		end
	end
	cfg
end

#consolidate_configuration(cfg) ⇒ Object

combines the values from the parameter-object and those, already present in the existing configuration



63
64
65
66
67
# File 'lib/configurator.rb', line 63

def consolidate_configuration(cfg)
	if(cfg.respond_to?(:each_key))
		cfg.each_key {|k| @configurations[k] = cfg[k] if cfg[k]}
	end
end

#init_config(opts = nil) ⇒ Object

The initial default configuration is merged with program options, or those from the configuration file.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/configurator.rb', line 91

def init_config(opts = nil)
	if (!@configurations || @configurations.empty?)
		@configurations =  {
			:date => Time.now.to_s,
                               :formats => Image::FORMATS,
			:target => './',
			:source => './',
			:suffix => '_slideshow',
			:source_type => 'HTML',
			:recursive => false,
			:browser => 'firefox',
			:log => '/tmp/html2slideshow.log',
			:merge => false,
			:scale_medium => '500',
			:scale_small => '300',
			:title => '',
		}
	end
	if(opts)
		if opts.respond_to?(:delete_field)
                           @log.debug('calling read_options')
			read_options(opts) 
		elsif opts.respond_to?(:to_hash)
                           @log.debug('calling read_hashed_options')
			read_hashed_options(opts)   
		end
	else
		read_from_file
	end
	@configurations.freeze
end

#save(opts) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/configurator.rb', line 79

def save(opts)
	@log.debug(format("shall write options to config-file", CONFIG)) if @log
	@configurations = complete_configuration(opts)
	if(!File.exist?(CONFIG) || !file_check(CONFIG, [:file?, :writable?]))
		@configurations[:date] = Time.now.to_s
		@log.debug(format("writing to config-file %s:\n%s", CONFIG, @configurations)) if @log
		File.open(CONFIG, 'w') {|cfg| YAML::dump(@configurations, cfg)}
	end
end