Skip to content
Open
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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2
2.6.10
18 changes: 15 additions & 3 deletions lib/frodata/properties/decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Decimal < FrOData::Property
# Returns the property value, properly typecast
# @return [BigDecimal,nil]
def value
if (@value.nil? || @value.empty?) && (strict? && allows_nil?)
if (@value.nil? || (@value.respond_to?(:empty?) && @value.empty?)) && (strict? && allows_nil?)
nil
else
BigDecimal(@value)
Expand All @@ -15,8 +15,9 @@ def value
# Sets the property value
# @params new_value something BigDecimal() can parse
def value=(new_value)
validate(BigDecimal(new_value.to_s))
@value = new_value.to_s
cleaned_value = clean_value(new_value)
validate(BigDecimal(cleaned_value))
@value = cleaned_value
end

# The FrOData type name
Expand All @@ -32,6 +33,17 @@ def url_value

private

# Single pass cleaning a value to make it into a number based on the
# specs in decimal_spec.rb.
def clean_value(value)
BigDecimal(value) # Trigger any underlying exceptions.
value
rescue ArgumentError
clean_value = value.to_s.sub(/[^.[:digit:]].*/, "")
clean_value = "0" if clean_value.bytesize == 0
clean_value
end

def validate(value)
if value > max_value || value < min_value || value.precs.first > 29
validation_error "Value is outside accepted range: #{min_value} to #{max_value}, or has more than 29 significant digits"
Expand Down