Created
April 3, 2014 16:53
-
-
Save rosenfeld/9958332 to your computer and use it in GitHub Desktop.
Demonstrates how to generate refined version for Facets gem
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
Simple script that should work if you follow the convention of declaring a single outer class per file with simple class names and structure. | |
Some files may not be compatible with refinements and should have some comment like "#skip-refinement-conversion" in the file header (first 10_000 chars of the file). The "class" line should also be in the header section of the file. | |
Files skipped will be copied "as is" to the new directory. |
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
require 'fileutils' | |
class Converter | |
DEST = 'lib-refined' | |
def self.convert | |
new.convert | |
end | |
def convert | |
FileUtils.mkdir_p DEST | |
Dir['lib/**/*.rb'].each do |fn| | |
newfn = fn.sub 'lib', DEST | |
dir = File.dirname(newfn) | |
basename = File.basename fn | |
FileUtils.mkdir_p dir | |
File.write newfn, processed_file(fn) | |
end | |
end | |
def processed_file(fn) | |
content = file_content fn | |
header = content[0..10_000] # adjust for your needs or use full content | |
return content if header =~ /\#skip-refinement-conversion/ | |
return content unless match = header.match(/.*?^(\s*class\s.*?$)/m) | |
first_class_line = match[1] | |
match = first_class_line.match /\s*class\s*([A-Z]\w*)\s*$/ | |
return content unless match # skip complex cases | |
refinement_replacement = "module Facets\nrefine #{match[1]} do" | |
# assume a single "class XXX\n...\nend" block per file | |
content.sub(first_class_line, refinement_replacement) + "\nend" | |
end | |
def file_content(fn) | |
File.read fn | |
end | |
end | |
Converter.convert |
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
# ruby -I lib-refined/core test.rb | |
require 'facets/array/before' | |
using Facets | |
puts [1, 2].before 2 # 1 is printed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You gave me an idea.
Usage: