Module: ClassNotes::Notebook

Defined in:
lib/class_notes/notebook.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap_class(klass) ⇒ Object

Returns a copy of Klass with each of its methods wrapped.

Parameters:

  • klass

    the class

Returns:

  • a copy of Klass with each of its methods wrapped



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#wrap_methods(*meths) ⇒ Object

Returns nil.

Parameters:

  • meths

    the methods to wrap

Returns:

  • nil



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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