Skip to content

Instantly share code, notes, and snippets.

@rugyoga
Created November 6, 2019 20:54
Show Gist options
  • Save rugyoga/1ab523d2d6988afb0bff77e649dbf66b to your computer and use it in GitHub Desktop.
Save rugyoga/1ab523d2d6988afb0bff77e649dbf66b to your computer and use it in GitHub Desktop.
I ran into some issues using `plsm` library with Phoenix 1.4.10 and El;ixir 1.9. This script remedied most of them.
# frozen_string_literal: true
require 'active_support/inflector'
module PatchedString
refine String do
def camelcase
split('_').map(&:capitalize).join
end
end
end
# post processor for plsm tool
class PostPlsm
def self.run(dirname)
Dir.glob("./lib/#{dirname}/*s.ex") do |f|
process(f)
end
end
using PatchedString
def self.fix_module(match)
"#{match[1]}defmodule #{match[2].singularize} do"
end
def self.fix_field(match)
"#{match[1]}field #{match[2]}, :string#{match[3]}"
end
def self.fix_cast(match)
"#{match[1]}#{match[2]}"
end
def self.process(filename)
path = "#{filename.gsub(/\.ex$/, '').singularize}.ex"
File.open(path,'w') do |file|
File.readlines(filename).each do |line|
if line =~ /\A(\s*)defmodule\s+(.+)\s+do\s*\Z/
file.puts fix_module(Regexp.last_match)
elsif line =~ /\A(\s*)field\s+:id,\s+:integer\s*\Z/
next
elsif line =~ /\A(\s*)field\s+(:\w+),\s+:text(.*)\Z/
file.puts fix_field(Regexp.last_match)
elsif line =~ /\A(.*\|> cast\(params,\s*\[):id,\s*(.*\)).*\Z/
file.puts fix_cast(Regexp.last_match)
else
file.puts line
end
end
end
end
end
if $PROGRAM_NAME == __FILE__
raise ArgumentException, 'module name required' unless ARGV.size >= 1
PostPlsm.run(ARGV[0])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment