Class: Pastie::History

Inherits:
Object
  • Object
show all
Defined in:
lib/pastie-api/history.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHistory

Returns a new instance of History.


6
7
8
9
10
# File 'lib/pastie-api/history.rb', line 6

def initialize
  @path = "#{RUBY_PLATFORM =~ /mswin32|mingw32/ ? ENV['USERPROFILE'] : ENV['HOME']}/.pastie"
  @links = []
  self.load
end

Instance Attribute Details

Returns the value of attribute links


4
5
6
# File 'lib/pastie-api/history.rb', line 4

def links
  @links
end

#pathObject (readonly)

Returns the value of attribute path


3
4
5
# File 'lib/pastie-api/history.rb', line 3

def path
  @path
end

Instance Method Details

#add(url) ⇒ Object

Add new paste to the history


26
27
28
# File 'lib/pastie-api/history.rb', line 26

def add(url)
  @links << url
end

#flushObject

Flush history


31
32
33
# File 'lib/pastie-api/history.rb', line 31

def flush
  @links = []
end

#loadObject

Load history


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pastie-api/history.rb', line 13

def load
  if File.exists?(@path) && File.readable?(@path)
    File.open(@path).readlines("\r\n").each do |l|
      @links << l.strip
    end
  else
    if File.writable?(File.dirname(@path))
      File.open(@path, 'w').close
    end
  end
end

Print all existing pastes


41
42
43
44
45
46
47
48
49
# File 'lib/pastie-api/history.rb', line 41

def print
  puts "====== Pastes History ======".green
  unless @links.empty?
    @links.each_with_index { |l, i| puts "#{i+1}. #{l}" }
  else
    puts "your history is empty..."
  end
  puts "============================".green
end

#saveObject

Save history


36
37
38
# File 'lib/pastie-api/history.rb', line 36

def save
  File.open(@path, 'w') { |f| f.write(@links.join("\r\n")) }
end