Skip to content

Commit

Permalink
number_with_method? should handle floats and simplify this
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed Feb 21, 2025
1 parent 11a32fb commit d4bdbe2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,14 @@ def self.quote_human(val, typ)
case typ&.to_sym
when :integer, :decimal, :fixnum, :float
return val if val.kind_of?(Numeric)
return val.to_i unless val.to_s.number_with_method? || typ == :float
return val.to_i unless val.to_s.number_with_method?

if val.to_s =~ /^([0-9.,]+)\.([a-z]+)$/
val, sfx = $1, $2
if sfx.ends_with?("bytes") && FORMAT_BYTE_SUFFIXES.key?(sfx.to_sym)
"#{val} #{FORMAT_BYTE_SUFFIXES[sfx.to_sym]}"
else
"#{val} #{sfx.titleize}"
end
/^([0-9.,]+)\.([a-z]+)$/.match(val.to_s)
val, sfx = $1, $2
if sfx.ends_with?("bytes") && FORMAT_BYTE_SUFFIXES.key?(sfx.to_sym)
"#{val} #{FORMAT_BYTE_SUFFIXES[sfx.to_sym]}"
else
val
"#{val} #{sfx.titleize}"
end
when :string, :date, :datetime, nil
"\"#{val}\""
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,11 @@
expect(exp.to_human).to eq("VM and Instance.Hardware.Volumes : Free Space Percent > 75")
end

it "generates a human readable string for a numeric FIELD value expression" do
exp = MiqExpression.new(">" => {"field" => "Vm.hardware.volumes-free_space_percent", "value" => 75.0})
expect(exp.to_human).to eq("VM and Instance.Hardware.Volumes : Free Space Percent > 75.0")
end

it "generates a human readable string for a symbol FIELD value expression" do
exp = MiqExpression.new(">" => {"field" => "Vm-allocated_disk_storage", "value" => :"5.megabytes"})
expect(exp.to_human).to eq("VM and Instance : Allocated Disk Storage > 5 MB")
Expand Down

0 comments on commit d4bdbe2

Please sign in to comment.