reference_book 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ class ReferenceBook::Setup::Writer
2
+
3
+
4
+ def initialize(book_spec = nil)
5
+ @book_spec = book_spec
6
+ end
7
+
8
+
9
+
10
+ def create_book_with(raw_title, raw_library_key, collector)
11
+ values = collector.to_h
12
+ verify_values!(values)
13
+
14
+ book_title, library_key = sanitize_title_and_key(raw_title, raw_library_key)
15
+
16
+ verify_library_key!(library_key)
17
+
18
+ values[:title] = book_title
19
+ values[:library_key] = library_key
20
+
21
+ write_book_with(book_title, values)
22
+ end
23
+
24
+
25
+ private
26
+
27
+
28
+ def write_book_with(title, hash)
29
+ template = define_book_struct_with(title, hash.keys)
30
+ book = template.new
31
+ hash.each_pair { |k, v| book[k] = v }
32
+ book.freeze
33
+ book
34
+ end
35
+
36
+
37
+
38
+
39
+ def define_book_struct_with(title, keys)
40
+ ReferenceBook::Book.new(title, *keys)
41
+ end
42
+
43
+
44
+
45
+ def verify_values!(values)
46
+ @book_spec.verify_keys!(values.keys) if @book_spec
47
+ end
48
+
49
+
50
+ def verify_library_key!(key)
51
+ ReferenceBook::Library.verify_library_key!(key)
52
+ end
53
+
54
+
55
+ def sanitize_title_and_key(raw_title, raw_library_key)
56
+ raw_title ||= "Default"
57
+ book_title = ReferenceBook::Inflector.constantize(raw_title)
58
+
59
+ # If we have an explicit key, we just ensure it's a symbol,
60
+ # if we don't have an explicit key, we adapt the title
61
+ if raw_library_key
62
+ library_key = raw_library_key.to_sym
63
+ else
64
+ library_key = ReferenceBook::Inflector.make_key(raw_title)
65
+ end
66
+
67
+ [book_title, library_key]
68
+ end
69
+
70
+ end
@@ -0,0 +1,2 @@
1
+ module ReferenceBook::Setup
2
+ end
@@ -0,0 +1,3 @@
1
+ module ReferenceBook
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,52 @@
1
+ module ReferenceBook; end
2
+
3
+ require 'reference_book/book'
4
+ require 'reference_book/errors'
5
+ require 'reference_book/inflector'
6
+ require 'reference_book/library'
7
+ require 'reference_book/setup'
8
+ require 'reference_book/setup/collector'
9
+ require 'reference_book/setup/locked_book_spec'
10
+ require 'reference_book/setup/writer'
11
+ require 'reference_book/version'
12
+
13
+
14
+ module ReferenceBook
15
+ class << self
16
+
17
+ attr_reader :locked_book_spec
18
+
19
+
20
+ # to lock the structure of Books, optional
21
+ #
22
+ # ReferenceBook.define_book_structure :attr_1, :attr_2, 'attr_3', ...
23
+ #
24
+ def define_book_structure(*book_keys)
25
+ @locked_book_spec = Setup::LockedBookSpec.new(book_keys)
26
+ end
27
+
28
+
29
+
30
+ # with block with 1 argument
31
+ #
32
+ # ReferenceBook.write_book(title: :sym_or_str) do |book|
33
+ # book.some_prop = "foobar"
34
+ # end
35
+ #
36
+ def write_book(opts = {}, &block)
37
+ collector = Setup::Collector.new
38
+ yield collector
39
+
40
+ writer = Setup::Writer.new(locked_book_spec)
41
+ book = writer.create_book_with(opts[:title], opts[:library_key], collector)
42
+
43
+ library.store(book)
44
+ end
45
+
46
+
47
+ def library
48
+ ReferenceBook::Library
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'reference_book/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "reference_book"
8
+ spec.version = ReferenceBook::VERSION
9
+ spec.author = "Tommaso Pavese"
10
+ spec.summary = "A multi-context configuration library and DSL."
11
+ spec.description = "A multi-context configuration library and DSL. ReferenceBook provides an easy interface to define, validate and query multi-context configuration data."
12
+ spec.homepage = "https://github.com/tompave/reference_book"
13
+ spec.license = "MIT"
14
+
15
+ spec.required_ruby_version = '>= 2.0.0'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake", '~> 10.0'
24
+
25
+ spec.add_development_dependency 'minitest', '~> 5.3'
26
+ end
data/test/book_test.rb ADDED
@@ -0,0 +1,118 @@
1
+ require 'test_helper'
2
+
3
+ class BookTest < Minitest::Test
4
+
5
+ BOOK_STRUCT = ReferenceBook::Book.new("Example", :foo, :bar)
6
+
7
+ def setup
8
+ super
9
+ @book = BOOK_STRUCT.new
10
+ end
11
+
12
+
13
+ def test_name
14
+ assert_equal "ReferenceBook::Book::Example", @book.class.name
15
+ end
16
+
17
+
18
+ def test_setters
19
+ assert_respond_to @book, :foo=
20
+ assert_respond_to @book, :bar=
21
+ refute_respond_to @book, :baz=
22
+ end
23
+
24
+
25
+ def test_getters
26
+ assert_respond_to @book, :foo
27
+ assert_respond_to @book, :bar
28
+ refute_respond_to @book, :baz
29
+ end
30
+
31
+
32
+
33
+ def test_set_with_method
34
+ @book.foo = 1234
35
+ assert_equal 1234, @book.foo
36
+ end
37
+
38
+
39
+ def test_set_with_hash_access
40
+ @book[:foo] = 1234
41
+ assert_equal 1234, @book.foo
42
+ end
43
+
44
+
45
+
46
+ def test_get_with_method
47
+ @book.foo = 9876
48
+ assert_equal 9876, @book.foo
49
+ end
50
+
51
+
52
+ def test_get_with_hash_access
53
+ @book.foo = 9876
54
+ assert_equal 9876, @book[:foo]
55
+ end
56
+
57
+
58
+ def test_creation
59
+ template = ReferenceBook::Book.new("Foobar", :foo, :bar)
60
+ assert_equal Class, template.class
61
+ assert_equal "ReferenceBook::Book::Foobar", template.name
62
+
63
+ book = template.new
64
+ assert book.is_a?(ReferenceBook::Book)
65
+ assert_equal "ReferenceBook::Book::Foobar", book.class.name
66
+
67
+ refute_equal book, @book
68
+ assert_equal book, template.new
69
+ end
70
+
71
+
72
+
73
+ def test_to_h
74
+ assert_equal Hash, @book.to_h.class
75
+ assert_equal 2, @book.to_h.size
76
+ assert_includes @book.to_h.keys, :foo
77
+ assert_includes @book.to_h.keys, :bar
78
+ assert_nil @book.to_h[:foo]
79
+ assert_nil @book.to_h[:bar]
80
+
81
+ @book.foo = 11
82
+ @book.bar = 22
83
+ assert_equal 11, @book.to_h[:foo]
84
+ assert_equal 22, @book.to_h[:bar]
85
+ end
86
+
87
+
88
+
89
+ def test_freeze_methods
90
+ assert_respond_to @book, :freeze
91
+ assert_respond_to @book, :frozen?
92
+ end
93
+
94
+
95
+ def test_freeze_effects
96
+ refute @book.frozen?
97
+ @book.freeze
98
+ assert @book.frozen?
99
+ end
100
+
101
+
102
+ def test_fronzen_book_attributes
103
+ assert_nil @book.foo
104
+ @book.foo = 1
105
+ assert_equal 1, @book.foo
106
+ @book.freeze
107
+ assert_equal 1, @book.foo
108
+
109
+ assert_raises RuntimeError do
110
+ @book.foo = 2
111
+ end
112
+
113
+ assert_raises RuntimeError do
114
+ @book.bar = 3
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+
3
+ class InflectorTest < Minitest::Test
4
+
5
+ def test_constantize_simple_symbol
6
+ result = ReferenceBook::Inflector.constantize :hello
7
+ assert_equal "Hello", result
8
+ end
9
+
10
+
11
+ def test_constantize_two_words
12
+ result = ReferenceBook::Inflector.constantize 'hello world'
13
+ assert_equal "Helloworld", result
14
+ end
15
+
16
+
17
+ def test_constantize_already_ok
18
+ result = ReferenceBook::Inflector.constantize 'HelloWorld'
19
+ assert_equal "HelloWorld", result
20
+ end
21
+
22
+
23
+ def test_constantize_other_chars
24
+ result = ReferenceBook::Inflector.constantize "hello-world!11"
25
+ assert_equal "Helloworld", result
26
+ end
27
+
28
+
29
+ def test_constantize_nil
30
+ result = ReferenceBook::Inflector.constantize nil
31
+ assert_equal nil, result
32
+ end
33
+
34
+
35
+ def test_constantize_empty
36
+ result = ReferenceBook::Inflector.constantize ''
37
+ assert_equal nil, result
38
+ end
39
+
40
+
41
+ def test_make_key_simple
42
+ result = ReferenceBook::Inflector.make_key "hello"
43
+ assert_equal :hello, result
44
+ end
45
+
46
+
47
+ def test_make_key_two_words
48
+ result = ReferenceBook::Inflector.make_key "hello world"
49
+ assert_equal :hello_world, result
50
+ end
51
+
52
+
53
+ def test_make_key_camel
54
+ result = ReferenceBook::Inflector.make_key "HelloWorldDear"
55
+ assert_equal :hello_world_dear, result
56
+ end
57
+
58
+
59
+ def test_make_key_symbol
60
+ result = ReferenceBook::Inflector.make_key :hello
61
+ assert_equal :hello, result
62
+ end
63
+
64
+
65
+ def test_make_key_upcase
66
+ result = ReferenceBook::Inflector.make_key "heLLO"
67
+ assert_equal :he_llo, result
68
+ end
69
+
70
+
71
+ def test_make_key_with_numbers
72
+ result = ReferenceBook::Inflector.make_key "hello123_world"
73
+ assert_equal :hello123_world, result
74
+ end
75
+
76
+
77
+ def test_make_key_nil
78
+ result = ReferenceBook::Inflector.make_key nil
79
+ assert_equal nil, result
80
+ end
81
+ end
@@ -0,0 +1,166 @@
1
+ require 'test_helper'
2
+
3
+ class LibraryTest < Minitest::Test
4
+
5
+
6
+ def setup
7
+ super
8
+ ReferenceBook::Library.empty!
9
+ end
10
+
11
+
12
+ def teardown
13
+ super
14
+ ReferenceBook::Library.empty!
15
+ end
16
+
17
+
18
+
19
+ def test_shelf_class
20
+ assert_equal Hash, ReferenceBook::Library.shelf.class
21
+ end
22
+
23
+
24
+ def test_index_class
25
+ assert_equal Array, ReferenceBook::Library.index.class
26
+ end
27
+
28
+
29
+
30
+ def test_with_no_book_stored
31
+ assert_empty ReferenceBook::Library.index
32
+ assert_empty ReferenceBook::Library.shelf
33
+ end
34
+
35
+
36
+ def test_store
37
+ refute_respond_to ReferenceBook::Library, :one
38
+
39
+ book = make_book(:one)
40
+ result = ReferenceBook::Library.store(book)
41
+
42
+ assert_equal book, result
43
+
44
+ refute_empty ReferenceBook::Library.index
45
+ assert_includes ReferenceBook::Library.index, :one
46
+ assert_equal 1, ReferenceBook::Library.index.size
47
+
48
+ refute_empty ReferenceBook::Library.shelf
49
+ assert_equal book, ReferenceBook::Library.shelf[:one]
50
+ assert_equal 1, ReferenceBook::Library.shelf.size
51
+
52
+ assert_respond_to ReferenceBook::Library, :one
53
+ assert_equal book, ReferenceBook::Library.one
54
+ end
55
+
56
+
57
+
58
+ def test_store_incremental
59
+ assert_empty ReferenceBook::Library.index
60
+ assert_empty ReferenceBook::Library.shelf
61
+
62
+ book_1 = make_book(:one)
63
+ book_2 = make_book(:two)
64
+
65
+ ReferenceBook::Library.store book_1
66
+
67
+ assert_equal 1, ReferenceBook::Library.index.size
68
+ assert_includes ReferenceBook::Library.index, :one
69
+
70
+ assert_equal 1, ReferenceBook::Library.shelf.size
71
+ assert_equal book_1, ReferenceBook::Library.shelf[:one]
72
+
73
+ assert_respond_to ReferenceBook::Library, :one
74
+ assert_equal book_1, ReferenceBook::Library.one
75
+
76
+
77
+ ReferenceBook::Library.store book_2
78
+
79
+ assert_equal 2, ReferenceBook::Library.index.size
80
+ assert_includes ReferenceBook::Library.index, :one
81
+ assert_includes ReferenceBook::Library.index, :two
82
+
83
+ assert_equal 2, ReferenceBook::Library.shelf.size
84
+ assert_equal book_1, ReferenceBook::Library.shelf[:one]
85
+ assert_equal book_2, ReferenceBook::Library.shelf[:two]
86
+
87
+ assert_respond_to ReferenceBook::Library, :one
88
+ assert_equal book_1, ReferenceBook::Library.one
89
+ assert_respond_to ReferenceBook::Library, :two
90
+ assert_equal book_2, ReferenceBook::Library.two
91
+ end
92
+
93
+
94
+
95
+ def test_empty
96
+ book = make_book(:one)
97
+ ReferenceBook::Library.store(book)
98
+
99
+ assert_includes ReferenceBook::Library.index, :one
100
+ assert_equal book, ReferenceBook::Library.shelf[:one]
101
+ assert_respond_to ReferenceBook::Library, :one
102
+
103
+ ReferenceBook::Library.empty!
104
+
105
+ assert_empty ReferenceBook::Library.index
106
+ assert_empty ReferenceBook::Library.shelf
107
+ refute_respond_to ReferenceBook::Library, :one
108
+ end
109
+
110
+
111
+
112
+
113
+ def test_hash_access
114
+ assert_nil ReferenceBook::Library[:one]
115
+ assert_nil ReferenceBook::Library[:two]
116
+
117
+ book = make_book(:one)
118
+ result = ReferenceBook::Library.store(book)
119
+
120
+ assert_equal book, ReferenceBook::Library[:one]
121
+ assert_nil ReferenceBook::Library[:two]
122
+ end
123
+
124
+
125
+
126
+ def test_accessor_methods
127
+ refute_respond_to ReferenceBook::Library, :one
128
+ refute_respond_to ReferenceBook::Library, :two
129
+
130
+ book = make_book(:one)
131
+ result = ReferenceBook::Library.store(book)
132
+
133
+ assert_respond_to ReferenceBook::Library, :one
134
+ assert_equal book, ReferenceBook::Library.one
135
+ refute_respond_to ReferenceBook::Library, :two
136
+ end
137
+
138
+
139
+
140
+ def test_verify_library_key
141
+ assert_nil ReferenceBook::Library.verify_library_key! :one
142
+ assert_nil ReferenceBook::Library.verify_library_key! :two
143
+
144
+ ReferenceBook::Library.store(make_book(:one))
145
+
146
+ assert_raises ReferenceBook::BookDefinitionError do
147
+ ReferenceBook::Library.verify_library_key! :one
148
+ end
149
+ assert_nil ReferenceBook::Library.verify_library_key! :two
150
+ end
151
+
152
+
153
+
154
+ private
155
+
156
+ BOOK_STRUCT = ReferenceBook::Book.new("ExampleLib", :title, :library_key)
157
+
158
+ def make_book(key)
159
+ book = BOOK_STRUCT.new
160
+ book.title = "#{key}_title"
161
+ book.library_key = key
162
+ book
163
+ end
164
+
165
+
166
+ end
@@ -0,0 +1,197 @@
1
+ require 'test_helper'
2
+
3
+ class ReferenceBookTest < Minitest::Test
4
+
5
+
6
+ def teardown
7
+ super
8
+ ReferenceBook::Library.empty!
9
+ ReferenceBook.instance_eval { @locked_book_spec = nil }
10
+ end
11
+
12
+
13
+
14
+ def test_define_book_structure
15
+ assert_nil ReferenceBook.locked_book_spec
16
+ spec = ReferenceBook.define_book_structure :one, :two
17
+
18
+ assert spec.is_a?(ReferenceBook::Setup::LockedBookSpec)
19
+ assert_equal spec, ReferenceBook.locked_book_spec
20
+ end
21
+
22
+
23
+ def test_define_book_structure_fail
24
+ assert_nil ReferenceBook.locked_book_spec
25
+
26
+ assert_raises ReferenceBook::LockedBookSpecError do
27
+ ReferenceBook.define_book_structure
28
+ end
29
+ end
30
+
31
+
32
+
33
+
34
+
35
+ def test_write_book_normal
36
+ assert_empty ReferenceBook::Library.index
37
+ assert_empty ReferenceBook::Library.shelf
38
+ refute_respond_to ReferenceBook::Library, :ref_book
39
+
40
+ book = ReferenceBook.write_book(title: "RefBook") do |b|
41
+ b.foo = 111
42
+ b.bar = 222
43
+ end
44
+
45
+ assert book.is_a?(ReferenceBook::Book)
46
+ assert_equal "ReferenceBook::Book::RefBook", book.class.name
47
+ assert book.frozen?
48
+ assert_equal "RefBook", book.title
49
+ assert_equal :ref_book, book.library_key
50
+
51
+ assert_equal 111, book.foo
52
+ assert_equal 222, book.bar
53
+
54
+ assert_equal 1, ReferenceBook::Library.index.size
55
+ assert_equal 1, ReferenceBook::Library.shelf.size
56
+
57
+ assert_includes ReferenceBook::Library.index, :ref_book
58
+ assert_respond_to ReferenceBook::Library, :ref_book
59
+ assert_equal book, ReferenceBook::Library.ref_book
60
+
61
+ assert_equal book, ReferenceBook::Library.shelf[:ref_book]
62
+ end
63
+
64
+
65
+
66
+
67
+ def test_write_more_than_one_book
68
+ assert_empty ReferenceBook::Library.index
69
+ assert_empty ReferenceBook::Library.shelf
70
+
71
+ ReferenceBook.write_book(title: "RefBookA") do |b|
72
+ b.foo = 111
73
+ b.bar = 222
74
+ end
75
+
76
+ assert_equal 1, ReferenceBook::Library.index.size
77
+ assert_equal 1, ReferenceBook::Library.shelf.size
78
+
79
+ ReferenceBook.write_book(title: "RefBookB") do |b|
80
+ b.foo = 111
81
+ b.bar = 222
82
+ end
83
+
84
+ assert_equal 2, ReferenceBook::Library.index.size
85
+ assert_equal 2, ReferenceBook::Library.shelf.size
86
+ end
87
+
88
+
89
+
90
+
91
+
92
+ def test_attributes_of_created_book
93
+ book = ReferenceBook.write_book(title: "RefBookC") do |b|
94
+ b.foo = 111
95
+ b.bar = 222
96
+ b.baz = 333
97
+ end
98
+
99
+ assert_equal 111, book.foo
100
+ assert_equal 222, book.bar
101
+ assert_equal 333, book.baz
102
+ end
103
+
104
+
105
+
106
+
107
+ def test_write_book_duplicated_key
108
+ book = ReferenceBook.write_book(title: "Hello", library_key: :existing) do |b|
109
+ b.foo = 'hello'
110
+ end
111
+
112
+ assert book.is_a?(ReferenceBook::Book)
113
+ assert_equal 1, ReferenceBook::Library.index.size
114
+
115
+ assert_raises ReferenceBook::BookDefinitionError do
116
+ ReferenceBook.write_book(title: "HelloA", library_key: :existing) do |b|
117
+ b.foo = 'hello'
118
+ end
119
+ end
120
+
121
+
122
+ book = ReferenceBook.write_book(title: "HelloB", library_key: :another) do |b|
123
+ b.foo = 'hello'
124
+ end
125
+
126
+ assert book.is_a?(ReferenceBook::Book)
127
+ assert_equal 2, ReferenceBook::Library.index.size
128
+ end
129
+
130
+
131
+
132
+ def test_create_book_with_spec_pass
133
+ ReferenceBook.define_book_structure :one, :two, :three
134
+
135
+ book = ReferenceBook.write_book(title: "aaaa") do |b|
136
+ b.one = 1
137
+ b.two = 2
138
+ b.three = 3
139
+ end
140
+
141
+ assert book.is_a?(ReferenceBook::Book)
142
+ assert_equal 1, ReferenceBook::Library.index.size
143
+ end
144
+
145
+
146
+
147
+ def test_create_book_with_spec_fail_missing
148
+ ReferenceBook.define_book_structure :one, :two, :three
149
+
150
+ assert_raises ReferenceBook::BookDefinitionError do
151
+ ReferenceBook.write_book(title: "bbbb") do |b|
152
+ b.one = 1
153
+ b.two = 2
154
+ end
155
+ end
156
+ end
157
+
158
+
159
+ def test_create_book_with_spec_fail_unexpected
160
+ ReferenceBook.define_book_structure :one, :two, :three
161
+
162
+ assert_raises ReferenceBook::BookDefinitionError do
163
+ ReferenceBook.write_book(title: "cccc") do |b|
164
+ b.one = 1
165
+ b.two = 2
166
+ b.three = 3
167
+ b.four = 4
168
+ end
169
+ end
170
+ end
171
+
172
+
173
+ def test_create_book_with_spec_fail_missing_and_unexpected
174
+ ReferenceBook.define_book_structure :one, :two, :three
175
+
176
+ assert_raises ReferenceBook::BookDefinitionError do
177
+ ReferenceBook.write_book(title: "dddd") do |b|
178
+ b.one = 1
179
+ b.four = 4
180
+ end
181
+ end
182
+ end
183
+
184
+
185
+
186
+
187
+ def test_library
188
+ assert_equal ReferenceBook::Library, ReferenceBook.library
189
+ end
190
+
191
+
192
+
193
+ def test_locked_book_spec
194
+ assert_respond_to ReferenceBook, :locked_book_spec
195
+ end
196
+
197
+ end