Class: Cheatr::Client::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/cheatr/client/sheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_fileObject (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

#contentsObject

Returns the value of attribute contents.



6
7
8
# File 'lib/cheatr/client/sheet.rb', line 6

def contents
  @contents
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/cheatr/client/sheet.rb', line 7

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cheatr/client/sheet.rb', line 6

def name
  @name
end

#uriObject (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

.all(query = nil) ⇒ Object



9
10
11
# File 'lib/cheatr/client/sheet.rb', line 9

def self.all(query = nil)
  RestClient.get "http://#{Cheatr::Client.server}/#{query}"
end

Instance Method Details

#changed?Boolean

Returns true if the contents have been modified and need to be saved, false otherwise.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


36
37
38
# File 'lib/cheatr/client/sheet.rb', line 36

def remote?
  @remote == true
end

#saveObject

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