[gxml] Added GomDate Date attribute handling



commit 6bf4d84d4dbf9f712c02bf1158346d2f2112f672
Author: Daniel Espinosa <esodan gmail com>
Date:   Fri Feb 10 13:27:50 2017 -0600

    Added GomDate Date attribute handling

 gxml/GomProperty.vala          |   33 +++++++++++++++++++++++++++++++
 test/GomSerializationTest.vala |   42 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 2 deletions(-)
---
diff --git a/gxml/GomProperty.vala b/gxml/GomProperty.vala
index 747d90f..735a636 100644
--- a/gxml/GomProperty.vala
+++ b/gxml/GomProperty.vala
@@ -367,3 +367,36 @@ public class GXml.GomEnum : GomBaseProperty {
    */
   public void set_enum (int value) { _value = value; }
 }
+
+
+/**
+ * Convenient class to handle {@link GomElement}'s attributes
+ * using a {@link GLib.Date} as sources of values.
+ *
+ * Property is represented as a string using a %Y-%M-%D format
+ */
+public class GXml.GomDate : GomBaseProperty {
+  protected Date _value = Date ();
+  public override string? value {
+    owned get {
+      if (!_value.valid ()) return null;
+      char[] fr = new char[100];
+      _value.strftime (fr, "%Y-%m-%d");
+      return (string) fr;
+    }
+    set {
+      _value = Date ();
+      _value.set_parse (value);
+      if (!_value.valid ())
+        warning (_("Invalid Date for property: "+value));
+    }
+  }
+  /**
+   * Retrives current value.
+   */
+  public Date get_date () { return _value; }
+  /**
+   * Sets current value.
+   */
+  public void set_date (Date date) { _value = date; }
+}
diff --git a/test/GomSerializationTest.vala b/test/GomSerializationTest.vala
index 1bd17c2..6d5127d 100644
--- a/test/GomSerializationTest.vala
+++ b/test/GomSerializationTest.vala
@@ -189,6 +189,8 @@ class GomSerializationTest : GXmlTest  {
     public bool tax_free { get; set; }
     [Description (nick="::Month")]
     public Month month { get; set; }
+    [Description (nick="::PayDate")]
+    public GomDate pay_date { get; set; }
     construct { try { initialize ("Taxes"); } catch { assert_not_reached (); } }
     public string to_string () {
       var parser = new XParser (this);
@@ -390,9 +392,9 @@ class GomSerializationTest : GXmlTest  {
       var t = new Taxes ();
       string s = t.to_string ();
       assert (s != null);
-//#if DEBUG
+#if DEBUG
       GLib.message ("DOC:"+s);
-//#endif
+#endif
       assert ("<Taxes " in s);
       assert ("monthRate=\"0\"" in s);
       assert ("Month=\"january\"" in s);
@@ -419,6 +421,42 @@ class GomSerializationTest : GXmlTest  {
       assert_not_reached ();
     }
     });
+    Test.add_func ("/gxml/gom-serialization/write/property-date", () => {
+    try {
+      var t = new Taxes ();
+      string s = t.to_string ();
+      assert (s != null);
+//#if DEBUG
+      GLib.message ("DOC:"+s);
+//#endif
+      assert ("<Taxes " in s);
+      assert ("monthRate=\"0\"" in s);
+      assert ("Month=\"january\"" in s);
+      assert ("TaxFree=\"false\"" in s);
+      t.pay_date = new GomDate ();
+      var d = Date ();
+      d.set_dmy ((DateDay) 1, (DateMonth) 2, (DateYear) 2017);
+      assert (d.valid ());
+      t.pay_date.set_date (d);
+      assert (t.pay_date.get_date ().valid ());
+      assert (t.pay_date != null);
+      assert (t.pay_date.value != null);
+      assert (t.pay_date.value == "2017-02-01");
+      t.pay_date.value = "2023-3-10";
+      assert (t.pay_date.get_date ().valid ());
+      assert (t.pay_date.value != null);
+      assert (t.pay_date.value == "2023-03-10");
+      s = t.to_string ();
+      assert (s != null);
+#if DEBUG
+      GLib.message ("DOC:"+s);
+#endif
+      assert ("PayDate=\"2023-03-10\"" in s);
+    } catch (GLib.Error e) {
+      GLib.message ("Error: "+e.message);
+      assert_not_reached ();
+    }
+    });
     Test.add_func ("/gxml/gom-serialization/write/property-arraylist", () => {
     try {
       var bs = new BookStand ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]