Class: Dutiful::ApplicationFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, condition) ⇒ ApplicationFile

Returns a new instance of ApplicationFile.



4
5
6
# File 'lib/dutiful/application_file.rb', line 4

def initialize(path, condition)
  @condition, @path = condition, path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



2
3
4
# File 'lib/dutiful/application_file.rb', line 2

def path
  @path
end

Instance Method Details

#backupObject



8
9
10
# File 'lib/dutiful/application_file.rb', line 8

def backup
  Dutiful::Config.storage.backup self
end

#backup_pathObject



16
17
18
# File 'lib/dutiful/application_file.rb', line 16

def backup_path
  Dutiful::Config.storage.path path
end

#backup_timestampObject



20
21
22
# File 'lib/dutiful/application_file.rb', line 20

def backup_timestamp
  File.mtime backup_path if has_backup?
end

#commandObject



24
25
26
# File 'lib/dutiful/application_file.rb', line 24

def command
  @condition[:command] if has_condition?
end

#directory?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/dutiful/application_file.rb', line 52

def directory?
  path.chars.last == '/'
end

#exist?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/dutiful/application_file.rb', line 56

def exist?
  File.exist?(full_path) && meets_conditions?
end

#expected_outputObject



28
29
30
# File 'lib/dutiful/application_file.rb', line 28

def expected_output
  @condition[:expected_output] if has_condition?
end

#expected_statusObject



32
33
34
# File 'lib/dutiful/application_file.rb', line 32

def expected_status
  @condition[:expected_status] if has_condition?
end

#full_pathObject



36
37
38
39
40
41
42
# File 'lib/dutiful/application_file.rb', line 36

def full_path
  if directory?
    "#{File.expand_path "~/#{path}"}/"
  else
    File.expand_path "~/#{path}"
  end
end

#has_backup?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/dutiful/application_file.rb', line 60

def has_backup?
  Dutiful::Config.storage.exist?(self) && meets_conditions?
end

#has_condition?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/dutiful/application_file.rb', line 64

def has_condition?
  @condition
end

#in_sync?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dutiful/application_file.rb', line 86

def in_sync?
  has_backup? && Dutiful::Config.storage.in_sync?(self)
end

#meets_conditions?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dutiful/application_file.rb', line 68

def meets_conditions?
  if has_condition?
    output = `#{command}`.strip

    if expected_status
      $?.exitstatus == expected_status
    else
      $?.success? && output == expected_output
    end
  else
    true
  end
end

#nameObject



44
45
46
# File 'lib/dutiful/application_file.rb', line 44

def name
  path
end

#restoreObject



12
13
14
# File 'lib/dutiful/application_file.rb', line 12

def restore
  Dutiful::Config.storage.restore self
end

#timestampObject



48
49
50
# File 'lib/dutiful/application_file.rb', line 48

def timestamp
  File.mtime full_path if exist?
end

#to_sObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dutiful/application_file.rb', line 90

def to_s
  return "#{path} does not meet conditions (skipping)".yellow unless meets_conditions?

  if exist?
    if has_backup?
      if in_sync?
        "#{path} ✔".green
      else
        "#{path} (modified)".yellow
      end
    else
      "#{path} (pending backup)".yellow
    end
  elsif has_backup?
    "#{path} (pending restore)".yellow
  else
    "#{path} does not exist (skipping)".light_black
  end
end

#tracked?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/dutiful/application_file.rb', line 82

def tracked?
  exist? || has_backup?
end