hashema 0.1.1 → 0.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 663a42c26082914a797a078f3c45be35d9819726
4
+ data.tar.gz: 94a947af82292a51ccac8bee2034309275e23e61
5
+ SHA512:
6
+ metadata.gz: 963f4928cf2664b10735f72c094f72eec7a12af292ff3b96d94b8c2f657ee2f818bb6be469521b54d173e7df98006c1402ef6975a521c2ed575bed682c2360f8
7
+ data.tar.gz: 1c30aaec10287aae3b1234297f312f8081249522e3ab8b9b630160565c1433f09c019e2af0e9eba2bb1cec6b4e7c6660fb6f01555e85e7f7b82f09280076e2bc
data/README.md CHANGED
@@ -111,6 +111,13 @@ expect(
111
111
  )
112
112
  ```
113
113
 
114
+ ### Making a hash key optional
115
+
116
+ ```ruby
117
+ expect({entree: "eggs"})
118
+ .to conform_to_schema({entree: String, side: Hashema::Optional(String)})
119
+ ```
120
+
114
121
  ## RSpec matcher options
115
122
 
116
123
  ### with_indifferent_access
@@ -3,3 +3,4 @@ require 'hashema/version'
3
3
  require 'hashema/conform_to_schema'
4
4
  require 'hashema/compiler'
5
5
  require 'hashema/schema'
6
+ require 'hashema/optional'
@@ -12,6 +12,8 @@ module Hashema
12
12
 
13
13
  def compile(thing)
14
14
  case thing
15
+ when Hashema::Optional
16
+ Hashema::OptionalValueInHash.new(compile(thing.expected))
15
17
  when ::Array
16
18
  if thing.size == 1
17
19
  Hashema::Array.new(compile(thing[0]))
@@ -0,0 +1,9 @@
1
+ require_relative 'schema'
2
+
3
+ module Hashema
4
+ Optional = Struct.new(:expected)
5
+
6
+ def self.Optional(value)
7
+ Optional.new(value)
8
+ end
9
+ end
@@ -1,3 +1,5 @@
1
+ require "set"
2
+
1
3
  module Hashema
2
4
  class Schema < Struct.new(:expected)
3
5
  # A Schema is a Comparison factory.
@@ -5,6 +7,10 @@ module Hashema
5
7
  self.class.const_get('Comparison').new(actual, expected)
6
8
  end
7
9
 
10
+ def required_if_map_value?
11
+ true
12
+ end
13
+
8
14
  def inspect
9
15
  expected.inspect
10
16
  end
@@ -147,7 +153,7 @@ module Hashema
147
153
  end
148
154
 
149
155
  def missing_keys
150
- @missing_keys ||= expected_keys - actual_keys
156
+ @missing_keys ||= demanded_keys - actual_keys
151
157
  end
152
158
 
153
159
  def matching_keys
@@ -162,6 +168,12 @@ module Hashema
162
168
  @expected_keys ||= Set.new(expected.keys)
163
169
  end
164
170
 
171
+ def demanded_keys
172
+ @demanded_keys ||= Set.new(expected_keys.select { |key|
173
+ fetch(key, expected).required_if_map_value?
174
+ })
175
+ end
176
+
165
177
  def actual_keys
166
178
  @actual_keys ||= Set.new(actual.keys)
167
179
  end
@@ -198,15 +210,13 @@ module Hashema
198
210
 
199
211
  def extra_keys
200
212
  @extra_keys ||= actual.keys.reject do |key|
201
- expected.has_key? symbol_to_string key or
202
- expected.has_key? string_to_symbol key
213
+ has_key?(key, expected)
203
214
  end
204
215
  end
205
216
 
206
217
  def missing_keys
207
- @missing_keys ||= expected.keys.reject do |key|
208
- actual.has_key? symbol_to_string key or
209
- actual.has_key? string_to_symbol key
218
+ @missing_keys ||= expected.keys.select do |key|
219
+ !has_key?(key, actual) and fetch(key, expected).required_if_map_value?
210
220
  end
211
221
  end
212
222
 
@@ -216,6 +226,10 @@ module Hashema
216
226
  Set.new(actual.keys.map(&method(:symbol_to_string)))
217
227
  end
218
228
 
229
+ def has_key?(key, hash)
230
+ hash.has_key?(symbol_to_string key) or hash.has_key?(string_to_symbol key)
231
+ end
232
+
219
233
  def fetch(key, hash)
220
234
  return hash[symbol_to_string key] if hash.has_key? symbol_to_string key
221
235
  return hash[string_to_symbol key] if hash.has_key? string_to_symbol key
@@ -239,6 +253,21 @@ module Hashema
239
253
  end
240
254
  end
241
255
 
256
+ class OptionalValueInHash < Schema
257
+ def required_if_map_value?
258
+ false
259
+ end
260
+
261
+ class Comparison < Hashema::Comparison
262
+
263
+ private
264
+
265
+ def find_mismatches
266
+ expected.compare(actual).mismatches
267
+ end
268
+ end
269
+ end
270
+
242
271
  class Mismatch < Struct.new(:actual, :expected, :location, :verb)
243
272
  def self.at(location, original)
244
273
  new original.actual,
@@ -4,8 +4,7 @@ module Hashema
4
4
  class Validator
5
5
  def initialize(actual, schema, options={})
6
6
  @actual = actual
7
- @schema = schema
8
- @schema = compile schema, options
7
+ @schema = Compiler.compile schema, options
9
8
  end
10
9
 
11
10
  def valid?
@@ -18,10 +17,6 @@ module Hashema
18
17
 
19
18
  private
20
19
 
21
- def compile(schema, options={})
22
- Hashema::Compiler.compile(schema, options)
23
- end
24
-
25
20
  def comparison
26
21
  @comparison ||= @schema.compare(@actual)
27
22
  end
@@ -1,5 +1,5 @@
1
1
  module Hashema
2
2
  module Version
3
- STRING = '0.1.1'
3
+ STRING = '0.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,30 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ben Christel
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-05-04 00:00:00.000000000 Z
11
+ date: 2020-03-26 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 2.0.0
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 2.0.0
30
27
  description: Assert that JSONable objects conform to a schema
@@ -34,38 +31,38 @@ extensions: []
34
31
  extra_rdoc_files:
35
32
  - README.md
36
33
  files:
34
+ - License.txt
35
+ - README.md
37
36
  - lib/hashema.rb
38
37
  - lib/hashema/compiler.rb
39
38
  - lib/hashema/conform_to_schema.rb
39
+ - lib/hashema/optional.rb
40
40
  - lib/hashema/schema.rb
41
41
  - lib/hashema/validator.rb
42
42
  - lib/hashema/version.rb
43
- - License.txt
44
- - README.md
45
43
  homepage: http://github.com/benchristel/hashema
46
44
  licenses:
47
45
  - MIT
46
+ metadata: {}
48
47
  post_install_message:
49
48
  rdoc_options:
50
- - --charset=UTF-8
49
+ - "--charset=UTF-8"
51
50
  require_paths:
52
51
  - lib
53
52
  required_ruby_version: !ruby/object:Gem::Requirement
54
- none: false
55
53
  requirements:
56
- - - ! '>='
54
+ - - ">="
57
55
  - !ruby/object:Gem::Version
58
56
  version: '0'
59
57
  required_rubygems_version: !ruby/object:Gem::Requirement
60
- none: false
61
58
  requirements:
62
- - - ! '>='
59
+ - - ">="
63
60
  - !ruby/object:Gem::Version
64
61
  version: '0'
65
62
  requirements: []
66
63
  rubyforge_project:
67
- rubygems_version: 1.8.23
64
+ rubygems_version: 2.5.2.3
68
65
  signing_key:
69
- specification_version: 3
70
- summary: hashema-0.1.1
66
+ specification_version: 4
67
+ summary: hashema-0.2.0
71
68
  test_files: []