Class: State
- Inherits:
-
Object
- Object
- State
- Defined in:
- lib/droxi/state.rb
Overview
Encapsulates the session state of the client.
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
The actual/original command-line arguments.
-
#cache ⇒ Object
readonly
Hashof remote file paths to cached file metadata. -
#debug_enabled ⇒ Object
trueif the –debug option was given,falseotherwise. -
#exit_requested ⇒ Object
trueif the client has requested to quit,falseotherwise. -
#interactive ⇒ Object
readonly
Whether the session is interactive.
-
#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, interactive = true, argv = ARGV) ⇒ 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, interactive = true, argv = ARGV) ⇒ State
Return a new application state that uses the given client. Starts at the Dropbox root and with an empty cache.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/droxi/state.rb', line 35 def initialize(client, interactive = true, argv = ARGV) @interactive = interactive @argv = argv @cache = Cache.new @client = client @exit_requested = false @debug_enabled = false @pwd = '/' @oldpwd = Settings[:oldpwd] || '/' @local_oldpwd = Dir.pwd end |
Instance Attribute Details
#argv ⇒ Object (readonly)
The actual/original command-line arguments
19 20 21 |
# File 'lib/droxi/state.rb', line 19 def argv @argv end |
#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 |
#debug_enabled ⇒ Object
true if the –debug option was given, false otherwise.
31 32 33 |
# File 'lib/droxi/state.rb', line 31 def debug_enabled @debug_enabled end |
#exit_requested ⇒ Object
true if the client has requested to quit, false otherwise.
28 29 30 |
# File 'lib/droxi/state.rb', line 28 def exit_requested @exit_requested end |
#interactive ⇒ Object (readonly)
Whether the session is interactive.
22 23 24 |
# File 'lib/droxi/state.rb', line 22 def interactive @interactive end |
#local_oldpwd ⇒ Object
The previous local working directory path.
25 26 27 |
# File 'lib/droxi/state.rb', line 25 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.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/droxi/state.rb', line 63 def contents(path) path = path.downcase path = resolve_path(path) (path) path = "#{path}/".sub('//', '/') keys = @cache.keys.select do |key| key.start_with?(path) && key != path && !key.sub(path, '').include?('/') end keys.map { |key| @cache[key]['path'] } end |
#directory?(path) ⇒ Boolean
Return true if the Dropbox path is a directory, false otherwise.
75 76 77 78 79 80 |
# File 'lib/droxi/state.rb', line 75 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.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/droxi/state.rb', line 104 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.
117 118 119 120 121 122 123 124 125 |
# File 'lib/droxi/state.rb', line 117 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.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/droxi/state.rb', line 49 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.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/droxi/state.rb', line 90 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!(%r{/\.$}, '') path.chomp!('/') path.gsub!('//', '/') path.empty? ? '/' : path end |