Skip to content

Commit

Permalink
returning false or nil from safe_converter could be problematic for
Browse files Browse the repository at this point in the history
boolean values
  • Loading branch information
AaronLasseigne committed Jan 10, 2021
1 parent 8035640 commit 1568f79
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Layout/MultilineOperationIndentation:
Metrics/BlockLength:
Exclude:
- spec/**/*
Metrics/MethodLength:
Max: 15
Style/FrozenStringLiteralComment:
Exclude:
- '**/*.gemspec'
Expand Down
16 changes: 2 additions & 14 deletions lib/active_interaction/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def default?
# @private
# rubocop:disable Metrics/MethodLength
def cast(value, context, convert: true, reconstantize: true)
if safe_matches?(value)
if matches?(value)
adjust_output(value, context)
# we can't use `nil?` because BasicObject doesn't have it
elsif value == nil # rubocop:disable Style/NilComparison
Expand All @@ -186,7 +186,7 @@ def cast(value, context, convert: true, reconstantize: true)
reconstantize: false
)
elsif convert
public_send(__method__, safe_convert(value), context,
public_send(__method__, convert(value), context,
convert: false,
reconstantize: reconstantize
)
Expand Down Expand Up @@ -217,12 +217,6 @@ def matches?(_value)
false
end

def safe_matches?(value)
matches?(value)
rescue NoMethodError
false
end

def adjust_output(value, _context)
value
end
Expand All @@ -231,12 +225,6 @@ def convert(value)
value
end

def safe_convert(value)
convert(value)
rescue NoMethodError
false
end

# @param value [Object]
# @return [String]
def describe(value)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/abstract_date_time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def klasses

def matches?(value)
klasses.any? { |klass| value.is_a?(klass) }
rescue NoMethodError # BasicObject
false
end

def convert(value)
Expand All @@ -32,6 +34,8 @@ def convert(value)
end
rescue ArgumentError
value
rescue NoMethodError # BasicObject
super
end

def convert_string(value)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/abstract_numeric_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def database_column_type

def matches?(value)
value.is_a?(klass)
rescue NoMethodError # BasicObject
false
end

def convert(value)
Expand All @@ -27,6 +29,8 @@ def convert(value)
else
super
end
rescue NoMethodError # BasicObject
super
end

def converter(value)
Expand Down
6 changes: 5 additions & 1 deletion lib/active_interaction/filters/array_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def klasses

def matches?(value)
klasses.any? { |klass| value.is_a?(klass) }
rescue NoMethodError # BasicObject
false
end

def adjust_output(value, context)
Expand All @@ -62,8 +64,10 @@ def convert(value)
if value.respond_to?(:to_ary)
value.to_ary
else
value
super
end
rescue NoMethodError # BasicObject
super
end

def add_option_in_place_of_name(klass, options)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/boolean_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def database_column_type

def matches?(value)
value.is_a?(TrueClass) || value.is_a?(FalseClass)
rescue NoMethodError # BasicObject
false
end

def convert(value)
Expand All @@ -37,6 +39,8 @@ def convert(value)
else
super
end
rescue NoMethodError # BasicObject
super
end
end
end
6 changes: 5 additions & 1 deletion lib/active_interaction/filters/hash_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class HashFilter < Filter

def matches?(value)
value.is_a?(Hash)
rescue NoMethodError # BasicObject
false
end

def clean_value(hash, name, filter, value, context)
Expand All @@ -55,8 +57,10 @@ def convert(value)
if value.respond_to?(:to_hash)
value.to_hash
else
value
super
end
rescue NoMethodError # BasicObject
super
end

# rubocop:disable Style/MissingRespondToMissing, Style/MethodMissingSuper
Expand Down
6 changes: 5 additions & 1 deletion lib/active_interaction/filters/string_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def strip?

def matches?(value)
value.is_a?(String)
rescue NoMethodError # BasicObject
false
end

def adjust_output(value, _context)
Expand All @@ -38,8 +40,10 @@ def convert(value)
if value.respond_to?(:to_str)
value.to_str
else
value
super
end
rescue NoMethodError # BasicObject
super
end
end
end
4 changes: 4 additions & 0 deletions lib/active_interaction/filters/symbol_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class SymbolFilter < Filter

def matches?(value)
value.is_a?(Symbol)
rescue NoMethodError # BasicObject
false
end

def convert(value)
Expand All @@ -28,6 +30,8 @@ def convert(value)
else
super
end
rescue NoMethodError # BasicObject
super
end
end
end
2 changes: 2 additions & 0 deletions lib/active_interaction/filters/time_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def convert(value)
else
super
end
rescue NoMethodError # BasicObject
super
end
end
end

0 comments on commit 1568f79

Please sign in to comment.