Class: ClassNotes::Note
- Inherits:
-
Object
- Object
- ClassNotes::Note
- Defined in:
- lib/class_notes/note.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#children ⇒ Object
Returns the value of attribute children.
-
#results ⇒ Object
Returns the value of attribute results.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
[].
- #h_to_s(hash, pre) ⇒ Object
-
#initialize(title:, args: nil, results: nil) ⇒ Object
constructor
A new note object.
- #normalize(data) ⇒ Object
-
#reset! ⇒ Object
[].
- #tab(i) ⇒ Object
-
#to_h ⇒ Object
Hash.
-
#to_s(i = 0) ⇒ Object
String.
Constructor Details
#initialize(title:, args: nil, results: nil) ⇒ Object
Returns a new note object.
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
#args ⇒ Object
Returns the value of attribute args.
7 8 9 |
# File 'lib/class_notes/note.rb', line 7 def args @args end |
#children ⇒ Object
Returns the value of attribute children.
6 7 8 |
# File 'lib/class_notes/note.rb', line 6 def children @children end |
#results ⇒ Object
Returns the value of attribute results.
7 8 9 |
# File 'lib/class_notes/note.rb', line 7 def results @results 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 [].
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 [].
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_h ⇒ Object
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.
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 |