Class: State
- Inherits:
-
Object
- Object
- State
- Defined in:
- lib/droxi/state.rb
Overview
Encapsulates the session state of the client.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Hash
of remote file paths to cached file metadata. -
#exit_requested ⇒ Object
true
if the client has requested to quit,false
otherwise. -
#local_oldpwd ⇒ Object
The previous local working directory path.
-
#oldpwd ⇒ Object
readonly
The previous remote working directory path.
-
#pwd ⇒ Object
The remote working directory path.
Instance Method Summary collapse
-
#contents(path) ⇒ Object
Return an
Array
of paths of files in a Dropbox directory. -
#directory?(path) ⇒ Boolean
Return
true
if the Dropbox path is a directory,false
otherwise. -
#expand_patterns(patterns, preserve_root = false) ⇒ Object
Expand an
Array
of file globs into an anArray
of Dropbox file paths and return the result. -
#forget_contents(partial_path) ⇒ Object
Recursively remove directory contents from metadata cache.
-
#initialize(client) ⇒ State
constructor
Return a new application state that uses the given client.
-
#metadata(path, require_contents = true) ⇒ Object
Return a
Hash
of the Dropbox metadata for a file, ornil
if the file does not exist. -
#resolve_path(arg) ⇒ Object
Expand a Dropbox file path and return the result.
Constructor Details
#initialize(client) ⇒ State
Return a new application state that uses the given client. Starts at the Dropbox root and with an empty cache.
26 27 28 29 30 31 32 33 |
# File 'lib/droxi/state.rb', line 26 def initialize(client) @cache = Cache.new @client = client @exit_requested = false @pwd = '/' @oldpwd = Settings[:oldpwd] || '/' @local_oldpwd = Dir.pwd end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Hash
of remote file paths to cached file metadata.
10 11 12 |
# File 'lib/droxi/state.rb', line 10 def cache @cache end |
#exit_requested ⇒ Object
true
if the client has requested to quit, false
otherwise.
22 23 24 |
# File 'lib/droxi/state.rb', line 22 def exit_requested @exit_requested end |
#local_oldpwd ⇒ Object
The previous local working directory path.
19 20 21 |
# File 'lib/droxi/state.rb', line 19 def local_oldpwd @local_oldpwd end |
#oldpwd ⇒ Object (readonly)
The previous remote working directory path.
16 17 18 |
# File 'lib/droxi/state.rb', line 16 def oldpwd @oldpwd end |
#pwd ⇒ Object
The remote working directory path.
13 14 15 |
# File 'lib/droxi/state.rb', line 13 def pwd @pwd end |
Instance Method Details
#contents(path) ⇒ Object
Return an Array
of paths of files in a Dropbox directory.
51 52 53 54 55 56 57 58 59 |
# File 'lib/droxi/state.rb', line 51 def contents(path) path = path.downcase path = resolve_path(path) (path) path = "#{path}/".sub('//', '/') @cache.keys.select do |key| key.start_with?(path) && key != path && !key.sub(path, '').include?('/') end.map { |key| @cache[key]['path'] } end |
#directory?(path) ⇒ Boolean
Return true
if the Dropbox path is a directory, false
otherwise.
62 63 64 65 66 67 |
# File 'lib/droxi/state.rb', line 62 def directory?(path) path = path.downcase path = resolve_path(path) (File.dirname(path)) @cache.include?(path) && @cache[path]['is_dir'] end |
#expand_patterns(patterns, preserve_root = false) ⇒ Object
Expand an Array
of file globs into an an Array
of Dropbox file paths and return the result.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/droxi/state.rb', line 91 def (patterns, preserve_root = false) patterns.flat_map do |pattern| path = resolve_path(pattern) if directory?(path) preserve_root ? pattern : path else get_matches(pattern, path, preserve_root) end end end |
#forget_contents(partial_path) ⇒ Object
Recursively remove directory contents from metadata cache. Yield lines of (error) output if a block is given.
104 105 106 107 108 109 110 111 112 |
# File 'lib/droxi/state.rb', line 104 def forget_contents(partial_path) path = resolve_path(partial_path).downcase if @cache.fetch(path, {}).include?('contents') @cache[path]['contents'].dup.each { |m| @cache.remove(m['path']) } @cache[path].delete('contents') elsif block_given? yield "forget: #{partial_path}: nothing to forget" end end |
#metadata(path, require_contents = true) ⇒ Object
Return a Hash
of the Dropbox metadata for a file, or nil
if the file does not exist.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/droxi/state.rb', line 37 def (path, require_contents = true) path = path.downcase tokens = path.split('/').drop(1) (0..tokens.size).each do |i| partial_path = '/' + tokens.take(i).join('/') next if @cache.full_info?(partial_path, require_contents) return nil unless (partial_path) end @cache[path] end |
#resolve_path(arg) ⇒ Object
Expand a Dropbox file path and return the result.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/droxi/state.rb', line 77 def resolve_path(arg) # REVIEW: See if we can do this in fewer lines (e.g. without two gsub!s). path = arg.start_with?('/') ? arg.dup : "#{@pwd}/#{arg}" path.gsub!('//', '/') nil while path.sub!(%r{/([^/]+?)/\.\.}, '') nil while path.sub!('./', '') path.sub!(/\/\.$/, '') path.chomp!('/') path.gsub!('//', '/') path.empty? ? '/' : path end |