Class: ClassNotes::Note

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, data:) ⇒ Object

Returns a new note object.

Parameters:

  • title

    The title of the note

  • data

    The note’s data



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

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#titleObject

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 [].

Parameters:

  • other

    the child, can be either a note or a hash. If a hash is given, a new note will be created using the hash as its arguments

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 [].

Returns:



27
28
29
# File 'lib/class_notes/note.rb', line 27

def reset!
  @children = []
end

#to_hObject

Returns Hash.

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.

Parameters:

  • indent (defaults to: 0)

    the indent level of the base string

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