Class: Copypasta::Plan
- Inherits:
-
Object
- Object
- Copypasta::Plan
- Defined in:
- lib/copypasta/plan.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(parameters, destination_directory, force: false) ⇒ Object
-
#initialize(settings:, contents: []) ⇒ Plan
constructor
A new instance of Plan.
- #interrogate(parameters) ⇒ Object
- #parameters_valid?(parameters) ⇒ Boolean
Constructor Details
#initialize(settings:, contents: []) ⇒ Plan
Returns a new instance of Plan.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/copypasta/plan.rb', line 10 def initialize(settings:, contents: []) raise "settings must be a Copypasta::Settings" \ unless settings.is_a?(Copypasta::Settings) raise "'contents' must be an Array of Copypasta::Contents." \ unless contents.is_a?(Array) && contents.all? { |c| c.is_a?(Copypasta::Contents) } @settings = settings.dup.freeze @contents = contents.dup.freeze end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
8 9 10 |
# File 'lib/copypasta/plan.rb', line 8 def contents @contents end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/copypasta/plan.rb', line 6 def root @root end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
7 8 9 |
# File 'lib/copypasta/plan.rb', line 7 def settings @settings end |
Class Method Details
.from_directory(root) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/copypasta/plan.rb', line 67 def self.from_directory(root) raise "#{root} doesn't exist." unless Dir.exist?(root) root = File.(root) settings = Copypasta::Settings.from_file("#{root}/_settings.rb") contents = Copypasta::Contents.from_tree(root) Copypasta::Plan.new(settings: settings, contents: contents) end |
Instance Method Details
#apply(parameters, destination_directory, force: false) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/copypasta/plan.rb', line 55 def apply(parameters, destination_directory, force: false) # TODO: validate the parameters, then call all of the entries with the # parameter set raise "parameter set is invalid, check the logs." \ unless parameters_valid?(parameters) raise "Directory is dirty. Use force to create anyway." \ if !force && !directory_clean?(destination_directory) contents.each { |c| c.apply(destination_directory, parameters) } end |
#interrogate(parameters) ⇒ Object
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 |
# File 'lib/copypasta/plan.rb', line 21 def interrogate(parameters) raise "#interrogate can only be called when STDOUT is a tty?." \ unless STDOUT.tty? # TODO: check to see if the parameter exists; if it doesn't, ask on the # tty for a value. # TODO: decide whether to notify-and-retry or fail on invalid parameter. require "highline" cli = HighLine.new missing_parameters = settings.parameter_definitions .values.select { |pd| parameters[pd.name].nil? } missing_parameters.each do |pd| puts pd.description answer = cli.ask("#{pd.name}: ") { |q| q.default = pd.default } answer = pd.postprocess.call(answer) unless pd.postprocess.nil? puts "Received parameter '#{pd.name}': '#{answer}'" parameters[pd.name] = answer end parameters end |
#parameters_valid?(parameters) ⇒ Boolean
49 50 51 52 53 |
# File 'lib/copypasta/plan.rb', line 49 def parameters_valid?(parameters) ret = @settings.validate(parameters) ret.all?(&:empty?) end |