Class: Vita::GardenNote

Inherits:
Object
  • Object
show all
Defined in:
lib/vita/garden_note.rb

Constant Summary collapse

EXCERPT_LENGTH =
160

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(garden, note) ⇒ GardenNote

Returns a new instance of GardenNote.



10
11
12
13
# File 'lib/vita/garden_note.rb', line 10

def initialize(garden, note)
  @garden = garden
  @note = note
end

Instance Attribute Details

#gardenObject (readonly)

Returns the value of attribute garden.



8
9
10
# File 'lib/vita/garden_note.rb', line 8

def garden
  @garden
end

Instance Method Details

#all_namesObject

Get all names for this note, including its title and synonyms.



30
31
32
# File 'lib/vita/garden_note.rb', line 30

def all_names
  [title, *synonyms]
end


71
72
73
# File 'lib/vita/garden_note.rb', line 71

def backlinks
  garden.links_to(self)
end

#contentObject



49
50
51
52
53
54
55
# File 'lib/vita/garden_note.rb', line 49

def content
  if @note.content.start_with? "Synonyms:"
    @note.content[@note.content.index("\n") + 1..]
  else
    @note.content
  end
end

#excerptObject



61
62
63
64
65
# File 'lib/vita/garden_note.rb', line 61

def excerpt
  excerpt = content[0, EXCERPT_LENGTH]
  excerpt += "" if excerpt.length < content.length
  excerpt
end

#home?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/vita/garden_note.rb', line 79

def home?
  garden.home_note == self
end

#htmlObject



57
58
59
# File 'lib/vita/garden_note.rb', line 57

def html
  Format.for_filename(@note.filename).to_html(content)
end


75
76
77
# File 'lib/vita/garden_note.rb', line 75

def links
  outlinks + backlinks
end

#names_regexpObject

Get a regular expression that matches any of this note’s names surrounded by word boundaries.



36
37
38
39
# File 'lib/vita/garden_note.rb', line 36

def names_regexp
  elements = all_names.map { |name| /#{Regexp.quote(name)}/i }
  /\b#{Regexp.union(elements)}\b/
end


67
68
69
# File 'lib/vita/garden_note.rb', line 67

def outlinks
  @outlinks ||= create_outlinks
end

#pathObject



41
42
43
44
45
46
47
# File 'lib/vita/garden_note.rb', line 41

def path
  if home?
    "index.html"
  else
    @note.title.downcase.gsub(/\s+/, "-") + ".html"
  end
end

#synonymsObject

Get alternative names for this note.



21
22
23
24
25
26
27
# File 'lib/vita/garden_note.rb', line 21

def synonyms
  if @note.content.start_with? "Synonyms:"
    @note.content[9..@note.content.index("\n")].split(",").map(&:strip)
  else
    []
  end
end

#titleObject

Get this note’s primary title.



16
17
18
# File 'lib/vita/garden_note.rb', line 16

def title
  @note.title
end