Class: Ncn::NoteLoader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ NoteLoader

Returns a new instance of NoteLoader.



5
6
7
# File 'lib/ncn/note_loader.rb', line 5

def initialize(file_name)
  @file_name = file_name
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



3
4
5
# File 'lib/ncn/note_loader.rb', line 3

def file_name
  @file_name
end

Instance Method Details

#bodyObject



45
46
47
# File 'lib/ncn/note_loader.rb', line 45

def body
  text.gsub(/\A---(.|\n)*---\s*/, "").strip
end

#createdObject



22
23
24
25
# File 'lib/ncn/note_loader.rb', line 22

def created
  value = meta["created"]
  value.is_a?(Time) ? value : extract_approximate_timestamp
end

#extract_approximate_timestampObject



27
28
29
30
# File 'lib/ncn/note_loader.rb', line 27

def extract_approximate_timestamp
  year, month, day = file_name.split(File::SEPARATOR)[-4..-2]
  Time.new(Integer(year), Integer(month), Integer(day), 0, 0, 0, 0)
end

#extract_metaObject



40
41
42
43
# File 'lib/ncn/note_loader.rb', line 40

def extract_meta
  result = YAML.safe_load(text, permitted_classes: [Time])
  result.is_a?(Hash) ? result : {}
end

#idObject



18
19
20
# File 'lib/ncn/note_loader.rb', line 18

def id
  @id ||= Integer(File.basename(file_name.split(".").first))
end

#loadObject



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

def load
  Note.new(
    id: id,
    created: created,
    tags: tags,
    body: body
  )
end

#metaObject



36
37
38
# File 'lib/ncn/note_loader.rb', line 36

def meta
  @meta ||= extract_meta
end

#tagsObject



32
33
34
# File 'lib/ncn/note_loader.rb', line 32

def tags
  meta["tags"].to_s.split(",").map(&:strip)
end

#textObject



49
50
51
# File 'lib/ncn/note_loader.rb', line 49

def text
  @text ||= File.read(file_name)
end