Class: SiteDiff::Config::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/sitediff/config/creator.rb

Overview

SiteDiff Config Creator Object.

Instance Method Summary collapse

Constructor Details

#initialize(debug, before, after) ⇒ Creator

Creates a Creator object.



17
18
19
20
21
22
# File 'lib/sitediff/config/creator.rb', line 17

def initialize(debug, before, after)
  @config = nil
  @before = before
  @after = after
  @debug = debug
end

Instance Method Details

#build_config(options) ⇒ Object

Build and populate the config object which is being created.

Parameters:

  • options (String)

    One or more options.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sitediff/config/creator.rb', line 55

def build_config(options)
  options = Config.stringify_keys options

  # Build config for "before" and "after".
  %w[before after].each do |tag|
    next unless (url = roots[tag])

    @config[tag] = { 'url' => url }
  end

  # Build other settings.
  @config['settings'] = {}
  Config::ALLOWED_SETTINGS_KEYS.each do |key|
    @config['settings'][key] = options[key]
  end
end

#config_fileObject

Returns the name of the config file.



102
103
104
# File 'lib/sitediff/config/creator.rb', line 102

def config_file
  @dir + Config::DEFAULT_FILENAME
end

#create(options) ⇒ Object

Build a config structure, return it.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sitediff/config/creator.rb', line 34

def create(options)
  @config = {}

  # @callback = block

  @dir = Pathname.new(options[:directory])

  # Setup instance vars
  @paths = Hash.new { |h, k| h[k] = Set.new }
  @cache = Cache.new(directory: @dir.to_s, create: true)
  @cache.write_tags << :before << :after

  build_config options
  write_config
end

#directoryObject

Returns the name of the config directory.



96
97
98
# File 'lib/sitediff/config/creator.rb', line 96

def directory
  @dir
end

#make_gitignore(dir) ⇒ Object

Create a gitignore if we seem to be in git.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sitediff/config/creator.rb', line 74

def make_gitignore(dir)
  # Check if we're in git
  unless dir.realpath.to_enum(:ascend).any? { |d| Dir.exist?("#{d}/.git") }
    return
  end

  f = File.open("#{dir}/.gitignore", 'w')
  f.puts <<-GITIGNORE.gsub(/^\s+/, '')
    # Directories.
    diffs
    snapshot

    # Files.
    settings.yaml
    paths.txt
    failures.txt
  GITIGNORE
  f.close
end

#rootsObject

Determine if we’re dealing with one or two URLs.



26
27
28
29
30
# File 'lib/sitediff/config/creator.rb', line 26

def roots
  @roots = { 'after' => @after }
  @roots['before'] = @before || @after
  @roots
end

#write_configObject

Writes the built config into the config file. TODO: Exclude default params before writing.



109
110
111
112
113
# File 'lib/sitediff/config/creator.rb', line 109

def write_config
  make_gitignore(@dir)
  data = Config.remove_defaults(@config)
  config_file.open('w') { |f| f.puts data.to_yaml }
end