Class: Cheatr::Server::Sheet

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/cheatr/server/sheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Sheet

Returns a new instance of Sheet.



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

def initialize(name)
  super(name: name)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.all(query = nil) ⇒ Object

Querying for sheets



69
70
71
72
# File 'lib/cheatr/server/sheet.rb', line 69

def self.all(query = nil)
  query = "*" if query.nil?
  git.ls_files("#{query}.md").keys.map { |f| Sheet.new f.gsub(/\.md\z/, '') }
end

.find(name) ⇒ Object



74
75
76
77
# File 'lib/cheatr/server/sheet.rb', line 74

def self.find(name)
  sheet = Sheet.new(name)
  sheet.new? ? nil : sheet
end

.find!(name) ⇒ Object



79
80
81
# File 'lib/cheatr/server/sheet.rb', line 79

def self.find!(name)
  find(name) or raise Sinatra::NotFound
end

.gitObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cheatr/server/sheet.rb', line 95

def self.git
  if block_given?
    git.reset_hard
    git.checkout('master')
    git.chdir { yield }
    return
  end
  @@git ||= Git.open(repository) rescue init_repository
rescue Errno::EEXIST
  raise Cheatr::Error, "Folder #{repository} is not a valid cheatr repository."
end

.repositoryObject



91
92
93
# File 'lib/cheatr/server/sheet.rb', line 91

def self.repository
  @@repository ||= Dir.pwd
end

.repository=(path) ⇒ Object

Git connection



85
86
87
88
89
# File 'lib/cheatr/server/sheet.rb', line 85

def self.repository=(path)
  @@repository = path
  git # check if the path is a valid repository
  path
end

Instance Method Details

#contentsObject



22
23
24
# File 'lib/cheatr/server/sheet.rb', line 22

def contents
  @contents ||= object.try(:contents)
end

#contents=(new_contents) ⇒ Object



26
27
28
# File 'lib/cheatr/server/sheet.rb', line 26

def contents=(new_contents)
  @contents = filter_contents(new_contents)
end

#file_nameObject



53
54
55
# File 'lib/cheatr/server/sheet.rb', line 53

def file_name
  @file_name ||= "#{name}.md"
end

#git(&block) ⇒ Object



107
108
109
# File 'lib/cheatr/server/sheet.rb', line 107

def git(&block)
  self.class.git(&block)
end

#human_nameObject



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

def human_name
  @human_name ||= name.gsub('.', ' / ')
end

#new?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/cheatr/server/sheet.rb', line 63

def new?
  object.nil?
end

#objectObject



57
58
59
60
61
# File 'lib/cheatr/server/sheet.rb', line 57

def object
  @object ||= git.object("master:#{file_name}")
rescue
  nil
end

#reloadObject



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

def reload
  # Forces reloading when getters are invoked
  @object = @contents = nil
end

#saveObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cheatr/server/sheet.rb', line 30

def save
  return false unless valid?

  git do
    File.open(file_name, 'w') { |f| f.write(contents) }
    git.add(file_name)
    if git.status[file_name].type
      git.commit "#{new? ? 'Create' : 'Update'} #{name}"
      self.reload
    end
  end

  return true
rescue => e
  errors.add(:contents, "couldn't be saved (#{e.message})")
  return false
end