class_notes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eab5805e4b978258c9dc4c0f1c59a5b319855db9
4
+ data.tar.gz: f9a5eecefd88a15d7385f07105653d65027bd947
5
+ SHA512:
6
+ metadata.gz: a5b45f9cf7c20e3fc5515b8f0ee2ae35e8fe91a0b3151def792c0c0de5549758ba174f67cf0a32d8a2eb6ba289d1e6c2b9ac5af6b93057516fe66903e1f266ab
7
+ data.tar.gz: 58ecd321ecbddef2f8330382ccbaedcd0c74207a882ce67dd09de211fa7403312b1e81ded5153c1f12da27aecbd03857d87e8b0e25fe1a1e87288ccef2b8524d
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # class_notes
2
+ make your classes take notes
3
+
4
+ ## install:
5
+ gem install class_notes
6
+
7
+ ## usage:
8
+
9
+ ```ruby
10
+ require 'class_notes'
11
+
12
+ class SimpleMath
13
+ def add(a:, b:)
14
+ a + b
15
+ end
16
+
17
+ def add_4(a:,b:,c:,d:)
18
+ add(a: add(a: a, b: b), b: add(a: c, b: d) )
19
+ end
20
+ end
21
+
22
+ notebook = ClassNotes.jot(SimpleMath).new
23
+ notebook.add_4(a: 1, b: 2, c: 3, d: 4) # => 10
24
+ puts notebook.notes # =>
25
+ #SimpleMathNotebook
26
+ # add_4
27
+ # add
28
+ # {:args=>{:a=>"1", :b=>"2"}, :result=>3}
29
+ # add
30
+ # {:args=>{:a=>"3", :b=>"4"}, :result=>7}
31
+ # add
32
+ # {:args=>{:a=>"3", :b=>"7"}, :result=>10}
33
+ # {:args=>{:a=>"1", :b=>"2", :c=>"3", :d=>"4"}, :result=>10}
34
+ # {}
35
+ ```
@@ -0,0 +1,21 @@
1
+ require_relative 'class_notes/note'
2
+ require_relative 'class_notes/notebook'
3
+
4
+ module ClassNotes
5
+ ::Version = [1, 0, 0]
6
+
7
+ ## @brief create a new note taker Proc on parent
8
+ ## @param parent the parent note
9
+ ## @return a new Proc that can add notes to the parent
10
+ def self.note_taker(parent)
11
+ ->(note){ parent << note }
12
+ end
13
+
14
+ ## @brief wrap a class in notes
15
+ ## @param klass the class
16
+ ## @return a wrapped class
17
+ def self.jot(klass)
18
+ ClassNotes::Notebook.wrap_class(klass)
19
+ end
20
+
21
+ end
@@ -0,0 +1,73 @@
1
+ module ClassNotes
2
+ ##
3
+ ## @brief Class for a single step.
4
+ ##
5
+ class Note
6
+ attr_accessor :title, :data, :children
7
+
8
+ ##
9
+ ## @brief makes a new note
10
+ ##
11
+ ## @param title The title of the note
12
+ ## @param data The note's data
13
+ ##
14
+ ## @return a new note object
15
+ ##
16
+ def initialize(title:, data:)
17
+ @children = []
18
+ @title = title
19
+ @data = data
20
+ end
21
+
22
+ ##
23
+ ## @brief clears a notes children
24
+ ##
25
+ ## @return []
26
+ ##
27
+ def reset!
28
+ @children = []
29
+ end
30
+
31
+ ##
32
+ ## @brief add children
33
+ ##
34
+ ## @param other the child, can be either a note or a hash. If a hash is
35
+ ## given, a new note will be created using the hash as its
36
+ ## arguments
37
+ ##
38
+ ## @return []
39
+ ##
40
+ def <<(other)
41
+ case other
42
+ when Note
43
+ @children << other
44
+ when Hash
45
+ @children << Note.new(other)
46
+ end
47
+ @children.last
48
+ end
49
+
50
+ ## @brief render the note as a string
51
+ ## @param indent the indent level of the base string
52
+ ## @return String
53
+ def to_s(indent=0)
54
+ [
55
+ "#{" " * indent}#{title}",
56
+ unless children.empty?
57
+ children.map { |child| child.to_s(indent+1) }.join("\n")
58
+ end,
59
+ "#{" " * (indent+1)}#{data}"
60
+ ].compact.join("\n")
61
+ end
62
+
63
+ ## @brief render the note as a hash
64
+ ## @return Hash
65
+ def to_h
66
+ {
67
+ title: title,
68
+ children: children.empty? ? nil : children.map { |child| child.to_h },
69
+ data: data
70
+ }.compact
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,58 @@
1
+ module ClassNotes
2
+ module Notebook
3
+ ::WrappedSuffix = "Notebook"
4
+
5
+ ## @brief wrap a class in notes
6
+ ## @param klass the class
7
+ ## @return a copy of Klass with each of its methods wrapped
8
+ def self.wrap_class(klass)
9
+ notebook = Class.new(klass) {
10
+ include Notebook
11
+
12
+ attr_reader :notes, :add_note
13
+
14
+ def initialize
15
+ @notes = ClassNotes::Note.new(title: self.class.to_s, data: {})
16
+ @add_note = ClassNotes.note_taker(notes)
17
+ wrap_methods(*self.class.superclass.instance_methods(false))
18
+ end
19
+ }
20
+
21
+
22
+ notebook_name = "#{klass.to_s}#{::WrappedSuffix}"
23
+ Object.const_set notebook_name, notebook
24
+
25
+ notebook
26
+ end
27
+
28
+ ## @brief wrap methods with notes
29
+ ## @param meths the methods to wrap
30
+ ## @return nil
31
+ def wrap_methods(*meths)
32
+ meths.each { |meth|
33
+ m = method(meth)
34
+ define_singleton_method(meth) { |*args, &block|
35
+
36
+ parent = add_note
37
+ child = add_note.({title: meth.to_s, data: {}})
38
+ @add_note = ClassNotes.note_taker(child)
39
+
40
+ result = super(*args, &block)
41
+ @add_note = parent
42
+
43
+ pretty_args =
44
+ case args.first
45
+ when Hash
46
+ args.first.map { |t, v| [t, v.to_s]}.to_h
47
+ else
48
+ args
49
+ end
50
+
51
+ child.data = {args: pretty_args, result: result}
52
+
53
+ result
54
+ }
55
+ }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,13 @@
1
+ class SimpleMath
2
+ def add(a:, b:)
3
+ add_2(a, b)
4
+ end
5
+
6
+ def add_2(a, b)
7
+ a + b
8
+ end
9
+
10
+ def add_4(a:,b:,c:,d:)
11
+ add(a: add(a: a, b: b), b: add(a: c, b: d) )
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/unit'
2
+ require_relative '../lib/class_notes'
3
+ require_relative 'simple_math'
4
+
5
+ class TestClassNotes < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @notebook = ClassNotes.jot(SimpleMath).new
9
+ end
10
+
11
+ def teardown
12
+ Object.send(:remove_const, @notebook.class.to_s)
13
+ end
14
+
15
+ def test_notebook
16
+ [:add, :add_2, :add_4].each { |meth|
17
+ assert @notebook.respond_to? meth
18
+ }
19
+ end
20
+
21
+ def test_unobtrusiveness
22
+ assert_equal @notebook.add_4(a: 1, b: 2, c: 3, d: 4), 10
23
+ end
24
+
25
+ def test_penmanship
26
+ @notebook.add_4(a: 1, b: 2, c: 3, d: 4)
27
+ notes = @notebook.notes
28
+ assert_equal notes.title, "SimpleMathNotebook"
29
+ assert_equal notes.to_h[:children].first[:children][2][:children].first[:data][:args][1], 7
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_notes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - annacrombie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: wrap a classes instance methods in logic that keeps track of their actions
14
+ email: stone.tickle@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - "./README.md"
20
+ - "./lib/class_notes.rb"
21
+ - "./lib/class_notes/note.rb"
22
+ - "./lib/class_notes/notebook.rb"
23
+ - "./test/simple_math.rb"
24
+ - "./test/test_class_notes.rb"
25
+ homepage: https://github.com/uab-cs/class_notes/
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.11
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: make your classes take notes
49
+ test_files: []