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_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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/planter/fileentry.rb', line 21

def initialize(file, target, operation)
  return nil unless File.exist?(file)

  @file = file
  @target = target
  @operation = operation

  @tags = Tag.get(file)

  super()
end

Instance Attribute Details

#fileObject (readonly)

File path and target 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)

File path and target path



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

def tags
  @tags
end

#targetObject (readonly)

File path and target path



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

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.



63
64
65
66
67
68
69
70
71
# File 'lib/planter/fileentry.rb', line 63

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.



78
79
80
# File 'lib/planter/fileentry.rb', line 78

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



38
39
40
# File 'lib/planter/fileentry.rb', line 38

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:



48
49
50
51
52
53
54
55
56
57
# File 'lib/planter/fileentry.rb', line 48

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.



87
88
89
# File 'lib/planter/fileentry.rb', line 87

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