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
Hashof remote file paths to cached file metadata. -
#exit_requested ⇒ Object
trueif the client has requested to quit,falseotherwise. -
#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
Arrayof paths of files in a Dropbox directory. -
#directory?(path) ⇒ Boolean
Return
trueif the Dropbox path is a directory,falseotherwise. -
#expand_patterns(patterns, preserve_root = false) ⇒ Object
Expand an
Arrayof file globs into an anArrayof 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
Hashof the Dropbox metadata for a file, ornilif 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.
50 51 52 53 54 55 56 57 |
# File 'lib/droxi/state.rb', line 50 def contents(path) path = resolve_path(path) (path) path = "#{path}/".sub('//', '/') @cache.keys.select do |key| key.start_with?(path) && key != path && !key.sub(path, '').include?('/') end end |
#directory?(path) ⇒ Boolean
Return true if the Dropbox path is a directory, false otherwise.
60 61 62 63 64 |
# File 'lib/droxi/state.rb', line 60 def directory?(path) 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.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/droxi/state.rb', line 88 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.
101 102 103 104 105 106 107 108 109 |
# File 'lib/droxi/state.rb', line 101 def forget_contents(partial_path) path = resolve_path(partial_path) 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 |
# File 'lib/droxi/state.rb', line 37 def (path, require_contents = true) 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.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/droxi/state.rb', line 74 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 |