1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/spec/support/unit/change_value.rb
Kapil Sachdev b7e02184d9 fix(rubocop): Fix Layout/LineLength
- bump rubocop to v1.0
- Fix Layout/MultilineAssignmentLayout and other remaining offences
- Exculde appraisal generated gemfiles in rubocop
- Replace NON_NUMERIC_VALUE constant with instance method against 
failing test case in  rails <= 5.1 and postgres adapter.
This is a defect in rails where a frozen string is modified in 
https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb#L25
2020-11-03 10:05:25 -07:00

111 lines
2 KiB
Ruby

module UnitTests
class ChangeValue
def self.call(column_type, value, value_changer)
new(column_type, value, value_changer).call
end
def initialize(column_type, value, value_changer)
@column_type = column_type
@value = value
@value_changer = value_changer
end
def call
if value_changer.is_a?(Proc)
value_changer.call(value)
elsif respond_to?(value_changer, true)
send(value_changer)
else
value.public_send(value_changer)
end
end
protected
attr_reader :column_type, :value, :value_changer
private
def previous_value
if value.is_a?(String)
value[0..-2] + (value[-1].ord - 1).chr
else
value - 1
end
end
def next_value
if value.is_a?(Array)
value + [value.first.class.new]
elsif value.respond_to?(:next)
value.next
else
value + 1
end
end
def next_next_value
change_value(change_value(value, :next_value), :next_value)
end
def next_value_or_numeric_value
if value
change_value(value, :next_value)
else
change_value(value, :numeric_value)
end
end
def next_value_or_non_numeric_value
if value
change_value(value, :next_value)
else
change_value(value, :non_numeric_value)
end
end
def never_falsy
value || dummy_value_for_column
end
def truthy_or_numeric
value || 1
end
def never_blank
value.presence || dummy_value_for_column
end
def nil_to_blank
value || ''
end
def always_nil
nil
end
def add_character
"#{value}a"
end
def remove_character
value.chop
end
def numeric_value
'1'
end
def non_numeric_value
'a'
end
def change_value(value, value_changer)
self.class.call(column_type, value, value_changer)
end
def dummy_value_for_column
Shoulda::Matchers::Util.dummy_value_for(column_type)
end
end
end