Skip to content

Instantly share code, notes, and snippets.

@craigjbass
Created March 26, 2019 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigjbass/5704d9ce62f8f89cc0968911939666dc to your computer and use it in GitHub Desktop.
Save craigjbass/5704d9ce62f8f89cc0968911939666dc to your computer and use it in GitHub Desktop.
Mad Ruby
require 'csv'
expectations = []
CSV.foreach("#{__dir__}/expectations2.csv") do |line|
expectations << {
expectation: line[0],
roles: line[4]&.split(','),
description: line[1]
}
end
roles = {}
expectations.each do |expectation|
expectation[:roles].each do |role|
roles[role] ||= []
e = expectation.dup
e.delete(:roles)
roles[role] << e
end
end
class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
def safe_up
self.gsub("'", "\\'")
end
end
classes = []
roles.each_with_index do |(role, expectations)|
template = "class #{role.gsub(' ', '')}Expectations < JobSpec::Role::Expectations\n"
pp expectations
expectations.each do |expectation|
name = expectation[:expectation]&.safe_up
description = expectation[:description]&.safe_up
template += " expected '#{name}',\n '#{description}'\n\n"
end
classes << { code: template.strip + "\nend", file_name: (role.gsub(' ', '')+'Expectations').underscore+'.rb' }
end
classes.each do |clazz|
File.open("#{__dir__}/out/#{clazz[:file_name]}", 'w') do |file|
file << clazz[:code]
end
end
pp classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment