class ClassNotes::Note

@brief Class for a single step.

Attributes

args[R]
children[RW]
results[R]
title[RW]

Public Class Methods

new(title:, args:nil, results:nil) click to toggle source

@brief makes a new note

@param title The title of the note @param data The note's data

@return a new note object

# 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

Public Instance Methods

<<(other) click to toggle source

@brief add children

@param 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

@return []

# 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
args=(args) click to toggle source
# File lib/class_notes/note.rb, line 25
def args=(args)
  @args =
    if args.length == 1 and args.first.class == Hash
      normalize(args.first)
    else
      normalize(args)
    end
end
h_to_s(hash, pre) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source

@brief clears a notes children

@return []

# File lib/class_notes/note.rb, line 57
def reset!
  @children = []
end
results=(data) click to toggle source
# File lib/class_notes/note.rb, line 34
def results=(data)
  @results=normalize(data)
end
tab(i) click to toggle source
# File lib/class_notes/note.rb, line 84
def tab(i)
  "  " * i
end
to_h() click to toggle source

@brief render the note as a hash @return Hash

# 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) click to toggle source

@brief render the note as a string @param indent the indent level of the base string @return String

# 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