Class: GLCommand::Matchers::CommandArgumentMatcher

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

Overview

Base matcher for ‘requires` and `allows`, which store a Hash of { attribute: Type }.

Direct Known Subclasses

AllowArgumentMatcher, RequireArgumentMatcher

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ CommandArgumentMatcher

Returns a new instance of CommandArgumentMatcher.



9
10
11
12
# File 'lib/gl_command/rspec/matchers.rb', line 9

def initialize(attribute)
  @attribute = attribute
  @expected_type = nil
end

Instance Method Details

#being(expected_type) ⇒ Object



14
15
16
17
# File 'lib/gl_command/rspec/matchers.rb', line 14

def being(expected_type)
  @expected_type = expected_type
  self
end

#descriptionObject



29
30
31
# File 'lib/gl_command/rspec/matchers.rb', line 29

def description
  "#{action} argument `#{@attribute}`"
end

#failure_messageObject



33
34
35
36
37
# File 'lib/gl_command/rspec/matchers.rb', line 33

def failure_message
  message = "Expected #{@command_class.name} to #{action} `#{@attribute}`"
  message += " of type `#{@expected_type}`" if @expected_type
  message
end

#failure_message_when_negatedObject



39
40
41
42
43
# File 'lib/gl_command/rspec/matchers.rb', line 39

def failure_message_when_negated
  message = "Expected #{@command_class.name} not to #{action} `#{@attribute}`"
  message += " of type `#{@expected_type}`" if @expected_type
  message
end

#matches?(command_class) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
# File 'lib/gl_command/rspec/matchers.rb', line 19

def matches?(command_class)
  @command_class = command_class.is_a?(Class) ? command_class : command_class.class
  attributes = @command_class.public_send(scope)

  return false unless attributes.key?(@attribute)
  return true if @expected_type.nil? # Type check not requested

  attributes[@attribute] == @expected_type
end