check_please 0.4.1 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ module CheckPlease
2
+
3
+ class PathSegment
4
+ include CheckPlease::Reification
5
+ can_reify String, Symbol, Numeric, nil
6
+
7
+ KEY_EXPR = %r{
8
+ ^
9
+ \: # a literal colon
10
+ ( # capture key
11
+ [^\:]+ # followed by one or more things that aren't colons
12
+ ) # end capture key
13
+ $
14
+ }x
15
+
16
+ KEY_VAL_EXPR = %r{
17
+ ^
18
+ ( # capture key
19
+ [^=]+ # stuff (just not an equal sign)
20
+ ) # end capture key
21
+ \= # an equal sign
22
+ ( # capture key value
23
+ [^=]+ # stuff (just not an equal sign)
24
+ ) # end capture key value
25
+ $
26
+ }x
27
+
28
+ attr_reader :name, :key, :key_value
29
+ alias_method :to_s, :name
30
+
31
+ def initialize(name = nil)
32
+ @name = name.to_s.strip
33
+
34
+ case @name
35
+ when "", /\s/ # blank or has any whitespace
36
+ raise InvalidPathSegment, "#{name.inspect} is not a valid #{self.class} name"
37
+ end
38
+
39
+ parse_key_and_value
40
+ freeze
41
+ end
42
+
43
+ def key_expr?
44
+ name.match?(KEY_EXPR)
45
+ end
46
+
47
+ def key_val_expr?
48
+ name.match?(KEY_VAL_EXPR)
49
+ end
50
+
51
+ def match?(other_segment_or_string)
52
+ other = reify(other_segment_or_string)
53
+ PathSegmentMatcher.call(self, other)
54
+ end
55
+
56
+ def splat?
57
+ name == '*'
58
+ end
59
+
60
+ private
61
+
62
+ def parse_key_and_value
63
+ case name
64
+ when KEY_EXPR
65
+ @key = $1
66
+ when KEY_VAL_EXPR
67
+ @key, @key_value = $1, $2
68
+ else
69
+ # :nothingtodohere:
70
+ end
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,44 @@
1
+ module CheckPlease
2
+
3
+ class PathSegmentMatcher
4
+ def self.call(a,b)
5
+ new(a,b).call
6
+ end
7
+
8
+ attr_reader :a, :b, :types
9
+ def initialize(a, b)
10
+ @a, @b = a, b
11
+ @types = [ _type(a), _type(b) ].sort
12
+ end
13
+
14
+ def call
15
+ return true if either?(:splat)
16
+ return a.name == b.name if both?(:plain)
17
+ return a.key == b.key if key_and_key_value?
18
+
19
+ false
20
+ end
21
+
22
+ private
23
+
24
+ def _type(x)
25
+ return :splat if x.splat?
26
+ return :key if x.key_expr?
27
+ return :key_value if x.key_val_expr?
28
+ :plain
29
+ end
30
+
31
+ def both?(type)
32
+ types.uniq == [type]
33
+ end
34
+
35
+ def either?(type)
36
+ types.include?(type)
37
+ end
38
+
39
+ def key_and_key_value?
40
+ types == [ :key, :key_value ]
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,50 @@
1
+ module CheckPlease
2
+
3
+ module Reification
4
+ def self.included(receiver)
5
+ receiver.extend ClassMethods
6
+ receiver.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def reifiable
11
+ @_reifiable ||= []
12
+ end
13
+
14
+ def can_reify(*klasses)
15
+ klasses.flatten!
16
+
17
+ unless ( klasses - [nil] ).all? { |e| e.is_a?(Class) }
18
+ raise ArgumentError, "classes (or nil) only, please"
19
+ end
20
+
21
+ reifiable.concat klasses
22
+ reifiable.uniq!
23
+ nil
24
+ end
25
+
26
+ def reify(primitive_or_object)
27
+ case primitive_or_object
28
+ when self ; return primitive_or_object
29
+ when Array ; return primitive_or_object.map { |e| reify(e) }
30
+ when *reifiable ; return new(primitive_or_object)
31
+ end
32
+ # note early return ^^^
33
+
34
+ # that didn't work? complain!
35
+ acceptable = reifiable.map { |e| Class === e ? e.name : e.inspect }
36
+ raise ArgumentError, <<~EOF
37
+ #{self}.reify was given: #{primitive_or_object.inspect}
38
+ but only accepts: #{acceptable.join(", ")}
39
+ EOF
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ def reify(x)
45
+ self.class.reify(x)
46
+ end
47
+ end
48
+ end
49
+
50
+ end
@@ -1,5 +1,5 @@
1
1
  module CheckPlease
2
2
  # NOTE: 'check_please_rspec_matcher' depends on this,
3
3
  # so try to keep them roughly in sync
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.4" # about to release? rerun `bundle lock` to update Gemfile.lock
5
5
  end
data/usage_examples.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'check_please'
2
2
 
3
- reference = { foo: "wibble" }
4
- candidate = { bar: "wibble" }
3
+ reference = { "foo" => "wibble" }
4
+ candidate = { "bar" => "wibble" }
5
5
 
6
6
 
7
7
 
@@ -28,3 +28,9 @@ diffs = CheckPlease.diff(reference, candidate)
28
28
  #
29
29
  # If you come up with a useful way to present these, feel free to submit a PR
30
30
  # with a new class in `lib/check_please/printers` !
31
+
32
+ # To print these in the console, you can just do:
33
+ puts diffs
34
+
35
+ # If for some reason you want to print the JSON version, it gets a little more verbose:
36
+ puts diffs.to_s(format: :json)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: check_please
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Livingston-Gray
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-30 00:00:00.000000000 Z
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print
@@ -71,6 +71,7 @@ files:
71
71
  - README.md
72
72
  - Rakefile
73
73
  - bin/console
74
+ - bin/gh-md-toc
74
75
  - bin/setup
75
76
  - check_please.gemspec
76
77
  - exe/check_please
@@ -85,11 +86,14 @@ files:
85
86
  - lib/check_please/flag.rb
86
87
  - lib/check_please/flags.rb
87
88
  - lib/check_please/path.rb
89
+ - lib/check_please/path_segment.rb
90
+ - lib/check_please/path_segment_matcher.rb
88
91
  - lib/check_please/printers.rb
89
92
  - lib/check_please/printers/base.rb
90
93
  - lib/check_please/printers/json.rb
91
94
  - lib/check_please/printers/table_print.rb
92
95
  - lib/check_please/refinements.rb
96
+ - lib/check_please/reification.rb
93
97
  - lib/check_please/version.rb
94
98
  - usage_examples.rb
95
99
  homepage: https://github.com/RealGeeks/check_please