lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 3f1d437 088/156: Add add_abbreviated_variable


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 3f1d437 088/156: Add add_abbreviated_variable() helper
Date: Tue, 30 Jan 2018 17:22:19 -0500 (EST)

branch: master
commit 3f1d437ac36b4cce8da49b8303ff5ba80d70992f
Author: Vadim Zeitlin <address@hidden>
Commit: Vadim Zeitlin <address@hidden>

    Add add_abbreviated_variable() helper
    
    Move "CorpNameAbbrev50" and "Insured1Abbrev50" variables out of common
    code as they are specific to regular illustrations only.
    
    Define add_abbreviated_variable() to allow consistently defining other
    variables with similar names in the future.
    
    No real changes, this is a pure refactoring.
---
 ledger_pdf_generator_wx.cpp | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/ledger_pdf_generator_wx.cpp b/ledger_pdf_generator_wx.cpp
index aacd259..bee5646 100644
--- a/ledger_pdf_generator_wx.cpp
+++ b/ledger_pdf_generator_wx.cpp
@@ -686,6 +686,30 @@ class pdf_illustration : protected html_interpolator
             }
     }
 
+  protected:
+    // Helper for abbreviating a string to at most the given length (in bytes).
+    static std::string abbreviate_if_necessary(std::string s, size_t len)
+    {
+        if(s.length() > len && len > 3)
+            {
+            s.replace(len - 3, std::string::npos, "...");
+            }
+
+        return s;
+    }
+
+    // Helper for creating abbreviated variables in the derived classes: such
+    // variables have the name based on the name of the original variable with
+    // "Abbrev" and "len" appended to it and their value is at most "len" bytes
+    // long.
+    void add_abbreviated_variable(std::string const& var, size_t len)
+    {
+        add_variable
+            (var + "Abbrev" + std::to_string(len)
+            ,abbreviate_if_necessary(evaluate(var), len)
+            );
+    }
+
   private:
     // Initialize the variables that can be interpolated later.
     void init_variables()
@@ -729,15 +753,6 @@ class pdf_illustration : protected html_interpolator
             return s;
             };
 
-        add_variable
-            ("CorpNameAbbrev50"
-            ,abbreviate_if_necessary(evaluate("CorpName"), 50)
-            );
-
-        add_variable
-            ("Insured1Abbrev50"
-            ,abbreviate_if_necessary(evaluate("Insured1"), 50)
-            );
     }
 
     // Use non-default font sizes to make it simpler to replicate the existing
@@ -1641,6 +1656,9 @@ class pdf_illustration_regular : public pdf_illustration
         auto const& state_abbrev = invar.GetStatePostalAbbrev();
 
         // Define variables specific to this illustration.
+        add_abbreviated_variable("CorpName", 50);
+        add_abbreviated_variable("Insured1", 50);
+
         add_variable
             ("ModifiedSinglePremium"
             ,starts_with(policy_name, "Single") && state_abbrev == "MA"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]