Class: Ncn::NoteLoader
- Inherits:
-
Object
- Object
- Ncn::NoteLoader
- Defined in:
- lib/ncn/note_loader.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
Instance Method Summary collapse
- #body ⇒ Object
- #created ⇒ Object
- #extract_approximate_timestamp ⇒ Object
- #extract_meta ⇒ Object
- #id ⇒ Object
-
#initialize(file_name) ⇒ NoteLoader
constructor
A new instance of NoteLoader.
- #load ⇒ Object
- #meta ⇒ Object
- #tags ⇒ Object
- #text ⇒ Object
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_name ⇒ Object (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
#body ⇒ Object
45 46 47 |
# File 'lib/ncn/note_loader.rb', line 45 def body text.gsub(/\A---(.|\n)*---\s*/, "").strip end |
#created ⇒ Object
22 23 24 25 |
# File 'lib/ncn/note_loader.rb', line 22 def created value = ["created"] value.is_a?(Time) ? value : end |
#extract_approximate_timestamp ⇒ Object
27 28 29 30 |
# File 'lib/ncn/note_loader.rb', line 27 def year, month, day = file_name.split(File::SEPARATOR)[-4..-2] Time.new(Integer(year), Integer(month), Integer(day), 0, 0, 0, 0) end |
#extract_meta ⇒ Object
40 41 42 43 |
# File 'lib/ncn/note_loader.rb', line 40 def result = YAML.safe_load(text, permitted_classes: [Time]) result.is_a?(Hash) ? result : {} end |
#id ⇒ Object
18 19 20 |
# File 'lib/ncn/note_loader.rb', line 18 def id @id ||= Integer(File.basename(file_name.split(".").first)) end |
#load ⇒ Object
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: , body: body ) end |
#meta ⇒ Object
36 37 38 |
# File 'lib/ncn/note_loader.rb', line 36 def ||= end |
#tags ⇒ Object
32 33 34 |
# File 'lib/ncn/note_loader.rb', line 32 def ["tags"].to_s.split(",").map(&:strip) end |
#text ⇒ Object
49 50 51 |
# File 'lib/ncn/note_loader.rb', line 49 def text @text ||= File.read(file_name) end |