Skip to content

Commit

Permalink
Negative numbers (#126)
Browse files Browse the repository at this point in the history
* update float/integer regex to handle negative values

* bump version to 3.6.1

* spec

* update hash syntax in specs

* change to major version 3.7.0

* use eq

* actual bump to major version v4

* update gem version in readme

* v3->v4 migration guide

* add sample custom parser

* Add comment syntax highlighting

Co-authored-by: Conor Hawes <[email protected]>
  • Loading branch information
dianacamacho and chawes13 authored Nov 23, 2021
1 parent d4b7851 commit 45a54d5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
decanter (3.6.0)
decanter (4.0.0)
actionpack (>= 4.2.10)
activesupport
rails-html-sanitizer (>= 1.0.4)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Decanter is a Ruby gem that makes it easy to transform incoming data before it hits the model. You can think of Decanter as the opposite of Active Model Serializers (AMS). While AMS transforms your outbound data into a format that your frontend consumes, Decanter transforms your incoming data into a format that your backend consumes.

```ruby
gem 'decanter', '~> 3.0'
gem 'decanter', '~> 4.0'
```

## Migration Guides
Expand Down
2 changes: 1 addition & 1 deletion lib/decanter/parser/float_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Decanter
module Parser
class FloatParser < ValueParser
REGEX = /(\d|[.])/
REGEX = /(\d|[.]|[-])/

allow Float, Integer

Expand Down
2 changes: 1 addition & 1 deletion lib/decanter/parser/integer_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Decanter
module Parser
class IntegerParser < ValueParser
REGEX = /(\d|[.])/
REGEX = /(\d|[.]|[-])/

allow Integer

Expand Down
2 changes: 1 addition & 1 deletion lib/decanter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Decanter
VERSION = '3.6.0'.freeze
VERSION = '4.0.0'.freeze
end
35 changes: 35 additions & 0 deletions migration-guides/v4.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# v4.0.0 Migration Guide

_Note: this guide assumes you are upgrading from decanter v3 to v4._

This version contains the following breaking changes:

1. `FloatParser` and `IntegerParser` have been updated to address a bug where negative numbers were being parsed as positive. In the (unlikely) event that your project was relying on the previous behavior, you can pin the gem version to `v3.6.0` or include the legacy version(s) of the parsers as custom parsers in your project.

To add a custom parser, add the new parser class to your project:

```rb
# app/parsers/postive_float_parser.rb

class PositiveFloatParser < Decanter::Parser::ValueParser
REGEX = /(\d|[.])/

allow Float, Integer

parser do |val, options|
raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array
next if (val.nil? || val === '')
val.scan(REGEX).join.try(:to_f)
end
end
```

Then, use the appropriate key to look up the parser in your decanter:

```rb
# app/decanters/product_decanter.rb

class ProductDecanter < Decanter::Base
input :price, :positive_float #=> PositiveFloatParser
end
```
16 changes: 12 additions & 4 deletions spec/decanter/parser/float_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@

describe '#parse' do
context 'with a string' do
it 'returns a float' do
expect(parser.parse(name, '1.00')).to match({name => 1.00})
context 'with a positive value' do
it 'returns a positive float' do
expect(parser.parse(name, '1.00')).to eq({ foo: 1.00 })
end
end

context 'with a negative value' do
it 'returns a negative float' do
expect(parser.parse(name, '-1.00')).to eq({ foo: -1.00 })
end
end
end

context 'with empty string' do
it 'returns nil' do
expect(parser.parse(name, '')).to match({name => nil})
expect(parser.parse(name, '')).to eq({ foo: nil })
end
end

context 'with nil' do
it 'returns nil' do
expect(parser.parse(name, nil)).to match({name => nil})
expect(parser.parse(name, nil)).to eq({ foo: nil })
end
end

Expand Down
16 changes: 12 additions & 4 deletions spec/decanter/parser/integer_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@

describe '#parse' do
context 'with a string' do
it 'returns an integer' do
expect(parser.parse(name, '1')).to match({name => 1})
context 'with a positive value' do
it 'returns a positive integer' do
expect(parser.parse(name, '1')).to eq({ foo: 1 })
end
end

context 'with a negative value' do
it 'returns a negative integer' do
expect(parser.parse(name, '-1')).to eq({ foo: -1 })
end
end
end

context 'with empty string' do
it 'returns nil' do
expect(parser.parse(name, '')).to match({name => nil})
expect(parser.parse(name, '')).to eq({ foo: nil })
end
end

context 'with nil' do
it 'returns nil' do
expect(parser.parse(name, nil)).to match({name => nil})
expect(parser.parse(name, nil)).to eq({ foo: nil })
end
end

Expand Down

0 comments on commit 45a54d5

Please sign in to comment.