Class: Watirmark::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/watirmark/configuration.rb

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



10
11
12
13
14
# File 'lib/watirmark/configuration.rb', line 10

def initialize
  @settings         = {}
  @runtime_defaults = {}
  reload
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/watirmark/configuration.rb', line 85

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Instance Method Details

#[](key) ⇒ Object



72
73
74
# File 'lib/watirmark/configuration.rb', line 72

def [](key)
  @settings[key.to_sym]
end

#[]=(key, value) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/watirmark/configuration.rb', line 76

def []=(key, value)
  override_method = "#{key}_value".to_sym
  if respond_to? override_method
    @settings[key.to_sym] = self.send override_method, value
  else
    @settings[key.to_sym] = value
  end
end

#dbObject



93
94
95
96
# File 'lib/watirmark/configuration.rb', line 93

def db
  @db = nil if (@db && @db.respond_to?(:dbh) && @db.dbh.handle == nil)
  @db ||= WatirmarkDB::DB.new(self.hostname, self.dbhostname, self.dbusername, self.dbpassword, self.dbsid, self.dbport)
end

#defaultsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/watirmark/configuration.rb', line 16

def defaults
  {
    :configfile         => nil,
    :hostname           => nil,
    :email              => 'devnull',
    :closebrowseronexit => false,
    :loglevel           => Logger::INFO,
    :uuid               => nil,
    :webdriver          => :firefox,
    :headless           => false,
    :always_locate      => true,
    :prefer_css         => false,
    :watir_timeout      => 30,
    :http_timeout       => 60,
    :firebug            => false,
    # database
    :dbhostname         => nil,
    :dbusername         => nil,
    :dbpassword         => nil,
    :dbsid              => nil,
    :dbport             => nil,
    # snapshots
    :snapshotwidth      => 1000,
    :snapshotheight     => 1000,
    :projectpath        => nil,
    :sauce_username     => nil,
    :sauce_access_key   => nil,
    :dbi_url            => nil,

    #to deprecate
    :profile            => Hash.new { |h, k| h[k] = Hash.new },
    :profile_name       => :undefined,

  }.merge @runtime_defaults
end

#defaults=(x) ⇒ Object



52
53
54
55
# File 'lib/watirmark/configuration.rb', line 52

def defaults=(x)
  @runtime_defaults.merge! x
  reload
end

#inspectObject



64
65
66
# File 'lib/watirmark/configuration.rb', line 64

def inspect
  @settings.inspect
end

#loggerObject



138
139
140
141
142
143
144
# File 'lib/watirmark/configuration.rb', line 138

def logger
  @logger ||= begin
    log = Logger.new STDOUT
    log.formatter = proc {|severity, datetime, progname, msg| "[#{datetime}] #{msg}\n"}
    log
  end
end

#loglevel=(x) ⇒ Object



146
147
148
# File 'lib/watirmark/configuration.rb', line 146

def loglevel=(x)
  logger.level = x
end

#read_from_environmentObject

The variable needs to be set as a default here or in the library to be read from the environment



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/watirmark/configuration.rb', line 118

def read_from_environment
  @settings.each_key do |var|
    next if var.to_s.upcase == "USERNAME"
    next if var.to_s.upcase == "HOSTNAME" && self.hostname
    env = ENV[var.to_s.upcase]
    if var == :webdriver
      ENV['JOB_NAME']=~ /WEBDRIVER=(\w+)/
      env ||= $1
    end
    update_key var, env if env
  end
end

#read_from_fileObject Also known as: read

This will read in ANY variable set in a configuration file



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/watirmark/configuration.rb', line 100

def read_from_file
  return unless File.exists?(configfile.to_s)
  filename = File.expand_path(configfile)
  case File.extname filename
    when ".txt", ".hudson"
      parse_text_file filename
    when ".yml"
      parse_yaml_file filename
    else
      Watirmark.logger.warn "Unsure how to handle configuration file #{configfile}. Assuming .txt"
      parse_text_file filename
  end
end

#reloadObject



131
132
133
134
135
136
# File 'lib/watirmark/configuration.rb', line 131

def reload
  update(defaults)
  read_from_file
  read_from_environment
  initialize_logger
end

#resetObject



68
69
70
# File 'lib/watirmark/configuration.rb', line 68

def reset
  @settings.each_key { |key| @settings.delete key }
end

#update(values) ⇒ Object



57
58
59
60
61
62
# File 'lib/watirmark/configuration.rb', line 57

def update(values)
  values.each_pair { |k, v|
    v = Logger.const_get(v.upcase) if k.to_s == "loglevel" && v.class == String
    self[k] = v
  }
end