Class: DropboxSync::Entry
- Inherits:
-
Object
- Object
- DropboxSync::Entry
- Extended by:
- Enumerable
- Defined in:
- lib/dropbox_sync/entry.rb
Class Attribute Summary collapse
-
.sync_folder ⇒ Object
Returns the value of attribute sync_folder.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .[](name_or_path) ⇒ Object
- .add_entry(entry) ⇒ Object
- .create(name, path) ⇒ Object
- .each(&block) ⇒ Object
- .entries ⇒ Object
- .from_hash(name, options) ⇒ Object
- .normalized_path(path) ⇒ Object
- .remove_entry(entry) ⇒ Object
- .save_entries! ⇒ Object
- .settings_file ⇒ Object
- .sync_foler=(folder) ⇒ Object
Instance Method Summary collapse
- #applied? ⇒ Boolean
- #apply! ⇒ Object
- #applyable? ⇒ Boolean
- #backup_path ⇒ Object
- #backup_target! ⇒ Object
- #create! ⇒ Object
- #destroy! ⇒ Object
-
#initialize(name, path) ⇒ Entry
constructor
A new instance of Entry.
- #status ⇒ Object
- #sync_path ⇒ Object
- #target_exist? ⇒ Boolean
- #target_path ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #unapply! ⇒ Object
Constructor Details
#initialize(name, path) ⇒ Entry
Returns a new instance of Entry.
67 68 69 |
# File 'lib/dropbox_sync/entry.rb', line 67 def initialize(name, path) @name, @path = name, self.class.normalized_path(path) end |
Class Attribute Details
.sync_folder ⇒ Object
Returns the value of attribute sync_folder.
7 8 9 |
# File 'lib/dropbox_sync/entry.rb', line 7 def sync_folder @sync_folder end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
65 66 67 |
# File 'lib/dropbox_sync/entry.rb', line 65 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
65 66 67 |
# File 'lib/dropbox_sync/entry.rb', line 65 def path @path end |
Class Method Details
.[](name_or_path) ⇒ Object
39 40 41 42 |
# File 'lib/dropbox_sync/entry.rb', line 39 def [](name_or_path) return name_or_path if name_or_path.kind_of?(DropboxSync::Entry) entries.detect { |entry| entry.name == name_or_path || File.(name_or_path) == entry.target_path } end |
.add_entry(entry) ⇒ Object
22 23 24 25 |
# File 'lib/dropbox_sync/entry.rb', line 22 def add_entry(entry) entries << entry save_entries! end |
.create(name, path) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/dropbox_sync/entry.rb', line 54 def create(name, path) raise "#{path.inspect} not found!" unless File.exist?(path) raise "#{path.inspect} is already being synced!" if entries.any? { |e| e.target_path == File.(path) } raise "Entry #{name.inspect} already exists, choose another name!" if entries.any? { |e| e.name == name } entry = self.new(name, path) entry.create! end |
.each(&block) ⇒ Object
18 19 20 |
# File 'lib/dropbox_sync/entry.rb', line 18 def each(&block) entries.each(&block) end |
.entries ⇒ Object
32 33 34 35 36 37 |
# File 'lib/dropbox_sync/entry.rb', line 32 def entries @entries ||= begin hash = YAML.load(File.read(settings_file)) || {} hash.map { |name, | DropboxSync::Entry.from_hash(name, ) } end end |
.from_hash(name, options) ⇒ Object
50 51 52 |
# File 'lib/dropbox_sync/entry.rb', line 50 def from_hash(name, ) self.new(name, ['path']) end |
.normalized_path(path) ⇒ Object
71 72 73 |
# File 'lib/dropbox_sync/entry.rb', line 71 def self.normalized_path(path) File.(path).sub(Regexp.new('^' + Regexp.quote(File.('~'))), '~') end |
.remove_entry(entry) ⇒ Object
27 28 29 30 |
# File 'lib/dropbox_sync/entry.rb', line 27 def remove_entry(entry) entries.delete(self[entry]) save_entries! end |
.save_entries! ⇒ Object
44 45 46 47 48 |
# File 'lib/dropbox_sync/entry.rb', line 44 def save_entries! hash = {} each { |entry| hash[entry.name] = entry.to_hash } File.open(settings_file, 'w') { |io| io.write(YAML.dump(hash)) } end |
.settings_file ⇒ Object
14 15 16 |
# File 'lib/dropbox_sync/entry.rb', line 14 def settings_file File.join(sync_folder, 'sync-settings.yml') end |
.sync_foler=(folder) ⇒ Object
9 10 11 12 |
# File 'lib/dropbox_sync/entry.rb', line 9 def sync_foler=(folder) @sync_folder = folder @entries = nil end |
Instance Method Details
#applied? ⇒ Boolean
88 89 90 |
# File 'lib/dropbox_sync/entry.rb', line 88 def applied? File.symlink?(sync_path) && File.readlink(sync_path) == target_path && File.exist?(target_path) end |
#apply! ⇒ Object
112 113 114 115 116 |
# File 'lib/dropbox_sync/entry.rb', line 112 def apply! raise "Traget already exists" if target_exist? FileUtils.mv(sync_path, target_path) FileUtils.ln_s(target_path, sync_path) end |
#applyable? ⇒ Boolean
92 93 94 |
# File 'lib/dropbox_sync/entry.rb', line 92 def applyable? File.exist?(sync_path) && !File.symlink?(sync_path) && File.exist?(target_path) end |
#backup_path ⇒ Object
100 101 102 |
# File 'lib/dropbox_sync/entry.rb', line 100 def backup_path target_path + '.local_backup' end |
#backup_target! ⇒ Object
108 109 110 |
# File 'lib/dropbox_sync/entry.rb', line 108 def backup_target! FileUtils.mv(target_path, backup_path) end |
#create! ⇒ Object
83 84 85 86 |
# File 'lib/dropbox_sync/entry.rb', line 83 def create! FileUtils.ln_s(target_path, sync_path) self.class.add_entry(self) end |
#destroy! ⇒ Object
125 126 127 128 |
# File 'lib/dropbox_sync/entry.rb', line 125 def destroy! FileUtils.rm(sync_path) if File.exist?(sync_path) self.class.remove_entry(self) end |
#status ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/dropbox_sync/entry.rb', line 130 def status if applied? :applied elsif applyable? :applyable else :invalid end end |
#sync_path ⇒ Object
79 80 81 |
# File 'lib/dropbox_sync/entry.rb', line 79 def sync_path File.join(self.class.sync_folder, name) end |
#target_exist? ⇒ Boolean
104 105 106 |
# File 'lib/dropbox_sync/entry.rb', line 104 def target_exist? File.exist?(target_path) end |
#target_path ⇒ Object
96 97 98 |
# File 'lib/dropbox_sync/entry.rb', line 96 def target_path File.(path) end |
#to_hash ⇒ Object
75 76 77 |
# File 'lib/dropbox_sync/entry.rb', line 75 def to_hash { 'path' => @path } end |
#to_s ⇒ Object
140 141 142 |
# File 'lib/dropbox_sync/entry.rb', line 140 def to_s "#{name} -> #{path} [#{status}]" end |
#unapply! ⇒ Object
118 119 120 121 122 123 |
# File 'lib/dropbox_sync/entry.rb', line 118 def unapply! raise "Cannot unapply entries that have not been applied!" unless applied? FileUtils.rm(sync_path) FileUtils.cp_r(target_path, sync_path) self.class.remove_entry(self) end |