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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c438fdb093ecd7108e075de06c7a4f6c909fe9c6
4
- data.tar.gz: e24bbe368325884d3c242283a7265cb5ce966baa
3
+ metadata.gz: c086793af4a6d967fc3d39cf4da4e267507e72ae
4
+ data.tar.gz: ed62bbe8a97d9e8d096c153643f299a729c5cbf9
5
5
  SHA512:
6
- metadata.gz: 0e981e9ef4763017560efc5d3ee7f6dea94131ce76e9e53bbb921450c85167b11ce6fedc9399cb8cb37a31dda4eeabeb74551138f30736a4a59c00bb1288de50
7
- data.tar.gz: 030f1a10b8bf78c85c350b712baf8e9608a491727c74762189e05fda660deac3b00100fc8d4a0a1c6b6e501690fed1ed874b06e89f2913e63882e77b4fd02f45
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://badge.fury.io/rb/class_notes.svg)](https://badge.fury.io/rb/class_notes)
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
@@ -2,7 +2,7 @@ require_relative 'class_notes/note'
2
2
  require_relative 'class_notes/notebook'
3
3
 
4
4
  module ClassNotes
5
- ::Version = [1, 0, 1]
5
+ self::Version = [2, 0, 0]
6
6
 
7
7
  ## @brief create a new note taker Proc on parent
8
8
  ## @param parent the parent note
@@ -3,7 +3,8 @@ module ClassNotes
3
3
  ## @brief Class for a single step.
4
4
  ##
5
5
  class Note
6
- attr_accessor :title, :data, :children
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:, data:)
17
- @children = []
17
+ def initialize(title:, args:nil, results:nil)
18
18
  @title = title
19
- @data = data
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(indent=0)
91
+ def to_s(i=0)
54
92
  [
55
- "#{" " * indent}#{title}",
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
- children.map { |child| child.to_s(indent+1) }.join("\n")
98
+ tab(i+1) + "children:\n" +
99
+ children.map { |child| child.to_s(i+2) }.join("\n")
58
100
  end,
59
- "#{" " * (indent+1)}#{data}"
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
- data: data
114
+ results: results
70
115
  }.compact
71
116
  end
72
117
  end
@@ -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, data: {})
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, data: {}})
36
+ child = add_note.({title: meth.to_s, args: args})
38
37
  @add_note = ClassNotes.note_taker(child)
39
38
 
40
- result = super(*args, &block)
39
+ results = super(*args, &block)
41
40
  @add_note = parent
42
41
 
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}
42
+ child.results = results
52
43
 
53
- result
44
+ results
54
45
  }
55
46
  }
56
47
  end
@@ -7,6 +7,10 @@ class SimpleMath
7
7
  a + b
8
8
  end
9
9
 
10
+ def recurse(i=0)
11
+ recurse i+1 unless i > 10
12
+ end
13
+
10
14
  def add_4(a:,b:,c:,d:)
11
15
  add(a: add(a: a, b: b), b: add(a: c, b: d) )
12
16
  end
@@ -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
- assert_equal notes.to_h[:children].first[:children][2][:children].first[:data][:args][1], 7
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: 1.0.1
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-10 00:00:00.000000000 Z
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