Module: ClassNotes::Notebook
- Defined in:
- lib/class_notes/notebook.rb
Class Method Summary collapse
-
.wrap_class(klass) ⇒ Object
A copy of Klass with each of its methods wrapped.
Instance Method Summary collapse
Class Method Details
.wrap_class(klass) ⇒ Object
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 @notes = ClassNotes::Note.new(title: self.class.to_s, data: {}) @add_note = ClassNotes.note_taker(notes) wrap_methods(*self.class.superclass.instance_methods(false)) end } notebook_name = "#{klass.to_s}#{::WrappedSuffix}" Object.const_set notebook_name, notebook notebook end |
Instance Method Details
#wrap_methods(*meths) ⇒ Object
Returns nil.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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, data: {}}) @add_note = ClassNotes.note_taker(child) result = super(*args, &block) @add_note = parent pretty_args = case args.first when Hash args.first.map { |t, v| [t, v.to_s]}.to_h else args end child.data = {args: pretty_args, result: result} result } } end |