-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds Decimal mapper type Shale Mappers generated from XSD's will use the Decimal mapper type to preserve the precision when reading decimal values from XML documents instead of converting them to floats. When the mapper is used for mapping other formats than XML the internal BigDecimal values are converted to floats as they would otherwise be written as strings in output documents. It should be possible to modify this in the future if the underlying serializers can handle BigDecimals. * Updates tests to handle the Decimal mapper The "maps xxx to object" test are extracted to a shared example to reduce repetition. But of course there were differences in the tests so the check for the value_collection was extracted as well.
- Loading branch information
Showing
10 changed files
with
313 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shale | ||
module Schema | ||
module Compiler | ||
# Class that maps Schema type to Shale Decimal type | ||
# | ||
# @api private | ||
class Decimal | ||
# Return name of the Shale type | ||
# | ||
# @return [String] | ||
# | ||
# @api private | ||
def name | ||
'Shale::Type::Decimal' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'value' | ||
|
||
module Shale | ||
module Type | ||
# Cast value to BigDecimal | ||
# | ||
# @api public | ||
class Decimal < Value | ||
class << self | ||
# @param [String, Float, Integer, nil] value Value to cast | ||
# | ||
# @return [BigDecimal, nil] | ||
# | ||
# @api private | ||
def cast(value) | ||
return if value.nil? | ||
|
||
case value | ||
when ::BigDecimal then value | ||
when ::Float then BigDecimal(value, value.to_s.length) | ||
else BigDecimal(value) | ||
end | ||
end | ||
|
||
def as_json(value, **) | ||
value.to_f | ||
end | ||
|
||
def as_yaml(value, **) | ||
value.to_f | ||
end | ||
|
||
def as_csv(value, **) | ||
value.to_f | ||
end | ||
|
||
def as_toml(value, **) | ||
value.to_f | ||
end | ||
|
||
def as_xml_value(value, **) | ||
value.to_s('F') | ||
end | ||
end | ||
end | ||
|
||
register(:decimal, Decimal) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'shale/schema/compiler/decimal' | ||
|
||
RSpec.describe Shale::Schema::Compiler::Decimal do | ||
describe '#name' do | ||
it 'returns Shale type name' do | ||
expect(described_class.new.name).to eq('Shale::Type::Decimal') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.