Use abstract class in the Rename module

This commit is contained in:
Alex Ling 2020-05-03 16:31:00 +00:00
parent 4cee5faecd
commit a5e97af3a3

View File

@ -1,7 +1,17 @@
module Rename module Rename
alias VHash = Hash(String, String) alias VHash = Hash(String, String)
class Variable abstract class Base(T)
@ary = [] of T
def push(var)
@ary.push var
end
abstract def render(hash : VHash)
end
class Variable < Base(String)
property id : String property id : String
def initialize(@id) def initialize(@id)
@ -12,13 +22,7 @@ module Rename
end end
end end
class Pattern class Pattern < Base(Variable)
@ary = [] of Variable
def push(var)
@ary.push var
end
def render(hash : VHash) def render(hash : VHash)
@ary.each do |v| @ary.each do |v|
if hash.has_key? v.id if hash.has_key? v.id
@ -29,13 +33,7 @@ module Rename
end end
end end
class Group class Group < Base(Pattern | String)
@ary = [] of (Pattern | String)
def push(var)
@ary.push var
end
def render(hash : VHash) def render(hash : VHash)
return "" if @ary.select(&.is_a? Pattern) return "" if @ary.select(&.is_a? Pattern)
.any? &.as(Pattern).render(hash).empty? .any? &.as(Pattern).render(hash).empty?
@ -49,11 +47,9 @@ module Rename
end end
end end
class Rule class Rule < Base(Group | String | Pattern)
ESCAPE = ['/'] ESCAPE = ['/']
@ary = [] of (Group | String | Pattern)
def initialize(str : String) def initialize(str : String)
parse! str parse! str
rescue e rescue e