Class: Pastie::History
- Inherits:
-
Object
- Object
- Pastie::History
- Defined in:
- lib/pastie-api/history.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
Returns the value of attribute links.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#add(url) ⇒ Object
Add new paste to the history.
-
#flush ⇒ Object
Flush history.
-
#initialize ⇒ History
constructor
A new instance of History.
-
#load ⇒ Object
Load history.
-
#print ⇒ Object
Print all existing pastes.
-
#save ⇒ Object
Save history.
Constructor Details
#initialize ⇒ History
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
#links ⇒ Object
Returns the value of attribute links.
4 5 6 |
# File 'lib/pastie-api/history.rb', line 4 def links @links end |
#path ⇒ Object (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 |
#flush ⇒ Object
Flush history
31 32 33 |
# File 'lib/pastie-api/history.rb', line 31 def flush @links = [] end |
#load ⇒ Object
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 ⇒ Object
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 |
#save ⇒ Object
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 |