Class: Planter::FileEntry

Inherits:
Hash
  • Object
show all
Defined in:
lib/planter/fileentry.rb

Overview

A single file entry in a FileList

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • file (String)

    The source file path

  • target (String)

    The target path

  • operation (Symbol)

    The operation to perform



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

#fileObject (readonly)

File path



10
11
12
# File 'lib/planter/fileentry.rb', line 10

def file
  @file
end

#operationObject

Operation to execute on the file



7
8
9
# File 'lib/planter/fileentry.rb', line 7

def operation
  @operation
end

#tagsObject (readonly)

Tags



16
17
18
# File 'lib/planter/fileentry.rb', line 16

def tags
  @tags
end

#targetObject (readonly)

Target path



13
14
15
# File 'lib/planter/fileentry.rb', line 13

def target
  @target
end

Instance Method Details

#ask_operationObject

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

#inspectString

Returns a string representation of the object.

Returns:

  • (String)

    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

Returns:

  • (Boolean)

    file matches pattern



44
45
46
# File 'lib/planter/fileentry.rb', line 44

def matches_pattern?
  Planter.patterns.filter { |pattern, _| @file =~ pattern }.count.positive?
end

#test_operatorSymbol

Determine operators based on configured filters, asking for input if necessary

Returns:



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

#to_sString

Returns a string representation of the object contents.

Returns:

  • (String)

    String representation of the object.



93
94
95
# File 'lib/planter/fileentry.rb', line 93

def to_s
  File.binary?(@file) ? 'Binary file' : IO.read(@file).to_s
end