7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/project/stringify_float.rb', line 7
def stringify_float(name, opts = {})
opts = {
precision: 2,
}.merge(opts)
precision = opts[:precision]
multiplier = 10 ** precision
name_titleized = name.to_s.dup
name_titleized[0] = name_titleized[0].upcase
define_method("stringified#{name_titleized}") do
value = send("#{name}")
if value
value = value.to_i
"#{value / multiplier}.#{'%02d' % (value % multiplier)}"
else
""
end
end
define_method("stringified#{name_titleized}=") do |newValue|
newValue = newValue.to_s
if newValue.nil? or newValue.length == 0
value = nil
else
int_string, fraction_string = newValue.split('.')
value = int_string.to_i * multiplier
if fraction_string
fraction_string += "0" while fraction_string.length < precision
fraction = fraction_string[0..(precision - 1)].to_i
value += fraction
end
end
send("#{name}=", value)
end
end
|