Skip to content

Instantly share code, notes, and snippets.

@markburns
Created October 27, 2013 13:35
Show Gist options
  • Save markburns/7182190 to your computer and use it in GitHub Desktop.
Save markburns/7182190 to your computer and use it in GitHub Desktop.
i15r idea
module I15r; end
module Visitable
def accept(visitor)
visitor.send visitor_method_name, self
end
def visitor_method_name
"visit_#{self.class.name}".gsub(/::/, "__")
end
end
class I15r::Line < String
include Visitable
end
class I15r::File
include Visitable
def initialize file
@file = file
@lines = File.readlines(file).map{|l| I15r::Line.new l }
end
def to_s
@file.to_s
end
def accept(visitor)
super
@lines.each{|e| e.accept visitor }
end
end
class I15r::Path
include Visitable
def initialize(path)
@path = path
files = Dir.glob(File.join path, "**/*.haml") +
Dir.glob(File.join path, "**/*.erb")
@files = files.map{|f| I15r::File.new(f) }
end
def to_s
@path.to_s
end
def accept(visitor)
super
@files.each{|e| e.accept visitor }
end
end
class I15r::TextReplacementPrintVisitor
def visit_I15r__Line line
puts "#{self.class.name} Replacing text of " + line.to_s
end
def visit_I15r__File file
puts "#{self.class.name} Visiting file #{file}"
end
def visit_I15r__Path path
puts "#{self.class.name} Visiting path #{path}"
end
end
class I15r::LocaleFileWriterVisitor
def visit_I15r__Line line
puts "#{self.class.name} Writing Locale for #{line}"
end
def visit_I15r__File file
puts "#{self.class.name} Opening #{file} for locale writing"
end
def visit_I15r__Path path
puts "#{self.class.name} Starting finding files in #{path}"
end
end
path = I15r::Path.new("spec/support")
path.accept(I15r::TextReplacementPrintVisitor.new());
path.accept(I15r::LocaleFileWriterVisitor.new());
@markburns
Copy link
Author

To get the gist to run, it expects erb or haml templates in spec/support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment