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
7
8
9
10
11
12
13
14
15
# File 'lib/dutiful/application_file.rb', line 4

def initialize(path, condition)
  @command         = condition[:command] if condition
  @expected_output = condition[:expected_output] if condition
  @expected_status = condition[:expected_status] if condition
  @path            = path

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

Instance Attribute Details

#full_pathObject (readonly)

Returns the value of attribute full_path.



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

def full_path
  @full_path
end

#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

#backup_pathObject



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

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

#backup_timestampObject



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

def backup_timestamp
  File.mtime backup_path if has_backup?
end

#directory?Boolean

Returns:

  • (Boolean)


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

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

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  File.exist? full_path
end

#has_backup?Boolean

Returns:

  • (Boolean)


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

def has_backup?
  Dutiful::Config.storage.exist? self
end

#has_condition?Boolean

Returns:

  • (Boolean)


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

def has_condition?
  @command
end

#meets_conditions?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dutiful/application_file.rb', line 29

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

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

#should_sync?Boolean

Returns:

  • (Boolean)


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

def should_sync?
  (exist? || has_backup?) && meets_conditions?
end

#synced?Boolean

Returns:

  • (Boolean)


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

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

#timestampObject



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

def timestamp
  File.mtime full_path if exist?
end

#to_sObject



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

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

  if exist?
    if has_backup?
      if synced?
        "#{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)".yellow
  end
end