Class: Note
- Inherits:
-
Object
- Object
- Note
- Defined in:
- lib/cnote/note.rb
Constant Summary collapse
- @@meta_regex =
/^<!\-{3}(.*)\-{2}>/
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#created ⇒ Object
Returns the value of attribute created.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#index ⇒ Object
Returns the value of attribute index.
-
#modified ⇒ Object
readonly
Returns the value of attribute modified.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_tags(tags) ⇒ Object
- #excerpt ⇒ Object
-
#initialize(path) ⇒ Note
constructor
A new instance of Note.
- #refresh(contents) ⇒ Object
- #remove_tags(tags) ⇒ Object
- #time_fmt(time) ⇒ Object
- #title_limit(length) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(path) ⇒ Note
Returns a new instance of Note.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cnote/note.rb', line 14 def initialize(path) @content = "" @tags = [] @filename = File.basename(path) @path = path File.open(@path, "r") do |file| refresh(file.read) end @modified = File.mtime(@path) if !@modified @created = @modified if !@created @title = "Untitled" if !@title end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/cnote/note.rb', line 4 def content @content end |
#created ⇒ Object
Returns the value of attribute created.
10 11 12 |
# File 'lib/cnote/note.rb', line 10 def created @created end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/cnote/note.rb', line 4 def filename @filename end |
#index ⇒ Object
Returns the value of attribute index.
10 11 12 |
# File 'lib/cnote/note.rb', line 10 def index @index end |
#modified ⇒ Object (readonly)
Returns the value of attribute modified.
4 5 6 |
# File 'lib/cnote/note.rb', line 4 def modified @modified end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/cnote/note.rb', line 4 def path @path end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
4 5 6 |
# File 'lib/cnote/note.rb', line 4 def @tags end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
4 5 6 |
# File 'lib/cnote/note.rb', line 4 def title @title end |
Instance Method Details
#add_tags(tags) ⇒ Object
49 50 51 52 53 |
# File 'lib/cnote/note.rb', line 49 def () @tags = @tags.concat() @modified = Time.new end |
#excerpt ⇒ Object
69 70 71 |
# File 'lib/cnote/note.rb', line 69 def excerpt @content.gsub(/[#*\-~]/i, "").strip.slice(0, 80) end |
#refresh(contents) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cnote/note.rb', line 30 def refresh(contents) @title = nil @content = '' contents.each_line do |line| line = line.strip if @@meta_regex =~ line ($~[1]) elsif !@title && line[0] == '#' @title = line.gsub(/#|[^a-z0-9\s\.\-]/i, "").strip else @content << line + "\n" end end # If no Markdown header is found, name it by the file's name. @title = File.basename(@filename, File.extname(@filename)) if !@title end |
#remove_tags(tags) ⇒ Object
55 56 57 58 59 |
# File 'lib/cnote/note.rb', line 55 def () @tags = @tags - @modified = Time.new end |
#time_fmt(time) ⇒ Object
73 74 75 |
# File 'lib/cnote/note.rb', line 73 def time_fmt(time) time.strftime("%A, %B %e %Y, %l:%M:%S%p") end |
#title_limit(length) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/cnote/note.rb', line 61 def title_limit(length) if @title.length >= length @title.strip.slice(0, length - 3) + "..." else @title end end |
#update ⇒ Object
77 78 79 80 |
# File 'lib/cnote/note.rb', line 77 def update @modified = Time.new end |