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:, args: nil, results: nil) ⇒ Object

Returns a new note object.

Parameters:

  • title

    The title of the note

  • data

    The note’s data



17
18
19
20
21
22
23
# File 'lib/class_notes/note.rb', line 17

def initialize(title:, args:nil, results:nil)
  @title    = title
  self.args=(args) if args
  self.results=(results) if results

  @children = []
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#resultsObject

Returns the value of attribute results.



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

def results
  @results
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:



70
71
72
73
74
75
76
77
78
# File 'lib/class_notes/note.rb', line 70

def <<(other)
  case other
  when Note
    @children << other
  when Hash
    @children << Note.new(other)
  end
  @children.last
end

#h_to_s(hash, pre) ⇒ Object



80
81
82
# File 'lib/class_notes/note.rb', line 80

def h_to_s(hash, pre)
  hash.map { |i, a| pre + "#{i}: #{a}" }.join("\n")
end

#normalize(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/class_notes/note.rb', line 38

def normalize(data)
  case data
  when Hash
    data.map { |t, v| [t, v.to_s]}.to_h
  when Array
    i = -1
    data.map { |e|
      [ i+=1 , e.to_s]
    }.to_h
  else
    {0 => "#{data}"}
  end
end

#reset!Object

Returns [].

Returns:



57
58
59
# File 'lib/class_notes/note.rb', line 57

def reset!
  @children = []
end

#tab(i) ⇒ Object



84
85
86
# File 'lib/class_notes/note.rb', line 84

def tab(i)
  "  " * i
end

#to_hObject

Returns Hash.

Returns:

  • Hash



109
110
111
112
113
114
115
116
# File 'lib/class_notes/note.rb', line 109

def to_h
  {
    title: title,
    args: args,
    children: children.empty? ? nil : children.map { |child| child.to_h },
    results: results
  }.compact
end

#to_s(i = 0) ⇒ Object

Returns String.

Parameters:

  • indent

    the indent level of the base string

Returns:

  • String



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/class_notes/note.rb', line 91

def to_s(i=0)
  [
    tab(i) + "#{title}:",
    if args and not args.empty?
      tab(i+1) + "args:\n" + h_to_s(args, tab(i+2))
    end,
    unless children.empty?
      tab(i+1) + "children:\n" +
      children.map { |child| child.to_s(i+2) }.join("\n")
    end,
    if results
      tab(i+1) + "results:\n" + h_to_s(results, tab(i+2))
    end
  ].compact.join("\n")
end