class_notes 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/class_notes.rb +1 -1
- data/lib/class_notes/note.rb +54 -9
- data/lib/class_notes/notebook.rb +9 -18
- data/test/simple_math.rb +4 -0
- data/test/test_class_notes.rb +5 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c086793af4a6d967fc3d39cf4da4e267507e72ae
|
4
|
+
data.tar.gz: ed62bbe8a97d9e8d096c153643f299a729c5cbf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 147ac4547ec928963ae8288fd8c607cc42cf61ef23e8629906cd6e4456559df3e95f340771aa7b2a41b0c781fe940aa07f4d06108f41a688f3eaa016f028beb1
|
7
|
+
data.tar.gz: e881a25cb665bc54417a04245e5ca847b2f4b46ae3683c29056b4a7b71c6318c7d6c822da5c7aa6c37fb73a0a8a195a2355370b85bd51f1c1fe0bd69157adfc3
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# class_notes
|
2
|
-
[![Gem Version](https://
|
2
|
+
[![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&type=6&v=2.0.0&x2=0)](https://rubygems.org/gems/class_notes)
|
3
3
|
|
4
4
|
make your classes take notes
|
5
5
|
|
@@ -35,3 +35,5 @@ puts notebook.notes # =>
|
|
35
35
|
# {:args=>{:a=>"1", :b=>"2", :c=>"3", :d=>"4"}, :result=>10}
|
36
36
|
# {}
|
37
37
|
```
|
38
|
+
## docs:
|
39
|
+
http://www.rubydoc.info/gems/class_notes
|
data/lib/class_notes.rb
CHANGED
data/lib/class_notes/note.rb
CHANGED
@@ -3,7 +3,8 @@ module ClassNotes
|
|
3
3
|
## @brief Class for a single step.
|
4
4
|
##
|
5
5
|
class Note
|
6
|
-
attr_accessor :title, :
|
6
|
+
attr_accessor :title, :children
|
7
|
+
attr_reader :args, :results
|
7
8
|
|
8
9
|
##
|
9
10
|
## @brief makes a new note
|
@@ -13,10 +14,39 @@ module ClassNotes
|
|
13
14
|
##
|
14
15
|
## @return a new note object
|
15
16
|
##
|
16
|
-
def initialize(title:,
|
17
|
-
@children = []
|
17
|
+
def initialize(title:, args:nil, results:nil)
|
18
18
|
@title = title
|
19
|
-
|
19
|
+
self.args=(args) if args
|
20
|
+
self.results=(results) if results
|
21
|
+
|
22
|
+
@children = []
|
23
|
+
end
|
24
|
+
|
25
|
+
def args=(args)
|
26
|
+
@args =
|
27
|
+
if args.length == 1 and args.first.class == Hash
|
28
|
+
normalize(args.first)
|
29
|
+
else
|
30
|
+
normalize(args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def results=(data)
|
35
|
+
@results=normalize(data)
|
36
|
+
end
|
37
|
+
|
38
|
+
def normalize(data)
|
39
|
+
case data
|
40
|
+
when Hash
|
41
|
+
data.map { |t, v| [t, v.to_s]}.to_h
|
42
|
+
when Array
|
43
|
+
i = -1
|
44
|
+
data.map { |e|
|
45
|
+
[ i+=1 , e.to_s]
|
46
|
+
}.to_h
|
47
|
+
else
|
48
|
+
{0 => "#{data}"}
|
49
|
+
end
|
20
50
|
end
|
21
51
|
|
22
52
|
##
|
@@ -47,16 +77,30 @@ module ClassNotes
|
|
47
77
|
@children.last
|
48
78
|
end
|
49
79
|
|
80
|
+
def h_to_s(hash, pre)
|
81
|
+
hash.map { |i, a| pre + "#{i}: #{a}" }.join("\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
def tab(i)
|
85
|
+
" " * i
|
86
|
+
end
|
87
|
+
|
50
88
|
## @brief render the note as a string
|
51
89
|
## @param indent the indent level of the base string
|
52
90
|
## @return String
|
53
|
-
def to_s(
|
91
|
+
def to_s(i=0)
|
54
92
|
[
|
55
|
-
|
93
|
+
tab(i) + "#{title}:",
|
94
|
+
if args and not args.empty?
|
95
|
+
tab(i+1) + "args:\n" + h_to_s(args, tab(i+2))
|
96
|
+
end,
|
56
97
|
unless children.empty?
|
57
|
-
|
98
|
+
tab(i+1) + "children:\n" +
|
99
|
+
children.map { |child| child.to_s(i+2) }.join("\n")
|
58
100
|
end,
|
59
|
-
|
101
|
+
if results
|
102
|
+
tab(i+1) + "results:\n" + h_to_s(results, tab(i+2))
|
103
|
+
end
|
60
104
|
].compact.join("\n")
|
61
105
|
end
|
62
106
|
|
@@ -65,8 +109,9 @@ module ClassNotes
|
|
65
109
|
def to_h
|
66
110
|
{
|
67
111
|
title: title,
|
112
|
+
args: args,
|
68
113
|
children: children.empty? ? nil : children.map { |child| child.to_h },
|
69
|
-
|
114
|
+
results: results
|
70
115
|
}.compact
|
71
116
|
end
|
72
117
|
end
|
data/lib/class_notes/notebook.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module ClassNotes
|
2
2
|
module Notebook
|
3
|
-
::WrappedSuffix = "Notebook"
|
3
|
+
self::WrappedSuffix = "Notebook"
|
4
4
|
|
5
5
|
## @brief wrap a class in notes
|
6
6
|
## @param klass the class
|
@@ -11,15 +11,15 @@ module ClassNotes
|
|
11
11
|
|
12
12
|
attr_reader :notes, :add_note
|
13
13
|
|
14
|
-
def initialize
|
15
|
-
@notes = ClassNotes::Note.new(title: self.class.to_s
|
14
|
+
def initialize(*args, &block)
|
15
|
+
@notes = ClassNotes::Note.new(title: self.class.to_s)
|
16
16
|
@add_note = ClassNotes.note_taker(notes)
|
17
17
|
wrap_methods(*self.class.superclass.instance_methods(false))
|
18
|
+
super(*args, &block)
|
18
19
|
end
|
19
20
|
}
|
20
21
|
|
21
|
-
|
22
|
-
notebook_name = "#{klass.to_s}#{::WrappedSuffix}"
|
22
|
+
notebook_name = "#{klass.name.split("::").last}#{Notebook::WrappedSuffix}"
|
23
23
|
Object.const_set notebook_name, notebook
|
24
24
|
|
25
25
|
notebook
|
@@ -32,25 +32,16 @@ module ClassNotes
|
|
32
32
|
meths.each { |meth|
|
33
33
|
m = method(meth)
|
34
34
|
define_singleton_method(meth) { |*args, &block|
|
35
|
-
|
36
35
|
parent = add_note
|
37
|
-
child = add_note.({title: meth.to_s,
|
36
|
+
child = add_note.({title: meth.to_s, args: args})
|
38
37
|
@add_note = ClassNotes.note_taker(child)
|
39
38
|
|
40
|
-
|
39
|
+
results = super(*args, &block)
|
41
40
|
@add_note = parent
|
42
41
|
|
43
|
-
|
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}
|
42
|
+
child.results = results
|
52
43
|
|
53
|
-
|
44
|
+
results
|
54
45
|
}
|
55
46
|
}
|
56
47
|
end
|
data/test/simple_math.rb
CHANGED
data/test/test_class_notes.rb
CHANGED
@@ -26,7 +26,11 @@ class TestClassNotes < Test::Unit::TestCase
|
|
26
26
|
@notebook.add_4(a: 1, b: 2, c: 3, d: 4)
|
27
27
|
notes = @notebook.notes
|
28
28
|
assert_equal notes.title, "SimpleMathNotebook"
|
29
|
-
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_recursion
|
32
|
+
@notebook.recurse
|
33
|
+
puts @notebook.notes
|
30
34
|
end
|
31
35
|
|
32
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_notes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- annacrombie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: wrap a classes instance methods in logic that keeps track of their actions
|
14
14
|
email: stone.tickle@gmail.com
|