Class: Cheatly::Sheet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, body = nil, options = {}) ⇒ Sheet

Returns a new instance of Sheet.



5
6
7
8
# File 'lib/cheatly/sheet.rb', line 5

def initialize(title, body = nil, options = {})
  @title, @body = title, body
  @persisted = options[:persisted] || false
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/cheatly/sheet.rb', line 3

def body
  @body
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/cheatly/sheet.rb', line 3

def title
  @title
end

Class Method Details

.adapterObject



44
45
46
# File 'lib/cheatly/sheet.rb', line 44

def self.adapter
  @adapter ||= Cheatly::Adapter::GitHub.new
end

.allObject



39
40
41
42
# File 'lib/cheatly/sheet.rb', line 39

def self.all
  handles = adapter.all
  handles.map { |h| Sheet.new(h, nil) }
end

.find(name) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/cheatly/sheet.rb', line 30

def self.find(name)
  body = adapter.find(name)
  Sheet.new(name, body, persisted: true)
rescue Octokit::NotFound => e
  nil
rescue RuntimeError => e
  puts e.message
end

.with_file_adapterObject



48
49
50
51
# File 'lib/cheatly/sheet.rb', line 48

def self.with_file_adapter
  @adapter = Cheatly::Adapter::File.new
  self
end

Instance Method Details

#createObject



14
15
16
# File 'lib/cheatly/sheet.rb', line 14

def create
  adapter.create(title, body)
end

#saveObject



22
23
24
25
26
27
28
# File 'lib/cheatly/sheet.rb', line 22

def save
  if @persisted
    update
  else
    create
  end
end

#to_sObject



10
11
12
# File 'lib/cheatly/sheet.rb', line 10

def to_s
  body.to_s
end

#updateObject



18
19
20
# File 'lib/cheatly/sheet.rb', line 18

def update
  adapter.update(title, body)
end