Class: Cheatr::Client::Sheet
- Inherits:
-
Object
- Object
- Cheatr::Client::Sheet
- Defined in:
- lib/cheatr/client/sheet.rb
Instance Attribute Summary collapse
-
#cache_file ⇒ Object
readonly
Returns the value of attribute cache_file.
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
-
#changed? ⇒ Boolean
Returns true if the contents have been modified and need to be saved, false otherwise.
-
#fetch(opts = {}) ⇒ Object
Fetches the contents, either from cache if available, or from the remote server.
-
#initialize(name, opts = {}) ⇒ Sheet
constructor
A new instance of Sheet.
-
#remote? ⇒ Boolean
Returns true if the contents were last fetched from the remote server, false if fetched from cache.
-
#save ⇒ Object
Saves the contents if changed.
Constructor Details
#initialize(name, opts = {}) ⇒ Sheet
Returns a new instance of Sheet.
13 14 15 16 17 18 19 |
# File 'lib/cheatr/client/sheet.rb', line 13 def initialize(name, opts = {}) raise "Sheet name '#{name}' is not valid" unless name =~ Cheatr::SHEET_NAME_REGEXP @name = name @cache_file = File.join(Cheatr::Client.cache_dir, "#{name}.md") @uri = "http://#{Cheatr::Client.server}/#{name}" fetch(opts) end |
Instance Attribute Details
#cache_file ⇒ Object (readonly)
Returns the value of attribute cache_file.
7 8 9 |
# File 'lib/cheatr/client/sheet.rb', line 7 def cache_file @cache_file end |
#contents ⇒ Object
Returns the value of attribute contents.
6 7 8 |
# File 'lib/cheatr/client/sheet.rb', line 6 def contents @contents end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/cheatr/client/sheet.rb', line 7 def errors @errors end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/cheatr/client/sheet.rb', line 6 def name @name end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/cheatr/client/sheet.rb', line 7 def uri @uri end |
Class Method Details
Instance Method Details
#changed? ⇒ Boolean
Returns true if the contents have been modified and need to be saved, false otherwise.
44 45 46 |
# File 'lib/cheatr/client/sheet.rb', line 44 def changed? @old_contents != @contents end |
#fetch(opts = {}) ⇒ Object
Fetches the contents, either from cache if available, or from the remote server.
If ignore_cache is true, the cache is ignored, and contents are updated from the remote server.
If contents are fetched from the remote server, the cache is updated. Returns true if successful, false otherwise.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cheatr/client/sheet.rb', line 75 def fetch(opts = {}) fetched = opts[:ignore_cache] ? nil : fetch_cache fetched ||= fetch_remote if fetched @contents = fetched @old_contents = @contents save_cache if remote? end !!fetched end |
#remote? ⇒ Boolean
Returns true if the contents were last fetched from the remote server, false if fetched from cache.
36 37 38 |
# File 'lib/cheatr/client/sheet.rb', line 36 def remote? @remote == true end |
#save ⇒ Object
Saves the contents if changed.
Cache is updated if saving to remote server was successful.
Returns true if successful, false otherwise.
55 56 57 58 59 60 61 62 63 |
# File 'lib/cheatr/client/sheet.rb', line 55 def save return false if contents.nil? if changed? && save_remote # Re-fetch because the server may modify contents on saving. # Cached version will be saved during re-fetching below. fetch(ignore_cache: true) end !changed? end |