Class: SiteDiff::Config
- Inherits:
-
Object
- Object
- SiteDiff::Config
- Defined in:
- lib/sitediff/config.rb,
lib/sitediff/config/creator.rb
Defined Under Namespace
Classes: ConfigNotFound, Creator, InvalidConfig
Constant Summary collapse
- DEFAULT_FILENAME =
'sitediff.yaml'- CONF_KEYS =
keys allowed in configuration files
Sanitizer::TOOLS.values.flatten(1) + %w[paths before after before_url after_url includes curl_opts]
Class Method Summary collapse
-
.merge(first, second) ⇒ Object
Merges two normalized Hashes according to the following rules: 1 paths are merged as arrays.
-
.normalize(conf) ⇒ Object
Takes a Hash and normalizes it to the following form by merging globals into before and after.
Instance Method Summary collapse
- #after ⇒ Object
- #before ⇒ Object
-
#initialize(files, dir) ⇒ Config
constructor
A new instance of Config.
- #paths ⇒ Object
- #paths=(paths) ⇒ Object
-
#validate(opts = {}) ⇒ Object
Checks if the configuration is usable for diff-ing.
Constructor Details
#initialize(files, dir) ⇒ Config
Returns a new instance of Config.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sitediff/config.rb', line 88 def initialize(files, dir) @config = { 'paths' => [], 'before' => {}, 'after' => {} } files = [File.join(dir, DEFAULT_FILENAME)] if files.empty? files.each do |file| unless File.exist?(file) raise InvalidConfig, format('Missing config file %s.', File.(file)) end @config = Config.merge(@config, Config.load_conf(file)) end end |
Class Method Details
.merge(first, second) ⇒ Object
Merges two normalized Hashes according to the following rules: 1 paths are merged as arrays. 2 before and after: for each subhash H (e.g. [‘before’]):
a) if first[H] and second[H] are expected to be arrays, their values
are merged as such,
b) if first[H] and second[H] are expected to be scalars, the value for
second[H] is kept if and only if first[H] is nil.
For example, merge(h1, h2) results in h3:
(h1) before: foo, sanitization: [pattern: foo] (h2) before: bar, sanitization: [pattern: bar] (h3) before: foo, sanitization: [pattern: foo, pattern: bar]
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sitediff/config.rb', line 69 def self.merge(first, second) result = { 'paths' => {}, 'before' => {}, 'after' => {} } result['paths'] = (first['paths'] || []) + (second['paths'] || []) # rule 1 %w[before after].each do |pos| unless first[pos] result[pos] = second[pos] || {} next end result[pos] = first[pos].merge!(second[pos]) do |key, a, b| result[pos][key] = if Sanitizer::TOOLS[:array].include? key # rule 2a (a || []) + (b || []) else a || b # rule 2b end end end result end |
.normalize(conf) ⇒ Object
Takes a Hash and normalizes it to the following form by merging globals into before and after. A normalized config Hash looks like this:
paths:
- /about
before:
url: http://before
selector: body
dom_transform:
- type: remove
selector: script
after:
url: http://after
selector: body
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sitediff/config.rb', line 36 def self.normalize(conf) tools = Sanitizer::TOOLS # merge globals %w[before after].each do |pos| conf[pos] ||= {} tools[:array].each do |key| conf[pos][key] ||= [] conf[pos][key] += conf[key] if conf[key] end tools[:scalar].each { |key| conf[pos][key] ||= conf[key] } conf[pos]['url'] ||= conf[pos + '_url'] conf[pos]['curl_opts'] = conf['curl_opts'] end # normalize paths conf['paths'] = Config.normalize_paths(conf['paths']) conf.select { |k, _v| %w[before after paths curl_opts].include? k } end |
Instance Method Details
#after ⇒ Object
105 106 107 |
# File 'lib/sitediff/config.rb', line 105 def after @config['after'] end |
#before ⇒ Object
101 102 103 |
# File 'lib/sitediff/config.rb', line 101 def before @config['before'] end |
#paths ⇒ Object
109 110 111 |
# File 'lib/sitediff/config.rb', line 109 def paths @config['paths'] end |
#paths=(paths) ⇒ Object
113 114 115 |
# File 'lib/sitediff/config.rb', line 113 def paths=(paths) @config['paths'] = Config.normalize_paths(paths) end |
#validate(opts = {}) ⇒ Object
Checks if the configuration is usable for diff-ing.
118 119 120 121 122 123 124 125 |
# File 'lib/sitediff/config.rb', line 118 def validate(opts = {}) opts = { need_before: true }.merge(opts) raise InvalidConfig, "Undefined 'before' base URL." if \ opts[:need_before] && !before['url'] raise InvalidConfig, "Undefined 'after' base URL." unless after['url'] raise InvalidConfig, "Undefined 'paths'." unless paths && !paths.empty? end |