Better assignments: class separation for sections, simple assignments, etc.

master
Philippe PITTOLI 4 years ago
parent 275c85db79
commit 34875be8c7

@ -1,5 +1,5 @@
name: spec
version: 0.1.0
version: 0.2.0
authors:
- Philippe Pittoli <karchnu@karchnu.fr>

@ -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 = /%\{([^}]*)\}/
value = v.value
while v =~ reg
while value =~ reg
x = reg.match(v)
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
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()
v = v.gsub "%{#{var[0]}}", "#{@assignments[var[0]]}"
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
@assignments[k] = v
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