module ClassNotes::Notebook

Constants

WrappedSuffix

Public Class Methods

new(*args, &block) click to toggle source
Calls superclass method
# File lib/class_notes/notebook.rb, line 14
def initialize(*args, &block)
  @notes    = ClassNotes::Note.new(title: self.class.to_s)
  @add_note = ClassNotes.note_taker(notes)
  wrap_methods(*self.class.superclass.instance_methods(false))
  super(*args, &block)
end
wrap_class(klass) click to toggle source

@brief wrap a class in notes @param klass the class @return a copy of Klass with each of its methods wrapped

# File lib/class_notes/notebook.rb, line 8
def self.wrap_class(klass)
  notebook = Class.new(klass) {
    include Notebook

    attr_reader :notes, :add_note

    def initialize(*args, &block)
      @notes    = ClassNotes::Note.new(title: self.class.to_s)
      @add_note = ClassNotes.note_taker(notes)
      wrap_methods(*self.class.superclass.instance_methods(false))
      super(*args, &block)
    end
  }

  notebook_name = "#{klass.name.split("::").last}#{Notebook::WrappedSuffix}"
  Object.const_set notebook_name, notebook

  notebook
end

Public Instance Methods

wrap_methods(*meths) click to toggle source

@brief wrap methods with notes @param meths the methods to wrap @return nil

Calls superclass method
# File lib/class_notes/notebook.rb, line 31
def wrap_methods(*meths)
  meths.each { |meth|
    m = method(meth)
    define_singleton_method(meth) { |*args, &block|
      parent    = add_note
      child     = add_note.({title: meth.to_s, args: args})
      @add_note = ClassNotes.note_taker(child)

      results   = super(*args, &block)
      @add_note = parent

      child.results = results

      results
    }
  }
end