reference_book 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/examples/interface_and_use.rb +15 -3
- data/lib/reference_book/library.rb +20 -0
- data/lib/reference_book/version.rb +1 -1
- data/test/library_test.rb +74 -0
- metadata +22 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4b6a7b22b35c94188e41157711d44416a1d61b0
|
4
|
+
data.tar.gz: 952b181d58f07a5b749df7d5c4b2246bec84d1b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc53552201efb3701cd427a90b90030438505c62f420d9f1e5531fe279d0b5b3ab16e042cd6a0d501e58de93e161ca4ab3c6ce34477ccdb25aeafe494719ef77
|
7
|
+
data.tar.gz: 54d77729f7193df83f9b207b847881b176edcee6d893b5bd47641b991e5e03268164347cecd8ad5988a2bd67588241f9fbddd8e90e1aed9af2f10aea9b71e0c9
|
data/README.md
CHANGED
@@ -34,6 +34,7 @@ Lots are available [in the repo](https://github.com/tompave/reference_book/tree/
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
+
|
37
38
|
## Rubies
|
38
39
|
|
39
40
|
**ReferenceBook** requires a Ruby patch level `>= 2.0`.
|
@@ -55,6 +56,8 @@ It doesn't use any _2-only_ syntax features, but it does rely on some core class
|
|
55
56
|
|
56
57
|
* add support for different libraries, each with an enforced book structure
|
57
58
|
* make it work with Ruby 1.9.3 (involves implementing methods that at the moment are provided by core classes).
|
59
|
+
* memoize results of `pluck` and `hash_pluck`
|
60
|
+
* implement `library_key` aliases, so that books can be retrieved with strings. Library keys have to be symbols, because they are used to define the accessor methods
|
58
61
|
|
59
62
|
|
60
63
|
## Installation
|
@@ -58,15 +58,27 @@ ReferenceBook.library.elf.dexterity
|
|
58
58
|
ReferenceBook.library.elf[:dexterity]
|
59
59
|
# => 2
|
60
60
|
|
61
|
-
ReferenceBook.library.dwarf.
|
62
|
-
ReferenceBook.library.dwarf[:
|
63
|
-
# =>
|
61
|
+
ReferenceBook.library.dwarf.charisma
|
62
|
+
ReferenceBook.library.dwarf[:charisma]
|
63
|
+
# => -2
|
64
64
|
|
65
65
|
ReferenceBook.library[:halfling].widsom
|
66
66
|
ReferenceBook.library[:halfling][:wisdom]
|
67
67
|
# => 0
|
68
68
|
|
69
69
|
|
70
|
+
# The Library supports horizontal queries
|
71
|
+
|
72
|
+
ReferenceBook.library.pluck :constitution
|
73
|
+
ReferenceBook.library.array_for :constitution
|
74
|
+
# => [-2, 2, 0]
|
75
|
+
|
76
|
+
|
77
|
+
ReferenceBook.library.hash_pluck :constitution
|
78
|
+
ReferenceBook.library.hash_for :constitution
|
79
|
+
# => {:elf => -2, :dwarf => 2, :halfling => 0}
|
80
|
+
|
81
|
+
|
70
82
|
|
71
83
|
|
72
84
|
# You can wrap it with your own logic:
|
@@ -42,6 +42,26 @@ module ReferenceBook::Library
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
+
|
46
|
+
def array_for(attribute)
|
47
|
+
shelf.map do |key, book|
|
48
|
+
book[attribute] if book.respond_to?(attribute)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
alias_method :pluck, :array_for
|
52
|
+
|
53
|
+
|
54
|
+
def hash_for(attribute)
|
55
|
+
Hash[
|
56
|
+
shelf.map do |key, book|
|
57
|
+
[key, (book[attribute] if book.respond_to?(attribute))]
|
58
|
+
end
|
59
|
+
]
|
60
|
+
end
|
61
|
+
alias_method :hash_pluck, :hash_for
|
62
|
+
|
63
|
+
|
64
|
+
|
45
65
|
private
|
46
66
|
|
47
67
|
def define_accessor_for(book)
|
data/test/library_test.rb
CHANGED
@@ -151,6 +151,80 @@ class LibraryTest < Minitest::Test
|
|
151
151
|
|
152
152
|
|
153
153
|
|
154
|
+
|
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
|
171
|
+
|
172
|
+
result = ReferenceBook.library.pluck(:foo)
|
173
|
+
assert_instance_of Array, result
|
174
|
+
assert_equal 3, result.size
|
175
|
+
assert_equal [11, 111, 1111], result
|
176
|
+
|
177
|
+
assert_equal result, ReferenceBook.library.array_for(:foo)
|
178
|
+
|
179
|
+
result = ReferenceBook.library.pluck(:baz)
|
180
|
+
assert_instance_of Array, result
|
181
|
+
assert_equal 3, result.size
|
182
|
+
assert_equal [nil, 333, nil], result
|
183
|
+
|
184
|
+
assert_equal result, ReferenceBook.library.array_for(:baz)
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
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
|
206
|
+
|
207
|
+
result = ReferenceBook.library.hash_pluck(:foo)
|
208
|
+
assert_instance_of Hash, result
|
209
|
+
assert_equal 3, result.size
|
210
|
+
assert_equal 11, result[:aaa]
|
211
|
+
assert_equal 111, result[:bbb]
|
212
|
+
assert_equal 1111, result[:ccc]
|
213
|
+
|
214
|
+
assert_equal result, ReferenceBook.library.hash_for(:foo)
|
215
|
+
|
216
|
+
|
217
|
+
result = ReferenceBook.library.hash_pluck(:baz)
|
218
|
+
assert_instance_of Hash, result
|
219
|
+
assert_equal 3, result.size
|
220
|
+
assert_nil result[:aaa]
|
221
|
+
assert_equal 333, result[:bbb]
|
222
|
+
assert_nil result[:ccc]
|
223
|
+
|
224
|
+
assert_equal result, ReferenceBook.library.hash_for(:baz)
|
225
|
+
end
|
226
|
+
|
227
|
+
|
154
228
|
private
|
155
229
|
|
156
230
|
BOOK_STRUCT = ReferenceBook::Book.new("ExampleLib", :title, :library_key)
|
metadata
CHANGED
@@ -1,59 +1,60 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reference_book
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tommaso Pavese
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
|
-
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
|
-
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
23
|
requirements:
|
22
24
|
- - ~>
|
23
25
|
- !ruby/object:Gem::Version
|
24
26
|
version: '1.6'
|
25
|
-
prerelease: false
|
26
|
-
type: :development
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
|
-
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
|
-
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
35
37
|
requirements:
|
36
38
|
- - ~>
|
37
39
|
- !ruby/object:Gem::Version
|
38
40
|
version: '10.0'
|
39
|
-
prerelease: false
|
40
|
-
type: :development
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
|
-
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '5.3'
|
48
|
-
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
51
|
requirements:
|
50
52
|
- - ~>
|
51
53
|
- !ruby/object:Gem::Version
|
52
54
|
version: '5.3'
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
email:
|
55
|
+
description: A multi-context configuration library and DSL. ReferenceBook provides
|
56
|
+
an easy interface to define, validate and query multi-context configuration data.
|
57
|
+
email:
|
57
58
|
executables: []
|
58
59
|
extensions: []
|
59
60
|
extra_rdoc_files: []
|
@@ -90,7 +91,7 @@ homepage: https://github.com/tompave/reference_book
|
|
90
91
|
licenses:
|
91
92
|
- MIT
|
92
93
|
metadata: {}
|
93
|
-
post_install_message:
|
94
|
+
post_install_message:
|
94
95
|
rdoc_options: []
|
95
96
|
require_paths:
|
96
97
|
- lib
|
@@ -105,9 +106,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
106
|
- !ruby/object:Gem::Version
|
106
107
|
version: '0'
|
107
108
|
requirements: []
|
108
|
-
rubyforge_project:
|
109
|
+
rubyforge_project:
|
109
110
|
rubygems_version: 2.2.2
|
110
|
-
signing_key:
|
111
|
+
signing_key:
|
111
112
|
specification_version: 4
|
112
113
|
summary: A multi-context configuration library and DSL.
|
113
114
|
test_files:
|
@@ -119,3 +120,4 @@ test_files:
|
|
119
120
|
- test/setup/locked_book_spec_test.rb
|
120
121
|
- test/setup/writer_test.rb
|
121
122
|
- test/test_helper.rb
|
123
|
+
has_rdoc:
|