Class: Dutiful::ApplicationFile
- Inherits:
-
Object
- Object
- Dutiful::ApplicationFile
- Defined in:
- lib/dutiful/application_file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #backup ⇒ Object
- #backup_path ⇒ Object
- #backup_timestamp ⇒ Object
- #command ⇒ Object
- #directory? ⇒ Boolean
- #exist? ⇒ Boolean
- #expected_output ⇒ Object
- #expected_status ⇒ Object
- #full_path ⇒ Object
- #has_backup? ⇒ Boolean
- #has_condition? ⇒ Boolean
- #in_sync? ⇒ Boolean
-
#initialize(path, condition) ⇒ ApplicationFile
constructor
A new instance of ApplicationFile.
- #meets_conditions? ⇒ Boolean
- #name ⇒ Object
- #restore ⇒ Object
- #timestamp ⇒ Object
- #to_s ⇒ Object
- #tracked? ⇒ Boolean
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
#path ⇒ Object (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 ⇒ Object
8 9 10 |
# File 'lib/dutiful/application_file.rb', line 8 def backup Dutiful::Config.storage.backup self end |
#backup_path ⇒ Object
16 17 18 |
# File 'lib/dutiful/application_file.rb', line 16 def backup_path Dutiful::Config.storage.path path end |
#backup_timestamp ⇒ Object
20 21 22 |
# File 'lib/dutiful/application_file.rb', line 20 def File.mtime backup_path if has_backup? end |
#command ⇒ Object
24 25 26 |
# File 'lib/dutiful/application_file.rb', line 24 def command @condition[:command] if has_condition? end |
#directory? ⇒ Boolean
52 53 54 |
# File 'lib/dutiful/application_file.rb', line 52 def directory? path.chars.last == '/' end |
#exist? ⇒ Boolean
56 57 58 |
# File 'lib/dutiful/application_file.rb', line 56 def exist? File.exist?(full_path) && meets_conditions? end |
#expected_output ⇒ Object
28 29 30 |
# File 'lib/dutiful/application_file.rb', line 28 def expected_output @condition[:expected_output] if has_condition? end |
#expected_status ⇒ Object
32 33 34 |
# File 'lib/dutiful/application_file.rb', line 32 def expected_status @condition[:expected_status] if has_condition? end |
#full_path ⇒ Object
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. "~/#{path}" end end |
#has_backup? ⇒ 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
64 65 66 |
# File 'lib/dutiful/application_file.rb', line 64 def has_condition? @condition end |
#in_sync? ⇒ 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
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 |
#name ⇒ Object
44 45 46 |
# File 'lib/dutiful/application_file.rb', line 44 def name path end |
#restore ⇒ Object
12 13 14 |
# File 'lib/dutiful/application_file.rb', line 12 def restore Dutiful::Config.storage.restore self end |
#timestamp ⇒ Object
48 49 50 |
# File 'lib/dutiful/application_file.rb', line 48 def File.mtime full_path if exist? end |
#to_s ⇒ Object
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
82 83 84 |
# File 'lib/dutiful/application_file.rb', line 82 def tracked? exist? || has_backup? end |