Skip to content
Open
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
21 changes: 21 additions & 0 deletions lib/transformer/caster/template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Transformer
module Caster
class Template
PRECISION = 14

def initialize
@template = { area: {}, file_values: {}, graph_values: {} }
end
Expand All @@ -9,6 +11,9 @@ def add_graph_value(graph_method, value)
key = graph_method.export_key.to_s
export_method = graph_method.export_method

# Round all numeric values recursively to PRECISION decimals for consistency
value = deep_round(value)

@template.fetch(:graph_values)[key] ||= {}
@template.fetch(:graph_values).fetch(key).store(export_method, value)
end
Expand All @@ -29,6 +34,22 @@ def get(key)
def dump
@template
end

private

# Recursively rounds all Float values to PRECISION decimal places
def deep_round(value)
case value
when Float
value.round(PRECISION)
when Hash
value.transform_values { |v| deep_round(v) }
when Array
value.map { |v| deep_round(v) }
else
value
end
end
end
end
end