hashape 1.0.0 → 2.0.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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -3
  3. data/README.md +67 -1
  4. data/hashape.gemspec +1 -0
  5. data/lib/hashape.rb +22 -11
  6. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b37fe671e0f57877e73dd909a3cb44d102c2756dfffe18256dca966c606f7747
4
- data.tar.gz: bd68bd8c108a93c2f31547cb54bda91e9828c88a94c04ed9abf650e5ee90806e
3
+ metadata.gz: e35dfd6e8729b87cdfa05e269c7f49a91e30226164cd9389f01af772065d0d06
4
+ data.tar.gz: b28498ce41057dd4c993798438c90b5e98e67ab7619e0f4da2d29da279c8d837
5
5
  SHA512:
6
- metadata.gz: be6614d94f50efeaa8616f002728da224f64d6c2ec8a77f906fd8a5a1bd3a9ceb9b0330c129a9291095fb8ef63cae55a08147b94750235552252e6b9f04f82de
7
- data.tar.gz: c0f58ee27cfce42b168a93c68a614059dd0971f952cabb66117eb3d14e5ddcc341056d2ccf1a3c472b9b268d5af71a5fed090405db1650b0be008e9a24020e9c
6
+ metadata.gz: 3768ff18d9b09f28904b3a7567d5b8b0fb6c673afff7519446a43be0e70e588e76134ab20533416c3301c29046b6aef2d6f4428c1ce8e5bfd2a355c24a522888
7
+ data.tar.gz: 269ebb1c077e3fef044402c7cac58af93c2d3b73ed1218920350370c882f06973b20891dd569019a156cc92c5e8a8a1508a5db29b1eb04316d1cde5e884a69c7
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in hashape.gemspec
4
- gemspec
5
-
6
- gem "simplecov", "~> 0.16.1"
4
+ gemspec
data/README.md CHANGED
@@ -22,7 +22,73 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ First, you can define your shape using `Hashape.shape`, which returns an
26
+ instance of `Hashape::Shape`.
27
+ Each key must be given a specifier, which can be one of the following:
28
+
29
+ - A type
30
+ - Simple values (e.g. strings, booleans, numbers)
31
+ - Another hash
32
+ - Regular expressions
33
+ - An instance of any class in `Hashape::Specifiers`
34
+
35
+ (Fundamentally, comparisons are done using `===` with your specifier on the left
36
+ hand side, so anything which supports this will work.)
37
+
38
+ You can then check that a given hash matches this shape using either
39
+ `Shape#matches?`, which returns a boolean, or `Shape#matches!`, which raises an
40
+ exception if the shape does not match.
41
+
42
+ Here's an example of checking that a hash has a `:name` key which is a string,
43
+ and a `:success` key which must be true:
44
+
45
+ ```
46
+ require 'hashape'
47
+ include Hashape
48
+
49
+ shape = Shape.new({
50
+ name: String,
51
+ success: true
52
+ })
53
+
54
+ p shape.matches?({
55
+ name: 'Aaron',
56
+ success: true
57
+ }) #=> true
58
+
59
+ p shape.matches?({
60
+ name: 'Aaron',
61
+ success: false
62
+ }) #=> false
63
+
64
+ shape.matches!({
65
+ name: 3,
66
+ success: true
67
+ }) #=> ShapeMatchError (key name with value 3 does not match spec String)
68
+ ```
69
+
70
+ Extra specifiers, which are within the `Hashape::Specifiers` module, give you
71
+ a bit more flexibility. They wrap another specifier (or an array of them) to
72
+ alter their behaviour. (Note that you can nest specifiers.) For example:
73
+
74
+ ```
75
+ require 'hashape'
76
+ include Hashape
77
+ include Hashape::Specifiers
78
+
79
+ # Note: for all specifiers, Name[...] is a shortcut for Name.new(...).
80
+
81
+ shape = Shape.new({
82
+ data: Optional[String]
83
+ })
84
+ shape.matches?({ data: "Hello!" }) #=> true
85
+ shape.matches?({ }) #=> true
86
+ shape.matches?({ data: 3 }) #=> false
87
+
88
+ shape = Shape.new({
89
+ data: OneOf[[String, Integer]]
90
+ })
91
+ ```
26
92
 
27
93
  ## License
28
94
 
data/hashape.gemspec CHANGED
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 2.0"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
27
  spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "simplecov", "~> 0.16.1"
28
29
  end
data/lib/hashape.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  module Hashape
5
5
  ##
6
6
  # The library version.
7
- VERSION = '1.0.0'
7
+ VERSION = '2.0.0'
8
8
 
9
9
  ##
10
10
  # Specifiers which can provide additional typing to shapes.
@@ -19,7 +19,7 @@ module Hashape
19
19
  # @param [*] spec The type spec for this specifier to match against.
20
20
  # @return [Specifier] A new specifier.
21
21
  def self.[](spec)
22
- Specifier.new(spec)
22
+ self.new(spec)
23
23
  end
24
24
 
25
25
  ##
@@ -59,6 +59,11 @@ module Hashape
59
59
  end
60
60
  end
61
61
 
62
+ ##
63
+ # Raised when Shape#matches! fails.
64
+ class ShapeMatchError < StandardError
65
+ end
66
+
62
67
  ##
63
68
  # A template hash representing how a hash should be structured, allowing
64
69
  # other hashes to be validated against it.
@@ -80,21 +85,27 @@ module Hashape
80
85
  # @return [TrueClass|FalseClass] A boolean indicating whether the subject
81
86
  # hash matches the template hash.
82
87
  def matches?(subject)
83
- subject.all? do |k, v|
84
- if v.is_a?(Hash) && shape[k].is_a?(Hash)
85
- Shape.new(shape[k]).matches?(v)
86
- else
87
- shape[k] === v
88
- end
89
- end
88
+ matches!(subject)
89
+ true
90
+ rescue ShapeMatchError
91
+ false
90
92
  end
91
93
 
92
94
  ##
93
95
  # Calls #matches? and raises a RuntimeError if it does not return true.
94
96
  # @param [Hash] subject The hash to compare the template hash against.
95
97
  def matches!(subject)
96
- raise unless matches?(subject)
97
- nil
98
+ shape.each do |k, spec|
99
+ v = subject[k]
100
+ if v.is_a?(Hash) && spec.is_a?(Hash)
101
+ Shape.new(spec).matches!(v)
102
+ else
103
+ unless spec === v
104
+ raise ShapeMatchError,
105
+ "key #{k} with value #{v} does not match spec #{spec}" \
106
+ end
107
+ end
108
+ end
98
109
  end
99
110
  end
100
111
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashape
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Christiansen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-16 00:00:00.000000000 Z
11
+ date: 2019-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.16.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.16.1
55
69
  description:
56
70
  email:
57
71
  - aaronc20000@gmail.com