Class: Planter::FileEntry
Overview
A single file entry in a FileList
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
File path.
-
#operation ⇒ Object
Operation to execute on the file.
-
#tags ⇒ Object
readonly
Tags.
-
#target ⇒ Object
readonly
Target path.
Instance Method Summary collapse
-
#ask_operation ⇒ Object
Prompt for file handling.
-
#initialize(file, target, operation) ⇒ FileEntry
constructor
Initialize a FileEntry object.
-
#inspect ⇒ String
Returns a string representation of the object.
-
#matches_pattern? ⇒ Boolean
Test if file matches any pattern in config.
-
#test_operator ⇒ Symbol
Determine operators based on configured filters, asking for input if necessary.
-
#to_s ⇒ String
Returns a string representation of the object contents.
Methods inherited from Hash
#deep_freeze, #deep_merge, #deep_thaw, #stringify, #stringify!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!
Constructor Details
#initialize(file, target, operation) ⇒ FileEntry
Initialize a FileEntry object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/planter/fileentry.rb', line 27 def initialize(file, target, operation) return unless File.exist?(file) @file = file @target = target @operation = operation @tags = Tag.get(file) super() end |
Instance Attribute Details
#file ⇒ Object (readonly)
File path
10 11 12 |
# File 'lib/planter/fileentry.rb', line 10 def file @file end |
#operation ⇒ Object
Operation to execute on the file
7 8 9 |
# File 'lib/planter/fileentry.rb', line 7 def operation @operation end |
#tags ⇒ Object (readonly)
Tags
16 17 18 |
# File 'lib/planter/fileentry.rb', line 16 def @tags end |
#target ⇒ Object (readonly)
Target path
13 14 15 |
# File 'lib/planter/fileentry.rb', line 13 def target @target end |
Instance Method Details
#ask_operation ⇒ Object
Prompt for file handling. If File exists, offer a merge/overwrite/ignore, otherwise simply ask whether or not to copy.
69 70 71 72 73 74 75 76 77 |
# File 'lib/planter/fileentry.rb', line 69 def ask_operation if File.exist?(@target) Prompt.file_what?(self) else res = Prompt.yn("Copy #{File.basename(@file)} to #{File.basename(@target)}", default_response: true) res ? :copy : :ignore end end |
#inspect ⇒ String
Returns a string representation of the object.
84 85 86 |
# File 'lib/planter/fileentry.rb', line 84 def inspect "<FileEntry: @file: #{@file}, @target: #{@target}, @operation: #{@operation}>" end |
#matches_pattern? ⇒ Boolean
Test if file matches any pattern in config
44 45 46 |
# File 'lib/planter/fileentry.rb', line 44 def matches_pattern? Planter.patterns.filter { |pattern, _| @file =~ pattern }.count.positive? end |
#test_operator ⇒ Symbol
Determine operators based on configured filters, asking for input if necessary
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/planter/fileentry.rb', line 54 def test_operator operator = Planter.overwrite ? :overwrite : :copy Planter.patterns.each do |pattern, op| next unless @file =~ pattern operator = op == :ask && !Planter.overwrite ? ask_operation : op break end operator end |