Skip to content

Commit

Permalink
Add a base option to integer filters
Browse files Browse the repository at this point in the history
This fixes #386.
  • Loading branch information
tfausak committed Oct 15, 2016
1 parent fe305e4 commit 9b8e3b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/active_interaction/filters/integer_filter.rb
Expand Up @@ -17,5 +17,18 @@ class Base
# @private
class IntegerFilter < AbstractNumericFilter
register :integer

private

# @return [Integer]
def base
options.fetch(:base, 0)
end

def convert(value, context)
Integer(value, base)
rescue ArgumentError
_cast(value, context)
end
end
end
8 changes: 8 additions & 0 deletions spec/active_interaction/filters/integer_filter_spec.rb
Expand Up @@ -42,6 +42,14 @@
end.to raise_error ActiveInteraction::InvalidValueError
end
end

it 'supports different bases' do
expect(filter.cast('07', nil)).to eql 7
expect do
filter.cast('08', nil)
end.to raise_error ActiveInteraction::InvalidValueError
expect(described_class.new(name, base: 10).cast('08', nil)).to eql 8
end
end

describe '#database_column_type' do
Expand Down

0 comments on commit 9b8e3b0

Please sign in to comment.