[Ovirt-devel] [PATCH client 04/11] Add way to format size as human readable

David Lutterkort lutter at redhat.com
Mon Jan 26 21:20:34 UTC 2009


---
 lib/ovirt.rb |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/lib/ovirt.rb b/lib/ovirt.rb
index ea55feb..ce51e66 100644
--- a/lib/ovirt.rb
+++ b/lib/ovirt.rb
@@ -21,6 +21,31 @@ module OVirt
   class Base < ActiveResource::Base
     LOGIN_PATH = "login/login"
 
+    def to_human_readable(size)
+      size = size.to_f
+      unit = "kB"
+      if size > 512
+        size /= 1024
+        unit = "MB"
+        if size > 512
+          size /= 1024
+          unit = "GB"
+          if size > 512
+            size /= 1024
+            unit = "TB"
+          end
+        end
+      end
+      format "%5.2f %s", size, unit
+    end
+
+    def self.human_readable(attr)
+      attr = attr.to_s
+      define_method("human_readable_#{attr}") do
+        to_human_readable(attributes[attr])
+      end
+    end
+
     def self.login
       response = nil
       begin
@@ -92,7 +117,13 @@ module OVirt
     end
   end
 
+  class StorageVolume < Base
+    human_readable :size
+  end
+
   class Host < Base
+    human_readable :memory
+
     def self.find_by_uuid(uuid)
       find(:first, :params => { :uuid => uuid })
     end
-- 
1.6.0.6




More information about the ovirt-devel mailing list