Class: Materielize::ConfigSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/materielize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigSetup

Returns a new instance of ConfigSetup.



8
9
10
11
12
# File 'lib/materielize.rb', line 8

def initialize
  @root_dir = "materiel"
  @default_config_dir = "default_config_files"
  @overwrite_all = false
end

Instance Attribute Details

#default_config_dirObject (readonly)

Returns the value of attribute default_config_dir.



6
7
8
# File 'lib/materielize.rb', line 6

def default_config_dir
  @default_config_dir
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



6
7
8
# File 'lib/materielize.rb', line 6

def root_dir
  @root_dir
end

Instance Method Details

#accepted_user_responsesObject

Valid single-character responses from the user



63
64
65
# File 'lib/materielize.rb', line 63

def accepted_user_responses
  %w[a A n N c C] + [true, false]
end

#default_config_dir_exists?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/materielize.rb', line 18

def default_config_dir_exists?
  Dir.exists?(sub_path(default_config_dir))
end

#init_cfg_files(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/materielize.rb', line 48

def init_cfg_files(options = {})
  @overwrite_all = {force_all: false}.merge(options)[:force_all] # initialize if not indicated so as to not inadvertently cause disaster on a second call or somesuch.
  @project_root = Dir.getwd
  @root = File.expand_path(default_config_dir, root_dir)

  begin
    copy([root_dir, default_config_dir]) do |item|
      yield(item)
    end
  rescue Interrupt => e
    yield({message: e.message, needs_confirmation: false})
  end
end

#installObject

Basic setup of materiel. Create materiel directory, default config subdirectory, throw around some READMEs, etc.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/materielize.rb', line 23

def install
  root_path = File.expand_path(root_dir)
  if !materiel_exists?
    yield({message: "Creating directory '#{root_path}'."}) if block_given?
    Dir.mkdir(root_path)
  else
    yield({message: "Directory '#{root_path}' already exists, no need to create."}) if block_given?
  end

  default_config_path = File.expand_path(default_config_dir, root_dir)

  if !default_config_dir_exists?
    yield({message: "Creating directory '#{default_config_path}'."}) if block_given?
    Dir.mkdir(default_config_path)
  else
    yield({message: "Directory '#{default_config_path}' already exists, no need to create."}) if block_given?
  end
end

#materiel_exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/materielize.rb', line 14

def materiel_exists?
  Dir.exists?(root_dir)
end

#uninstall {|{message: "Uninstalling: Removing #{root_path}"}| ... } ⇒ Object

Yields:

  • ({message: "Uninstalling: Removing #{root_path}"})


42
43
44
45
46
# File 'lib/materielize.rb', line 42

def uninstall
  root_path = File.expand_path(root_dir)
  yield({message: "Uninstalling: Removing #{root_path}"}) if block_given?
  FileUtils.rm_rf(root_path)
end

#valid_user_response?(response) ⇒ Boolean

Confirm that the user response is valid. Accepts either a string (char) response or the the whole response/messaging hash. Booleans also accepted.

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/materielize.rb', line 69

def valid_user_response?(response)
  if response.is_a?(String) || response.is_a?(TrueClass)|| response.is_a?(FalseClass)
    accepted_user_responses.include?(response)
  elsif response.is_a?(Hash)
    accepted_user_responses.include?(response[:confirmation])
  end
end