Class: Ncn::Note

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

Constant Summary collapse

MAX_TITLE_LENGTH =
80
OMISSION =
"..."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, created: nil, tags: nil, body: "") ⇒ Note

Returns a new instance of Note.



9
10
11
12
13
14
# File 'lib/ncn/note.rb', line 9

def initialize(id:, created: nil, tags: nil, body: "")
  @id = id
  @created = created || Time.now
  @tags = tags
  @body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/ncn/note.rb', line 7

def body
  @body
end

#createdObject (readonly)

Returns the value of attribute created.



6
7
8
# File 'lib/ncn/note.rb', line 6

def created
  @created
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/ncn/note.rb', line 6

def id
  @id
end

#tagsObject

Returns the value of attribute tags.



7
8
9
# File 'lib/ncn/note.rb', line 7

def tags
  @tags
end

Instance Method Details

#metaObject



22
23
24
25
26
27
28
# File 'lib/ncn/note.rb', line 22

def meta
  {
    "title" => title,
    "tags" => csv(uniq_tags),
    "created" => created.to_s
  }
end

#pathObject



35
36
37
# File 'lib/ncn/note.rb', line 35

def path
  NotePathBuilder.new(id: id, created: created).path
end

#titleObject



16
17
18
19
20
# File 'lib/ncn/note.rb', line 16

def title
  first_line = body.to_s.strip.lines.first.to_s.strip.gsub(/^#+\s+/, "")
  return first_line if first_line.length < MAX_TITLE_LENGTH
  first_line[0..(MAX_TITLE_LENGTH - OMISSION.length.succ)] + OMISSION
end

#to_hObject



39
40
41
42
43
44
45
46
# File 'lib/ncn/note.rb', line 39

def to_h
  {
    id: id,
    created: created,
    tags: uniq_tags,
    body: body
  }
end

#to_sObject



48
49
50
# File 'lib/ncn/note.rb', line 48

def to_s
  "#{meta.to_yaml}---\n\n#{body.strip}"
end

#uniq_tagsObject



30
31
32
33
# File 'lib/ncn/note.rb', line 30

def uniq_tags
  return [] unless tags
  tags.uniq(&:downcase)
end