Module: Dbox::Utils
- Included in:
- Database, Syncer::Operation
- Defined in:
- lib/dbox/utils.rb
Instance Method Summary collapse
- #calculate_hash(filepath) ⇒ Object
- #case_insensitive_difference(a, b) ⇒ Object
- #case_insensitive_equal(a, b) ⇒ Object
- #case_insensitive_join(path, *rest) ⇒ Object
- #case_insensitive_resolve(path) ⇒ Object
- #find_nonconflicting_path(filepath) ⇒ Object
-
#local_to_relative_path(path) ⇒ Object
assumes local_path is defined.
- #parse_time(t) ⇒ Object
-
#relative_to_local_path(path) ⇒ Object
assumes local_path is defined.
-
#relative_to_remote_path(path) ⇒ Object
assumes remote_path is defined.
-
#remote_to_relative_path(path) ⇒ Object
assumes remote_path is defined.
- #time_to_s(t) ⇒ Object
- #times_equal?(t1, t2) ⇒ Boolean
Instance Method Details
#calculate_hash(filepath) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/dbox/utils.rb', line 93 def calculate_hash(filepath) begin Digest::MD5.file(filepath).to_s rescue Errno::EISDIR nil rescue Errno::ENOENT nil end end |
#case_insensitive_difference(a, b) ⇒ Object
84 85 86 87 |
# File 'lib/dbox/utils.rb', line 84 def case_insensitive_difference(a, b) b = b.map(&:downcase).sort a.reject {|s| b.include?(s.downcase) } end |
#case_insensitive_equal(a, b) ⇒ Object
89 90 91 |
# File 'lib/dbox/utils.rb', line 89 def case_insensitive_equal(a, b) a && b && a.downcase == b.downcase end |
#case_insensitive_join(path, *rest) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/dbox/utils.rb', line 75 def case_insensitive_join(path, *rest) if rest.length == 0 case_insensitive_resolve(path) else rest = rest.map {|s| s.split(File::SEPARATOR) }.flatten case_insensitive_join(File.join(case_insensitive_resolve(path), rest[0]), *rest[1..-1]) end end |
#case_insensitive_resolve(path) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dbox/utils.rb', line 62 def case_insensitive_resolve(path) if File.exists?(path) path else matches = Dir.glob(path, File::FNM_CASEFOLD) case matches.size when 0 then path when 1 then matches.first else raise(RuntimeError, "Oops, you have multiple files with the same case. Please delete one of them, as Dropbox is case insensitive. (#{matches.join(', ')})") end end end |
#find_nonconflicting_path(filepath) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/dbox/utils.rb', line 103 def find_nonconflicting_path(filepath) proposed = filepath while File.exists?(case_insensitive_resolve(proposed)) dir, p = File.split(proposed) p = p.sub(/^(.*?)( \((\d+)\))?(\..*?)?$/) { "#{$1} (#{$3 ? $3.to_i + 1 : 1})#{$4}" } proposed = File.join(dir, p) end proposed end |
#local_to_relative_path(path) ⇒ Object
assumes local_path is defined
27 28 29 30 31 32 33 |
# File 'lib/dbox/utils.rb', line 27 def local_to_relative_path(path) if path =~ /^#{local_path}\/?(.*)$/i $1 else raise BadPath, "Not a local path: #{path}" end end |
#parse_time(t) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/dbox/utils.rb', line 17 def parse_time(t) case t when Time t when String Time.parse(t) end end |
#relative_to_local_path(path) ⇒ Object
assumes local_path is defined
45 46 47 48 49 50 51 |
# File 'lib/dbox/utils.rb', line 45 def relative_to_local_path(path) if path && path.length > 0 case_insensitive_join(local_path, path) else case_insensitive_resolve(local_path) end end |
#relative_to_remote_path(path) ⇒ Object
assumes remote_path is defined
54 55 56 57 58 59 60 |
# File 'lib/dbox/utils.rb', line 54 def relative_to_remote_path(path) if path && path.length > 0 File.join(remote_path, path) else remote_path end end |
#remote_to_relative_path(path) ⇒ Object
assumes remote_path is defined
36 37 38 39 40 41 42 |
# File 'lib/dbox/utils.rb', line 36 def remote_to_relative_path(path) if path =~ /^#{remote_path}\/?(.*)$/i $1 else raise BadPath, "Not a remote path: #{path}" end end |
#time_to_s(t) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/dbox/utils.rb', line 7 def time_to_s(t) case t when Time # matches dropbox time format t.utc.strftime("%a, %d %b %Y %H:%M:%S +0000") when String t end end |
#times_equal?(t1, t2) ⇒ Boolean
3 4 5 |
# File 'lib/dbox/utils.rb', line 3 def times_equal?(t1, t2) time_to_s(t1) == time_to_s(t2) end |