Class: Note

Inherits:
Object
  • Object
show all
Defined in:
lib/cnote/note.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
29
30
# File 'lib/cnote/note.rb', line 14

def initialize(path)
  @meta_regex = /^<!\-{3}(.*)\-{2}>/

  @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

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/cnote/note.rb', line 4

def content
  @content
end

#createdObject

Returns the value of attribute created.



4
5
6
# File 'lib/cnote/note.rb', line 4

def created
  @created
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/cnote/note.rb', line 4

def filename
  @filename
end

#modifiedObject (readonly)

Returns the value of attribute modified.



4
5
6
# File 'lib/cnote/note.rb', line 4

def modified
  @modified
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/cnote/note.rb', line 4

def path
  @path
end

#tagsObject (readonly)

Returns the value of attribute tags.



4
5
6
# File 'lib/cnote/note.rb', line 4

def tags
  @tags
end

#titleObject (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



50
51
52
53
54
# File 'lib/cnote/note.rb', line 50

def add_tags(tags)
  @tags = @tags.concat(tags)
  @modified = Time.new
  write_meta
end

#excerptObject



62
63
64
# File 'lib/cnote/note.rb', line 62

def excerpt
  @content.gsub(/[#*\-~]/i, "").strip.slice(0, 80)
end

#refresh(contents) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cnote/note.rb', line 32

def refresh(contents)
  @title = nil
  @content = ''

  contents.each_line do |line|
    line = line.strip
    if @meta_regex =~ line
      parse_meta($~[1])
    elsif !@title
      if line != ""
        @title = line.gsub(/#|[^a-z0-9\s\.\-]/i, "").strip
      end
    else
      @content << line + "\n"
    end
  end
end

#remove_tags(tags) ⇒ Object



56
57
58
59
60
# File 'lib/cnote/note.rb', line 56

def remove_tags(tags)
  @tags = @tags - tags
  @modified = Time.new
  write_meta
end

#time_fmt(time) ⇒ Object



66
67
68
# File 'lib/cnote/note.rb', line 66

def time_fmt(time)
  time.strftime("%A, %B %e %Y, %l:%M:%S%p")
end

#updateObject



70
71
72
73
# File 'lib/cnote/note.rb', line 70

def update
  @modified = Time.new
  write_meta
end