Skip to content

Commit c69db80

Browse files
committed
utils go global
1 parent e27aff1 commit c69db80

File tree

5 files changed

+206
-188
lines changed

5 files changed

+206
-188
lines changed

zcl_mustache_utils.clas.abap

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class ZCL_MUSTACHE_UTILS definition
2+
public
3+
final
4+
create public .
5+
6+
public section.
7+
8+
class-methods SPLIT_STRING
9+
importing
10+
!IV_TEXT type STRING
11+
!IV_SEP type CLIKE optional
12+
returning
13+
value(RT_TAB) type STRING_TABLE .
14+
class-methods JOIN_STRINGS
15+
importing
16+
!IT_TAB type STRING_TABLE
17+
!IV_SEP type CLIKE optional
18+
returning
19+
value(RV_TEXT) type STRING .
20+
class-methods CHECK_VERSION_FITS
21+
importing
22+
!I_REQUIRED_VERSION type STRING
23+
!I_CURRENT_VERSION type STRING
24+
returning
25+
value(R_FITS) type ABAP_BOOL .
26+
protected section.
27+
private section.
28+
ENDCLASS.
29+
30+
31+
32+
CLASS ZCL_MUSTACHE_UTILS IMPLEMENTATION.
33+
34+
35+
method check_version_fits.
36+
37+
types:
38+
begin of ty_version,
39+
major type numc4,
40+
minor type numc4,
41+
patch type numc4,
42+
end of ty_version.
43+
44+
data ls_cur_ver type ty_version.
45+
data ls_req_ver type ty_version.
46+
data lv_buf type string.
47+
48+
lv_buf = i_current_version.
49+
shift lv_buf left deleting leading 'v'.
50+
split lv_buf at '.' into ls_cur_ver-major ls_cur_ver-minor ls_cur_ver-patch.
51+
52+
lv_buf = i_required_version.
53+
shift lv_buf left deleting leading 'v'.
54+
split lv_buf at '.' into ls_req_ver-major ls_req_ver-minor ls_req_ver-patch.
55+
56+
if ls_req_ver-major <= ls_cur_ver-major.
57+
if ls_req_ver-minor <= ls_cur_ver-minor.
58+
if ls_req_ver-patch <= ls_cur_ver-patch.
59+
r_fits = abap_true.
60+
endif.
61+
endif.
62+
endif.
63+
64+
endmethod.
65+
66+
67+
method join_strings.
68+
69+
data lv_sep type string.
70+
71+
if iv_sep is not supplied.
72+
lv_sep = cl_abap_char_utilities=>newline.
73+
else.
74+
lv_sep = iv_sep.
75+
endif.
76+
77+
concatenate lines of it_tab into rv_text separated by lv_sep.
78+
79+
endmethod. "join_strings
80+
81+
82+
method split_string.
83+
84+
if iv_sep is not initial.
85+
split iv_text at iv_sep into table rt_tab.
86+
else.
87+
find first occurrence of cl_abap_char_utilities=>cr_lf in iv_text.
88+
if sy-subrc = 0.
89+
split iv_text at cl_abap_char_utilities=>cr_lf into table rt_tab.
90+
else.
91+
split iv_text at cl_abap_char_utilities=>newline into table rt_tab.
92+
endif.
93+
endif.
94+
95+
endmethod. " split_string.
96+
ENDCLASS.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class ltcl_mustache_utils definition final
2+
for testing risk level harmless duration short.
3+
4+
private section.
5+
methods split_string for testing.
6+
methods join_strings for testing.
7+
methods check_version_fits for testing.
8+
9+
endclass. "ltcl_mustache_utils
10+
11+
class ltcl_mustache_utils implementation.
12+
13+
method split_string.
14+
15+
data: lt_exp type string_table.
16+
17+
append 'ABC' to lt_exp.
18+
append '123' to lt_exp.
19+
20+
cl_abap_unit_assert=>assert_equals(
21+
act = zcl_mustache_utils=>split_string(
22+
'ABC' && cl_abap_char_utilities=>cr_lf && '123' )
23+
exp = lt_exp ).
24+
25+
cl_abap_unit_assert=>assert_equals(
26+
act = zcl_mustache_utils=>split_string(
27+
'ABC' && cl_abap_char_utilities=>newline && '123' )
28+
exp = lt_exp ).
29+
30+
cl_abap_unit_assert=>assert_equals(
31+
act = zcl_mustache_utils=>split_string(
32+
iv_text = 'ABC' && 'X' && '123' iv_sep = 'X' )
33+
exp = lt_exp ).
34+
35+
endmethod. " split_string.
36+
37+
method join_strings.
38+
39+
data: lt_src type string_table.
40+
41+
append 'ABC' to lt_src.
42+
append '123' to lt_src.
43+
44+
cl_abap_unit_assert=>assert_equals(
45+
act = zcl_mustache_utils=>join_strings( lt_src )
46+
exp = 'ABC' && cl_abap_char_utilities=>newline && '123' ).
47+
48+
cl_abap_unit_assert=>assert_equals(
49+
act = zcl_mustache_utils=>join_strings( it_tab = lt_src iv_sep = 'X' )
50+
exp = 'ABC' && 'X' && '123' ).
51+
52+
endmethod. "join_strings
53+
54+
method check_version_fits.
55+
cl_abap_unit_assert=>assert_true(
56+
zcl_mustache_utils=>check_version_fits(
57+
i_current_version = 'v2.2.2'
58+
i_required_version = 'v2.2.2' ) ).
59+
cl_abap_unit_assert=>assert_true(
60+
zcl_mustache_utils=>check_version_fits(
61+
i_current_version = 'v2.2.2'
62+
i_required_version = 'v2.1.2' ) ).
63+
cl_abap_unit_assert=>assert_true(
64+
zcl_mustache_utils=>check_version_fits(
65+
i_current_version = 'v2.2.2'
66+
i_required_version = 'v1.0.0' ) ).
67+
cl_abap_unit_assert=>assert_false(
68+
zcl_mustache_utils=>check_version_fits(
69+
i_current_version = 'v2.2.2'
70+
i_required_version = 'v2.2.3' ) ).
71+
cl_abap_unit_assert=>assert_false(
72+
zcl_mustache_utils=>check_version_fits(
73+
i_current_version = 'v2.2.2'
74+
i_required_version = 'v2.2.30' ) ).
75+
cl_abap_unit_assert=>assert_false(
76+
zcl_mustache_utils=>check_version_fits(
77+
i_current_version = 'v2.2.2'
78+
i_required_version = 'v2.3.1' ) ).
79+
cl_abap_unit_assert=>assert_false(
80+
zcl_mustache_utils=>check_version_fits(
81+
i_current_version = 'v2.2.2'
82+
i_required_version = 'v3.0.0' ) ).
83+
endmethod.
84+
85+
endclass. "ltcl_mustache_utils

zcl_mustache_utils.clas.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>ZCL_MUSTACHE_UTILS</CLSNAME>
7+
<VERSION>1</VERSION>
8+
<LANGU>E</LANGU>
9+
<DESCRIPT>Mustache utility library</DESCRIPT>
10+
<STATE>1</STATE>
11+
<CLSCCINCL>X</CLSCCINCL>
12+
<FIXPT>X</FIXPT>
13+
<UNICODE>X</UNICODE>
14+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
15+
</VSEOCLASS>
16+
</asx:values>
17+
</asx:abap>
18+
</abapGit>

zmustache.prog.abap

Lines changed: 5 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -26,97 +26,6 @@
2626
*| project homepage: https://github.com/sbcgua/abap_mustache |
2727
*\--------------------------------------------------------------------------------/
2828

29-
**********************************************************************
30-
* UTILS
31-
**********************************************************************
32-
33-
CLASS lcl_mustache_utils DEFINITION FINAL.
34-
35-
PUBLIC SECTION.
36-
37-
CLASS-METHODS split_string
38-
IMPORTING iv_text TYPE string
39-
iv_sep TYPE clike OPTIONAL
40-
RETURNING VALUE(rt_tab) TYPE string_table.
41-
42-
CLASS-METHODS join_strings
43-
IMPORTING it_tab TYPE string_table
44-
iv_sep TYPE clike OPTIONAL
45-
RETURNING VALUE(rv_text) TYPE string.
46-
47-
class-methods CHECK_VERSION_FITS
48-
importing
49-
!I_REQUIRED_VERSION type STRING
50-
!I_CURRENT_VERSION type STRING
51-
returning
52-
value(R_FITS) type ABAP_BOOL .
53-
54-
ENDCLASS. "lcl_mustache_utils
55-
56-
CLASS lcl_mustache_utils IMPLEMENTATION.
57-
58-
METHOD split_string.
59-
60-
IF iv_sep IS NOT INITIAL.
61-
SPLIT iv_text AT iv_sep INTO TABLE rt_tab.
62-
ELSE.
63-
FIND FIRST OCCURRENCE OF cl_abap_char_utilities=>cr_lf IN iv_text.
64-
IF sy-subrc = 0.
65-
SPLIT iv_text AT cl_abap_char_utilities=>cr_lf INTO TABLE rt_tab.
66-
ELSE.
67-
SPLIT iv_text AT cl_abap_char_utilities=>newline INTO TABLE rt_tab.
68-
ENDIF.
69-
ENDIF.
70-
71-
ENDMETHOD. " split_string.
72-
73-
METHOD join_strings.
74-
75-
DATA lv_sep TYPE string.
76-
77-
IF iv_sep IS NOT SUPPLIED.
78-
lv_sep = cl_abap_char_utilities=>newline.
79-
ELSE.
80-
lv_sep = iv_sep.
81-
ENDIF.
82-
83-
CONCATENATE LINES OF it_tab INTO rv_text SEPARATED BY lv_sep.
84-
85-
ENDMETHOD. "join_strings
86-
87-
method check_version_fits.
88-
89-
types:
90-
begin of ty_version,
91-
major type numc4,
92-
minor type numc4,
93-
patch type numc4,
94-
end of ty_version.
95-
96-
data ls_cur_ver type ty_version.
97-
data ls_req_ver type ty_version.
98-
data lv_buf type string.
99-
100-
lv_buf = i_current_version.
101-
shift lv_buf left deleting leading 'v'.
102-
split lv_buf at '.' into ls_cur_ver-major ls_cur_ver-minor ls_cur_ver-patch.
103-
104-
lv_buf = i_required_version.
105-
shift lv_buf left deleting leading 'v'.
106-
split lv_buf at '.' into ls_req_ver-major ls_req_ver-minor ls_req_ver-patch.
107-
108-
if ls_req_ver-major <= ls_cur_ver-major.
109-
if ls_req_ver-minor <= ls_cur_ver-minor.
110-
if ls_req_ver-patch <= ls_cur_ver-patch.
111-
r_fits = abap_true.
112-
endif.
113-
endif.
114-
endif.
115-
116-
endmethod.
117-
118-
ENDCLASS. "lcl_mustache_utils
119-
12029
**********************************************************************
12130
* MUSTACHE LOGIC
12231
**********************************************************************
@@ -244,10 +153,10 @@ CLASS lcl_mustache_parser IMPLEMENTATION.
244153

245154
IF lines( it_template ) > 0.
246155
LOOP AT it_template ASSIGNING <line>.
247-
APPEND LINES OF lcl_mustache_utils=>split_string( <line> ) TO lt_strings.
156+
APPEND LINES OF zcl_mustache_utils=>split_string( <line> ) TO lt_strings.
248157
ENDLOOP.
249158
ELSE. " iv_template, which can also be empty then
250-
lt_strings = lcl_mustache_utils=>split_string( iv_template ).
159+
lt_strings = zcl_mustache_utils=>split_string( iv_template ).
251160
ENDIF.
252161

253162
ls_newline_token-type = zif_mustache=>c_token_type-static.
@@ -969,7 +878,7 @@ CLASS lcl_mustache IMPLEMENTATION.
969878

970879
METHOD render_tt.
971880

972-
rt_tab = lcl_mustache_utils=>split_string(
881+
rt_tab = zcl_mustache_utils=>split_string(
973882
iv_sep = cl_abap_char_utilities=>newline
974883
iv_text = render( i_data ) ).
975884

@@ -991,7 +900,7 @@ CLASS lcl_mustache IMPLEMENTATION.
991900
CHANGING
992901
ct_lines = lt_temp ).
993902

994-
rv_text = lcl_mustache_utils=>join_strings( it_tab = lt_temp iv_sep = '' ).
903+
rv_text = zcl_mustache_utils=>join_strings( it_tab = lt_temp iv_sep = '' ).
995904

996905
ENDMETHOD. " render.
997906

@@ -1028,7 +937,7 @@ CLASS lcl_mustache IMPLEMENTATION.
1028937

1029938
method CHECK_VERSION_FITS.
1030939

1031-
r_fits = lcl_mustache_utils=>check_version_fits(
940+
r_fits = zcl_mustache_utils=>check_version_fits(
1032941
i_current_version = zif_mustache=>version
1033942
i_required_version = i_required_version ).
1034943

0 commit comments

Comments
 (0)