Class: Ftpmock::Cache
- Inherits:
-
Object
- Object
- Ftpmock::Cache
- Defined in:
- lib/ftpmock/core/cache.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
Instance Method Summary collapse
- #_alert(tag, content, color = :green) ⇒ Object
- #_chdir_alert(dirname, old_chdir, new_chdir) ⇒ Object
- #_get_alert_hit(tag, localfile) ⇒ Object
- #_get_alert_miss(tag) ⇒ Object
- #_get_alert_storing(tag, localfile) ⇒ Object
- #_get_raise_localfile_not_fetched(remotefile, localfile) ⇒ Object
- #_get_tag_for(remotefile) ⇒ Object
- #_list_alert_hit(tag, list) ⇒ Object
- #_list_alert_miss(tag) ⇒ Object
- #_list_alert_storing(tag, list) ⇒ Object
- #_list_tag_for(key) ⇒ Object
- #_put_alert_exists(tag) ⇒ Object
- #_put_alert_hit(tag, size) ⇒ Object
- #_put_alert_miss(tag) ⇒ Object
- #_put_alert_storing(tag, remotefile) ⇒ Object
- #_put_cached(tag, path, localfile, remotefile) ⇒ Object
- #_put_raise_localfile_differs(remotefile, localfile, diff) ⇒ Object
- #_put_raise_not_found(remotefile, localfile) ⇒ Object
- #_put_tag_for(localfile) ⇒ Object
- #chdir(dirname = nil) ⇒ Object
- #get(remotefile, localfile = File.basename(remotefile)) ⇒ Object
-
#initialize(configuration, credentials) ⇒ Cache
constructor
A new instance of Cache.
- #list(*args) ⇒ Object
- #path ⇒ Object
- #path_dir ⇒ Object
- #put(localfile, remotefile = File.basename(localfile)) ⇒ Object
Constructor Details
#initialize(configuration, credentials) ⇒ Cache
Returns a new instance of Cache.
5 6 7 8 9 |
# File 'lib/ftpmock/core/cache.rb', line 5 def initialize(configuration, credentials) @configuration = configuration @credentials = credentials @chdir = nil end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
3 4 5 |
# File 'lib/ftpmock/core/cache.rb', line 3 def configuration @configuration end |
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
3 4 5 |
# File 'lib/ftpmock/core/cache.rb', line 3 def credentials @credentials end |
Instance Method Details
#_alert(tag, content, color = :green) ⇒ Object
19 20 21 |
# File 'lib/ftpmock/core/cache.rb', line 19 def _alert(tag, content, color = :green) configuration.verbose && VerboseUtils.alert(tag, content, color) end |
#_chdir_alert(dirname, old_chdir, new_chdir) ⇒ Object
33 34 35 36 37 |
# File 'lib/ftpmock/core/cache.rb', line 33 def _chdir_alert(dirname, old_chdir, new_chdir) tag = "ftpmock.cache.chdir '#{dirname}'" _alert tag, "changed from '#{old_chdir}' to '#{new_chdir}'", :green end |
#_get_alert_hit(tag, localfile) ⇒ Object
99 100 101 |
# File 'lib/ftpmock/core/cache.rb', line 99 def _get_alert_hit(tag, localfile) _alert tag, "hit! (#{localfile})" end |
#_get_alert_miss(tag) ⇒ Object
103 104 105 |
# File 'lib/ftpmock/core/cache.rb', line 103 def _get_alert_miss(tag) _alert tag, 'miss!', :yellow end |
#_get_alert_storing(tag, localfile) ⇒ Object
107 108 109 |
# File 'lib/ftpmock/core/cache.rb', line 107 def _get_alert_storing(tag, localfile) _alert tag, "storing #{localfile}" end |
#_get_raise_localfile_not_fetched(remotefile, localfile) ⇒ Object
111 112 113 114 115 |
# File 'lib/ftpmock/core/cache.rb', line 111 def _get_raise_localfile_not_fetched(remotefile, localfile) msg = "FTP GET '#{remotefile}' should have created '#{localfile}'" msg = "#{msg}, but didn't." raise GetNotFetched, msg end |
#_get_tag_for(remotefile) ⇒ Object
93 94 95 96 97 |
# File 'lib/ftpmock/core/cache.rb', line 93 def _get_tag_for(remotefile) tag = "ftpmock.cache.get '#{remotefile}'" tag += ", chdir: '#{chdir}'" if chdir tag end |
#_list_alert_hit(tag, list) ⇒ Object
62 63 64 |
# File 'lib/ftpmock/core/cache.rb', line 62 def _list_alert_hit(tag, list) list.nil? || _alert(tag, "hit! (#{list.size} lines)") end |
#_list_alert_miss(tag) ⇒ Object
66 67 68 |
# File 'lib/ftpmock/core/cache.rb', line 66 def _list_alert_miss(tag) _alert tag, 'miss!', :yellow end |
#_list_alert_storing(tag, list) ⇒ Object
70 71 72 |
# File 'lib/ftpmock/core/cache.rb', line 70 def _list_alert_storing(tag, list) _alert tag, "storing #{list.size} lines!" end |
#_list_tag_for(key) ⇒ Object
56 57 58 59 60 |
# File 'lib/ftpmock/core/cache.rb', line 56 def _list_tag_for(key) tag = "ftpmock.cache.list '#{key}'" tag += ", chdir: '#{chdir}'" if chdir tag end |
#_put_alert_exists(tag) ⇒ Object
142 143 144 |
# File 'lib/ftpmock/core/cache.rb', line 142 def _put_alert_exists(tag) _alert tag, 'file exists?' end |
#_put_alert_hit(tag, size) ⇒ Object
162 163 164 165 |
# File 'lib/ftpmock/core/cache.rb', line 162 def _put_alert_hit(tag, size) color = size.zero? ? :green : :red _alert tag, "hit! (#{size} differing lines)", color end |
#_put_alert_miss(tag) ⇒ Object
146 147 148 |
# File 'lib/ftpmock/core/cache.rb', line 146 def _put_alert_miss(tag) _alert tag, 'miss!', :yellow end |
#_put_alert_storing(tag, remotefile) ⇒ Object
150 151 152 |
# File 'lib/ftpmock/core/cache.rb', line 150 def _put_alert_storing(tag, remotefile) _alert tag, "storing #{remotefile}" end |
#_put_cached(tag, path, localfile, remotefile) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/ftpmock/core/cache.rb', line 154 def _put_cached(tag, path, localfile, remotefile) diff = PutHelper.compare(path, localfile, remotefile) _put_alert_hit(tag, diff.size) diff.any? && _put_raise_localfile_differs(remotefile, localfile, diff) end |
#_put_raise_localfile_differs(remotefile, localfile, diff) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/ftpmock/core/cache.rb', line 173 def _put_raise_localfile_differs(remotefile, localfile, diff) spaces = ' ' * 20 VerboseUtils.puts VerboseUtils.puts ColorUtils.highlight "#{spaces}Diffy Begin#{spaces}" VerboseUtils.puts diff VerboseUtils.puts ColorUtils.highlight "#{spaces} Diffy End #{spaces}" VerboseUtils.puts msg = "FTP PUT '#{remotefile}' has failed because" msg = "#{msg} '#{localfile}' contents don't match #{path}/#{remotefile}" raise PutLocalDiffersFromCache, msg end |
#_put_raise_not_found(remotefile, localfile) ⇒ Object
167 168 169 170 171 |
# File 'lib/ftpmock/core/cache.rb', line 167 def _put_raise_not_found(remotefile, localfile) msg = "FTP PUT '#{remotefile}' has failed" msg = "#{msg} because '#{localfile}' does not exist." raise PutFileNotFound, msg end |
#_put_tag_for(localfile) ⇒ Object
136 137 138 139 140 |
# File 'lib/ftpmock/core/cache.rb', line 136 def _put_tag_for(localfile) tag = "ftpmock.cache.put '#{localfile}'" tag += ", chdir: '#{chdir}'" if chdir tag end |
#chdir(dirname = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/ftpmock/core/cache.rb', line 23 def chdir(dirname = nil) return @chdir if dirname.nil? original = @chdir @chdir = PathHelper.join(@chdir, dirname) _chdir_alert(dirname, original, @chdir) @chdir end |
#get(remotefile, localfile = File.basename(remotefile)) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ftpmock/core/cache.rb', line 74 def get(remotefile, localfile = File.basename(remotefile)) tag = _get_tag_for(remotefile) if GetHelper.read(path, chdir, remotefile, localfile) _get_alert_hit(tag, localfile) return end _get_alert_miss(tag) yield if GetHelper.fetched?(localfile) _get_alert_storing(tag, localfile) GetHelper.write(path, chdir, remotefile, localfile) else _get_raise_localfile_not_fetched(remotefile, localfile) end end |
#list(*args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ftpmock/core/cache.rb', line 39 def list(*args) key = args.join(',') tag = _list_tag_for(key) list = ListHelper.read(path, chdir, key) _list_alert_hit(tag, list) return list if list _list_alert_miss(tag) list = yield _list_alert_storing(tag, list) ListHelper.write(path, chdir, key, list) list end |
#path ⇒ Object
11 12 13 |
# File 'lib/ftpmock/core/cache.rb', line 11 def path @path ||= Pathname("#{configuration.path}/#{path_dir}") end |
#path_dir ⇒ Object
15 16 17 |
# File 'lib/ftpmock/core/cache.rb', line 15 def path_dir StringUtils.parameterize(credentials.map(&:to_s).join('_')) end |
#put(localfile, remotefile = File.basename(localfile)) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ftpmock/core/cache.rb', line 117 def put(localfile, remotefile = File.basename(localfile)) tag = _put_tag_for(localfile) _put_alert_exists(tag) PutHelper.exist?(localfile) || _put_raise_not_found(remotefile, localfile) if PutHelper.cached?(path, remotefile) _put_cached(tag, path, localfile, remotefile) else _put_alert_miss(tag) yield _put_alert_storing(tag, remotefile) PutHelper.write(path, localfile, remotefile) end true end |