Class: RSpecUUID::Matchers::UUIDMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-uuid/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(version = nil) ⇒ UUIDMatcher

Returns a new instance of UUIDMatcher.



4
5
6
# File 'lib/rspec-uuid/matchers.rb', line 4

def initialize(version = nil)
  @version = version
end

Instance Method Details

#descriptionObject



26
27
28
# File 'lib/rspec-uuid/matchers.rb', line 26

def description
  @version ? "a UUID v#{@version}" : "a UUID"
end

#failure_messageObject



30
31
32
33
34
35
36
# File 'lib/rspec-uuid/matchers.rb', line 30

def failure_message
  if @actual_version
    "expected #{description}, found a UUID v#{@actual_version}"
  else
    "expected #{description}"
  end
end

#failure_message_when_negatedObject



38
39
40
# File 'lib/rspec-uuid/matchers.rb', line 38

def failure_message_when_negated
  "did not expect #{description}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec-uuid/matchers.rb', line 8

def matches?(actual)
  return false unless actual.is_a?(String)

  # https://www.uuidtools.com/what-is-uuid
  match = actual.match /^\h{8}-\h{4}-(\h{4})-\h{4}-\h{12}$/

  return false unless match

  if @version
    # 1st nibble of 3rd section
    @actual_version = match[1].to_i(16) >> 12

    @version == @actual_version
  else
    true
  end
end