From 34875be8c7d91c498d3d108832837d90c4decd7a Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Thu, 1 Aug 2019 20:21:21 +0200 Subject: [PATCH] Better assignments: class separation for sections, simple assignments, etc. --- shard.yml | 2 +- src/spec.cr | 112 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 98 insertions(+), 16 deletions(-) diff --git a/shard.yml b/shard.yml index 61418a3..4523a35 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: spec -version: 0.1.0 +version: 0.2.0 authors: - Philippe Pittoli diff --git a/src/spec.cr b/src/spec.cr index 53d28df..d47f099 100644 --- a/src/spec.cr +++ b/src/spec.cr @@ -1,10 +1,39 @@ require "./parser" class Specs - property assignments : Hash(String, String) + class StringContainer + property value : String + + def initialize(@value) + end + end + + class LongStringContainer + property value : String + + def initialize(@value) + end + end + + class SectionContainer + property value : String + + def initialize(@value) + end + end + + class ArrayContainer + property value : Array(String) + + def initialize(@value) + end + end + + + property assignments : Hash(String, StringContainer | LongStringContainer | SectionContainer | ArrayContainer) def initialize - @assignments = Hash(String, String).new + @assignments = Hash(String, StringContainer | LongStringContainer | SectionContainer | ArrayContainer).new end def parse_hashglob(tree) @@ -89,7 +118,7 @@ class Specs return end - @assignments[name] = value + @assignments[name] = ArrayContainer.new value.split("\n") end def parse_freetextblock(tree) @@ -106,7 +135,7 @@ class Specs return end - @assignments[name] = value + @assignments[name] = SectionContainer.new value end def parse_glob (tree) @@ -165,7 +194,7 @@ class Specs return end - @assignments[name] = value + @assignments[name] = StringContainer.new value end def parse_tree(tree) @@ -186,22 +215,75 @@ class Specs end end - def rewrite - # replaces all occurences of %{variable} by the content of @assignments[variable] - @assignments.map do |k, v| - reg = /%\{([^}]*)\}/ + def replace_string_obj (v : StringContainer | SectionContainer | LongStringContainer) + reg = /%\{([^}]*)\}/ - while v =~ reg - x = reg.match(v) - unless x.nil? - var = x.captures() + value = v.value - v = v.gsub "%{#{var[0]}}", "#{@assignments[var[0]]}" + while value =~ reg + + x = reg.match(value) + unless x.nil? + var = x.captures() + + replacement_object = @assignments[var[0]] + case replacement_object + when StringContainer + replacement_value = replacement_object.value + value = value.gsub "%{#{var[0]}}", "#{replacement_value}" end end - @assignments[k] = v + v.value = value + + value = v.value + end + end + + def replace_array_obj (v : ArrayContainer) + reg = /%\{([^}]*)\}/ + + str_array = v.value + + newarray = Array(String).new + + str_array.each do |value| + tmp = value + while tmp =~ reg + + x = reg.match(tmp) + unless x.nil? + var = x.captures() + + replacement_object = @assignments[var[0]] + case replacement_object + when StringContainer + replacement_value = replacement_object.value + tmp = tmp.gsub "%{#{var[0]}}", "#{replacement_value}" + end + end + end + + newarray.push tmp + end + + v.value = newarray + end + + def rewrite + # replaces all occurences of %{variable} by the content of @assignments[variable] + @assignments.map do |k, v| + case v + when StringContainer + replace_string_obj v + when SectionContainer + replace_string_obj v + when LongStringContainer + replace_string_obj v + when ArrayContainer + replace_array_obj v + end end end