Class: ErgomentumRspec::Matchers::ActiveModel::BeValidFor

Inherits:
Object
  • Object
show all
Defined in:
lib/ergomentum_rspec/matchers/active_model/be_valid_for.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ BeValidFor

Returns a new instance of BeValidFor.



11
12
13
# File 'lib/ergomentum_rspec/matchers/active_model/be_valid_for.rb', line 11

def initialize(value)
  @value = value
end

Instance Method Details

#descriptionObject



37
38
39
# File 'lib/ergomentum_rspec/matchers/active_model/be_valid_for.rb', line 37

def description
  "'#{@value}' be an valid value"
end

#failure_messageObject



33
34
35
# File 'lib/ergomentum_rspec/matchers/active_model/be_valid_for.rb', line 33

def failure_message
  "expect '#{@value}' to be valid"
end

#matches?(validator) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ergomentum_rspec/matchers/active_model/be_valid_for.rb', line 15

def matches?(validator)
  model = Class.new do
    include ::ActiveModel::Model
    include ::ActiveModel::Attributes

    # ActiveModel sometimes needs a non nil class name.
    # Because we are using an anonymous class, which have no name by default, we have to override the name
    # method.
    def self.name
      "SomeClass"
    end

    attribute :some_attribute
    validates :some_attribute, validator
  end.new(some_attribute: @value)
  model.valid? && model.errors.empty?
end