reference_book 0.0.2 → 0.0.3

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: d4b6a7b22b35c94188e41157711d44416a1d61b0
4
- data.tar.gz: 952b181d58f07a5b749df7d5c4b2246bec84d1b1
3
+ metadata.gz: 8c3e7c15bc3227796acd8da604609317be256da9
4
+ data.tar.gz: 314b8f545ee5d8ed14c609d71b48fdabe2bc0436
5
5
  SHA512:
6
- metadata.gz: cc53552201efb3701cd427a90b90030438505c62f420d9f1e5531fe279d0b5b3ab16e042cd6a0d501e58de93e161ca4ab3c6ce34477ccdb25aeafe494719ef77
7
- data.tar.gz: 54d77729f7193df83f9b207b847881b176edcee6d893b5bd47641b991e5e03268164347cecd8ad5988a2bd67588241f9fbddd8e90e1aed9af2f10aea9b71e0c9
6
+ metadata.gz: ed7a48d91de0c053d0b5ff30cb0257a30ac9ce0e4dc9654bdd0928dc7fa6eff63d00d61c00dab16f54513b1348f228ecd6e40ea89ff706b581077a2aef9f9c1c
7
+ data.tar.gz: 77016384a101956dfc1b0ffcd2c9135ac42dd9eefca02fe1cf2b68c2e830827abc308148a604d5188669fba59bc50be857263fb6519442746bbab9a7122af45e
@@ -80,6 +80,59 @@ ReferenceBook.library.hash_for :constitution
80
80
 
81
81
 
82
82
 
83
+ # and complete serializations to Ruby
84
+ ReferenceBook.library.to_h
85
+ # => { :elf => { :strength => 0,
86
+ # :dexterity => 2,
87
+ # :constitution => -2,
88
+ # :intelligence => 0,
89
+ # :wisdom => 0,
90
+ # :charisma => 0 },
91
+ # :dwarf => { :strength => 0,
92
+ # :dexterity => 0,
93
+ # :constitution => 2,
94
+ # :intelligence => 0,
95
+ # :wisdom => 0,
96
+ # :charisma => -2 },
97
+ # :halfling => { :strength => -2,
98
+ # :dexterity => 2,
99
+ # :constitution => 0,
100
+ # :intelligence => 0,
101
+ # :wisdom => 0,
102
+ # :charisma => 0 }
103
+ # }
104
+
105
+ # You can pass 'true' to include 'title' and 'library_key' to the serialized results
106
+ ReferenceBook.library.to_h(true)
107
+ # => { :elf => { # ...like the one above, plus:
108
+ # :title => "Elf",
109
+ # :library_key => :elf },
110
+ # :dwarf => { # ...like the one above, plus:
111
+ # :title => "Dwarf",
112
+ # :library_key => :dwarf },
113
+ # :halfling => { # ...like the one above, plus:
114
+ # :title => "Halfling",
115
+ # :library_key => :halfling }
116
+ # }
117
+
118
+
119
+ ReferenceBook.library.rotate
120
+ # => { :strength => {:elf => 0, :dwarf => 0, :halfling => -2},
121
+ # :dexterity => {:elf => 2, :dwarf => 0, :halfling => 2},
122
+ # :constitution => {:elf => -2, :dwarf => 2, :halfling => 0},
123
+ # :intelligence => {:elf => 0, :dwarf => 0, :halfling => 0},
124
+ # :wisdom => {:elf => 0, :dwarf => 0, :halfling => 0},
125
+ # :charisma => {:elf => 0, :dwarf => -2, :halfling => 0}}
126
+
127
+
128
+ # You can pass 'true' to include 'title' and 'library_key' to the serialized results
129
+ ReferenceBook.library.rotate(true)
130
+ # => { # ...like the one above, plus:
131
+ # :title => {:elf => "Elf", :dwarf => "Dwarf", :halfling => "Halfling"},
132
+ # :library_key => {:elf => :elf, :dwarf => :dwarf, :halfling => :halfling}}
133
+
134
+
135
+
83
136
 
84
137
  # You can wrap it with your own logic:
85
138
 
@@ -1,2 +1,16 @@
1
1
  class ReferenceBook::Book < Struct
2
+
3
+ # To hide a book's meta attributes.
4
+ #
5
+ # Since it would be necessary to override almost all methods,
6
+ # it would be better to drop the inheritance and switch to
7
+ # composition, with the Struct (or a Hash) as a private member.
8
+ #
9
+ # def to_h
10
+ # h = super
11
+ # h.delete(:title)
12
+ # h.delete(:library_key)
13
+ # h
14
+ # end
15
+
2
16
  end
@@ -42,6 +42,20 @@ module ReferenceBook::Library
42
42
 
43
43
 
44
44
 
45
+ def book_keys
46
+ return @book_keys if @book_keys
47
+
48
+ if index.any?
49
+ @book_keys = shelf.map { |k, book| book.members }.flatten.uniq
50
+ @book_keys.delete(:title)
51
+ @book_keys.delete(:library_key)
52
+ @book_keys
53
+ else
54
+ []
55
+ end
56
+ end
57
+
58
+
45
59
 
46
60
  def array_for(attribute)
47
61
  shelf.map do |key, book|
@@ -62,6 +76,48 @@ module ReferenceBook::Library
62
76
 
63
77
 
64
78
 
79
+
80
+ def to_h(with_meta = false)
81
+ hash = {}
82
+
83
+ shelf.each do |key, book|
84
+ book_h = book.to_h
85
+
86
+ unless with_meta
87
+ book_h.delete(:title)
88
+ book_h.delete(:library_key)
89
+ end
90
+
91
+ missing = book_keys - book_h.keys
92
+ missing.each do |k|
93
+ book_h[k] = nil
94
+ end
95
+
96
+ hash[key] = book_h
97
+ end
98
+
99
+ hash
100
+ end
101
+
102
+
103
+
104
+
105
+ def rotate(with_meta = false)
106
+ hash = Hash[
107
+ book_keys.map do |book_k|
108
+ [book_k, hash_for(book_k)]
109
+ end
110
+ ]
111
+
112
+ if with_meta
113
+ hash[:title] = hash_for(:title)
114
+ hash[:library_key] = hash_for(:library_key)
115
+ end
116
+
117
+ hash
118
+ end
119
+
120
+
65
121
  private
66
122
 
67
123
  def define_accessor_for(book)
@@ -1,3 +1,3 @@
1
1
  module ReferenceBook
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class BookTest < Minitest::Test
4
4
 
5
- BOOK_STRUCT = ReferenceBook::Book.new("Example", :foo, :bar)
5
+ BOOK_STRUCT = ReferenceBook::Book.new("Example", :foo, :bar, :title, :library_key)
6
6
 
7
7
  def setup
8
8
  super
@@ -56,7 +56,7 @@ class BookTest < Minitest::Test
56
56
 
57
57
 
58
58
  def test_creation
59
- template = ReferenceBook::Book.new("Foobar", :foo, :bar)
59
+ template = ReferenceBook::Book.new("Foobar", :foo, :bar, :title, :library_key)
60
60
  assert_equal Class, template.class
61
61
  assert_equal "ReferenceBook::Book::Foobar", template.name
62
62
 
@@ -72,9 +72,12 @@ class BookTest < Minitest::Test
72
72
 
73
73
  def test_to_h
74
74
  assert_equal Hash, @book.to_h.class
75
- assert_equal 2, @book.to_h.size
75
+ assert_equal 4, @book.to_h.size
76
76
  assert_includes @book.to_h.keys, :foo
77
77
  assert_includes @book.to_h.keys, :bar
78
+ assert_includes @book.to_h.keys, :title
79
+ assert_includes @book.to_h.keys, :library_key
80
+
78
81
  assert_nil @book.to_h[:foo]
79
82
  assert_nil @book.to_h[:bar]
80
83
 
@@ -153,21 +153,7 @@ class LibraryTest < Minitest::Test
153
153
 
154
154
 
155
155
  def test_pluck
156
- ReferenceBook.write_book(title: "TestPluckA") do |b|
157
- b.foo = 11
158
- b.bar = 22
159
- end
160
-
161
- ReferenceBook.write_book(title: "TestPluckB") do |b|
162
- b.foo = 111
163
- b.bar = 222
164
- b.baz = 333
165
- end
166
-
167
- ReferenceBook.write_book(title: "TestPluckC") do |b|
168
- b.foo = 1111
169
- b.bar = 2222
170
- end
156
+ end_to_end_setup("TestPluck")
171
157
 
172
158
  result = ReferenceBook.library.pluck(:foo)
173
159
  assert_instance_of Array, result
@@ -188,21 +174,7 @@ class LibraryTest < Minitest::Test
188
174
 
189
175
 
190
176
  def test_hash_pluck
191
- ReferenceBook.write_book(title: "TestHashPluckA", library_key: :aaa) do |b|
192
- b.foo = 11
193
- b.bar = 22
194
- end
195
-
196
- ReferenceBook.write_book(title: "TestHashPluckB", library_key: :bbb) do |b|
197
- b.foo = 111
198
- b.bar = 222
199
- b.baz = 333
200
- end
201
-
202
- ReferenceBook.write_book(title: "TestHashPluckC", library_key: :ccc) do |b|
203
- b.foo = 1111
204
- b.bar = 2222
205
- end
177
+ end_to_end_setup("TestHashPluck")
206
178
 
207
179
  result = ReferenceBook.library.hash_pluck(:foo)
208
180
  assert_instance_of Hash, result
@@ -213,7 +185,6 @@ class LibraryTest < Minitest::Test
213
185
 
214
186
  assert_equal result, ReferenceBook.library.hash_for(:foo)
215
187
 
216
-
217
188
  result = ReferenceBook.library.hash_pluck(:baz)
218
189
  assert_instance_of Hash, result
219
190
  assert_equal 3, result.size
@@ -225,6 +196,68 @@ class LibraryTest < Minitest::Test
225
196
  end
226
197
 
227
198
 
199
+
200
+ def test_to_h_no_meta
201
+ end_to_end_setup("TestTohNoMeta")
202
+
203
+ result = ReferenceBook.library.to_h
204
+
205
+ assert_instance_of Hash, result
206
+ assert_equal 3, result.size
207
+
208
+ assert_equal({foo: 11, bar: 22, baz: nil}, result[:aaa])
209
+ assert_equal({foo: 111, bar: 222, baz: 333}, result[:bbb])
210
+ assert_equal({foo: 1111, bar: 2222, baz: nil}, result[:ccc])
211
+ end
212
+
213
+
214
+ def test_to_h_with_meta
215
+ end_to_end_setup("TestToH")
216
+
217
+ result = ReferenceBook.library.to_h(true)
218
+
219
+ assert_instance_of Hash, result
220
+ assert_equal 3, result.size
221
+
222
+ assert_equal({foo: 11, bar: 22, baz: nil, title: "TestToHA", library_key: :aaa}, result[:aaa])
223
+ assert_equal({foo: 111, bar: 222, baz: 333, title: "TestToHB", library_key: :bbb}, result[:bbb])
224
+ assert_equal({foo: 1111, bar: 2222, baz: nil, title: "TestToHC", library_key: :ccc}, result[:ccc])
225
+ end
226
+
227
+
228
+
229
+ def test_rotate_no_meta
230
+ end_to_end_setup("TestRotateNoMeta")
231
+
232
+ result = ReferenceBook.library.rotate
233
+
234
+ assert_instance_of Hash, result
235
+ assert_equal 3, result.size
236
+
237
+ assert_equal({aaa: 11, bbb: 111, ccc: 1111}, result[:foo])
238
+ assert_equal({aaa: 22, bbb: 222, ccc: 2222}, result[:bar])
239
+ assert_equal({aaa: nil, bbb: 333, ccc: nil}, result[:baz])
240
+ end
241
+
242
+
243
+ def test_rotate_with_meta
244
+ end_to_end_setup("TestRotate")
245
+
246
+ result = ReferenceBook.library.rotate(true)
247
+
248
+ assert_instance_of Hash, result
249
+ assert_equal 5, result.size
250
+
251
+ assert_equal({aaa: 11, bbb: 111, ccc: 1111}, result[:foo])
252
+ assert_equal({aaa: 22, bbb: 222, ccc: 2222}, result[:bar])
253
+ assert_equal({aaa: nil, bbb: 333, ccc: nil}, result[:baz])
254
+
255
+ assert_equal({aaa: "TestRotateA", bbb: "TestRotateB", ccc: "TestRotateC"}, result[:title])
256
+ assert_equal({aaa: :aaa, bbb: :bbb, ccc: :ccc}, result[:library_key])
257
+ end
258
+
259
+
260
+
228
261
  private
229
262
 
230
263
  BOOK_STRUCT = ReferenceBook::Book.new("ExampleLib", :title, :library_key)
@@ -237,4 +270,24 @@ private
237
270
  end
238
271
 
239
272
 
273
+
274
+ def end_to_end_setup(base_name)
275
+ ReferenceBook.write_book(title: "#{base_name}A", library_key: :aaa) do |b|
276
+ b.foo = 11
277
+ b.bar = 22
278
+ end
279
+
280
+ ReferenceBook.write_book(title: "#{base_name}B", library_key: :bbb) do |b|
281
+ b.foo = 111
282
+ b.bar = 222
283
+ b.baz = 333
284
+ end
285
+
286
+ ReferenceBook.write_book(title: "#{base_name}C", library_key: :ccc) do |b|
287
+ b.foo = 1111
288
+ b.bar = 2222
289
+ end
290
+ end
291
+
292
+
240
293
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reference_book
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommaso Pavese