Created
April 21, 2020 08:11
-
-
Save criess/fa0770a73babfbb3038570b9344e94aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'csv' | |
require 'yaml' | |
require 'active_support/core_ext/string' | |
PUBLIC_ROLE = 'Public' | |
ROLES = ['Admin', 'Carl Employee', 'Investor', 'Advisor'].freeze | |
csv = CSV(File.open(ARGV.first, encoding: 'UTF-8', &:read), col_sep: ';', headers: true) | |
csv.shift.headers | |
csv.rewind | |
@parsed_infos = {} | |
def parse_infos(controller, action, infos) | |
key = [controller, action] | |
return if @parsed_infos.key?(key) | |
@parsed_infos[key] = if infos[PUBLIC_ROLE] == 1 | |
'true' | |
else | |
infos.select { |k,v| v == 1 }.keys.sort.map { |role| role.downcase.gsub(' ', '_') }.join('? || ') + '?' | |
end | |
end | |
dupe_free = csv.select do |row| | |
row = row.to_h.map { |k, v| %w[0 1].include?(v) ? [k, v.to_i] : [k, v] }.to_h | |
parse_infos(row['Endpoint'].split('/')[2], row['Operation'], row.slice(*[PUBLIC_ROLE] + ROLES)) | |
end | |
puts( | |
@parsed_infos.inject({}) do |pols, rule| | |
k = rule[0][0].classify + 'Policy' | |
pols[k] ||= '' | |
pols[k] += <<-EOF | |
def #{rule[0][1]}? | |
#{rule[1]} | |
end\n | |
EOF | |
.gsub(' ', ' ') | |
pols | |
end.map do |k, v| "class " + k + " < ApplicationPolicy\n" + v.gsub(/[\n ]+\z/m, '') + "\nend" end | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment