Class: ClassNotes::Note
- Inherits:
-
Object
- Object
- ClassNotes::Note
- Defined in:
- lib/class_notes/note.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#data ⇒ Object
Returns the value of attribute data.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
[].
-
#initialize(title:, data:) ⇒ Object
constructor
A new note object.
-
#reset! ⇒ Object
[].
-
#to_h ⇒ Object
Hash.
-
#to_s(indent = 0) ⇒ Object
String.
Constructor Details
#initialize(title:, data:) ⇒ Object
Returns a new note object.
16 17 18 19 20 |
# File 'lib/class_notes/note.rb', line 16 def initialize(title:, data:) @children = [] @title = title @data = data end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
6 7 8 |
# File 'lib/class_notes/note.rb', line 6 def children @children end |
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/class_notes/note.rb', line 6 def data @data end |
#title ⇒ Object
Returns the value of attribute title.
6 7 8 |
# File 'lib/class_notes/note.rb', line 6 def title @title end |
Instance Method Details
#<<(other) ⇒ Object
Returns [].
40 41 42 43 44 45 46 47 48 |
# File 'lib/class_notes/note.rb', line 40 def <<(other) case other when Note @children << other when Hash @children << Note.new(other) end @children.last end |
#reset! ⇒ Object
Returns [].
27 28 29 |
# File 'lib/class_notes/note.rb', line 27 def reset! @children = [] end |
#to_h ⇒ Object
Returns Hash.
65 66 67 68 69 70 71 |
# File 'lib/class_notes/note.rb', line 65 def to_h { title: title, children: children.empty? ? nil : children.map { |child| child.to_h }, data: data }.compact end |
#to_s(indent = 0) ⇒ Object
Returns String.
53 54 55 56 57 58 59 60 61 |
# File 'lib/class_notes/note.rb', line 53 def to_s(indent=0) [ "#{" " * indent}#{title}", unless children.empty? children.map { |child| child.to_s(indent+1) }.join("\n") end, "#{" " * (indent+1)}#{data}" ].compact.join("\n") end |