Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a base option to integer filters #387

Merged
merged 1 commit into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/active_interaction/filters/integer_filter.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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