├── .gitignore ├── test ├── example.rb └── test_reusing.rb ├── Gemfile ├── lib └── reusing.rb ├── Indexfile ├── LICENSE.txt ├── .index ├── README.md └── .gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.lock 3 | pkg/ 4 | 5 | -------------------------------------------------------------------------------- /test/example.rb: -------------------------------------------------------------------------------- 1 | class String 2 | def example 3 | self + " example" 4 | end 5 | end 6 | 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "finder" 4 | 5 | group :test do 6 | gem "rubytest" 7 | gem "rubytest-cli" 8 | gem "microtest" 9 | end 10 | 11 | group :build do 12 | gem "indexer" 13 | end 14 | -------------------------------------------------------------------------------- /test/test_reusing.rb: -------------------------------------------------------------------------------- 1 | require 'reusing' 2 | 3 | $LOAD_PATH.unshift(__dir__) 4 | 5 | using 'example' 6 | 7 | module M 8 | refine String do 9 | def important 10 | self + "!" 11 | end 12 | end 13 | end 14 | 15 | using M 16 | 17 | result = "this".example 18 | raise unless result == "this example" 19 | 20 | result = "this".important 21 | raise unless result == "this!" 22 | 23 | 24 | -------------------------------------------------------------------------------- /lib/reusing.rb: -------------------------------------------------------------------------------- 1 | require 'finder' 2 | 3 | class << self 4 | alias :reusing :using 5 | 6 | define_method :using do |feature| 7 | if String === feature 8 | file = Find.feature(feature, :absolute=>true).first 9 | 10 | raise LoadError, "LoadError: cannot load such file -- #{file}" unless file 11 | 12 | text = File.read(file) 13 | 14 | text = text.gsub(/^\s*class\s*([A-Z]\w*)\s*$/, 'refine \1 do') 15 | text = "m = Module.new do\n" + text + "\nend\nreusing m\n" 16 | 17 | eval(text, TOPLEVEL_BINDING) 18 | else 19 | text = "reusing #{feature}" 20 | 21 | eval(text, TOPLEVEL_BINDING) 22 | end 23 | end 24 | end 25 | 26 | -------------------------------------------------------------------------------- /Indexfile: -------------------------------------------------------------------------------- 1 | --- 2 | name: 3 | reusing 4 | 5 | version: 6 | 1.0.0 7 | 8 | title: 9 | Reusing 10 | 11 | summary: 12 | Use `using` with extension files. 13 | 14 | description: 15 | Reusing makes it possible to use refinements without all the boiler plate 16 | normally necessary in their creation. Instead extension scripts can be 17 | loaded directly as refinements. 18 | 19 | organization: 20 | Rubyworks 21 | 22 | resources: 23 | home: http://rubyworks.github.com/reusing 24 | code: http://github.com/rubyworks/reusing 25 | 26 | repositories: 27 | upstream: git@github.com:rubyworks/reusing.git 28 | 29 | scm_uri: 30 | https://github.com/rubyworks/reusing/tree/master 31 | 32 | authors: 33 | - Thomas Sawyer 34 | 35 | copyrights: 36 | - (c) 2014 Rubyworks (BSD-2-Clause) 37 | 38 | created: 39 | 2014-04-16 40 | 41 | paths: 42 | load: 43 | - lib 44 | 45 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | BSD-2-Clause License (http://spdx.org/licenses/BSD-2-Clause) 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are 4 | permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of 7 | conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 10 | of conditions and the following disclaimer in the documentation and/or other materials 11 | provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED 14 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 15 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS 16 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 18 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 19 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 21 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | 23 | 24 | -------------------------------------------------------------------------------- /.index: -------------------------------------------------------------------------------- 1 | --- 2 | revision: 2013 3 | type: ruby 4 | sources: 5 | - Indexfile 6 | - Gemfile 7 | authors: 8 | - name: Thomas Sawyer 9 | email: transfire@gmail.com 10 | organizations: 11 | - name: Rubyworks 12 | requirements: 13 | - version: '>= 0' 14 | name: finder 15 | - groups: 16 | - test 17 | version: '>= 0' 18 | name: rubytest 19 | - groups: 20 | - test 21 | version: '>= 0' 22 | name: rubytest-cli 23 | - groups: 24 | - test 25 | version: '>= 0' 26 | name: microtest 27 | - groups: 28 | - build 29 | version: '>= 0' 30 | name: indexer 31 | conflicts: [] 32 | alternatives: [] 33 | resources: 34 | - type: home 35 | uri: http://rubyworks.github.com/reusing 36 | label: Homepage 37 | - type: code 38 | uri: http://github.com/rubyworks/reusing 39 | label: Source Code 40 | repositories: 41 | - name: upstream 42 | scm: git 43 | uri: git@github.com:rubyworks/reusing.git 44 | categories: [] 45 | copyrights: 46 | - holder: Rubyworks 47 | year: '2014' 48 | license: BSD-2-Clause 49 | customs: [] 50 | paths: 51 | load: 52 | - lib 53 | name: reusing 54 | title: Reusing 55 | version: 1.0.0 56 | summary: Use `using` with extension files. 57 | description: Reusing makes it possible to use refinements without all the boiler plate 58 | normally necessary in their creation. Instead extension scripts can be loaded directly 59 | as refinements. 60 | scm_uri: https://github.com/rubyworks/reusing/tree/master 61 | created: '2014-04-16' 62 | date: '2014-04-22' 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reusing 2 | 3 | ## Short Story 4 | 5 | Reusing allows for the use of refinements directly from an extension file. 6 | 7 | ```ruby 8 | require 'reusing' 9 | 10 | using 'some_extension_script' 11 | ``` 12 | 13 | ## Long Story 14 | 15 | Ruby introduced refinements in version 2.0. Refinements are essentially 16 | a safe alternative to monkey-patching. Unfortunately, the degree to which 17 | the syntax of refinements differs from writing traditional class extensions 18 | is a sever hinderence to their adoption. Traditionally, if you wanted to add 19 | a method to the String class, for instance, you simply open the class 20 | and define the method. 21 | 22 | ```ruby 23 | class String 24 | def some_method 25 | ... 26 | end 27 | end 28 | ``` 29 | 30 | And that's it. You can put this code in a file and require it as needed. 31 | Refinements, on the other hand, have much more *boiler-plate*. The 32 | above would have to be written: 33 | 34 | ```ruby 35 | module SomeModule 36 | refine String do 37 | def some_method 38 | ... 39 | end 40 | end 41 | end 42 | 43 | using SomeModule 44 | ``` 45 | 46 | The top portion can be put in an extension file too, but the `using SomeModule` part 47 | will have to be reissued in every file the refinement is needed. 48 | 49 | For a one-off, this isn't a big deal. But for a method library such as 50 | Ruby Facets, this has huge implications. In fact, Facets does not yet support 51 | refinements precisely becuase of this issue. To do so would require maintaining 52 | a second copy of every method in refinement format. While doable, it is obviously 53 | not DRY, and quite simply too much a pain in the ass to bother. 54 | 55 | So I consder what, if anything, could be done about this problem. And the idea of 56 | overriding the `using` method to accept a library file name was hatched. 57 | With it, most extension scripts can be readily used as-is, without all 58 | the boiler-plate. Usage is pretty simple. Let's say the example given 59 | above is in a library file called `some_method.rb`, then we can do: 60 | 61 | ```ruby 62 | require 'reusing' 63 | 64 | using 'some_method' 65 | ``` 66 | 67 | The new using method will find the file, read it in, perform a transformation 68 | converting `class String` into `refine String do` and wrap it all in a module 69 | which it then passed to the original `using` method (which has been aliased 70 | as `reusing`, btw, hence the name of this library). 71 | 72 | 73 | ## Caveats 74 | 75 | Unfortunately the implementation of Reusing is necessarily a bit of a hack. Although 76 | it works fine for basic extensions there are complications if, for instance, 77 | the script requires another extension script. While the scripts extensions will become 78 | refinements, the further requirements will not. There may also be issues if the 79 | extenesion defines meta-methods (i.e. class level extensions). 80 | 81 | 82 | ## Thoughts 83 | 84 | If Ruby Team were to take this issue to heat, than probably the ideal solution would 85 | have refinement syntax use normal `class` and `module` keywords, instead of the 86 | special `refine` clause. 87 | 88 | ```ruby 89 | module M 90 | class String 91 | def important! 92 | self + "!" 93 | end 94 | end 95 | end 96 | 97 | # refinement 98 | using M::* 99 | ``` 100 | 101 | In conjunction with this is should be possible to monkey patch with the same code as well. 102 | 103 | ```ruby 104 | # core extension 105 | patch M::* 106 | ``` 107 | 108 | In this way the both techniques could be used via the same code, while still being modular. 109 | But that is a significant change to Ruby itself, and ultimately falls to Matz to decide. 110 | 111 | 112 | ## Copyrights 113 | 114 | Copyright (c) 2014 Rubyworks (BSD-2 License) 115 | 116 | See LICENSE.txt for details. 117 | -------------------------------------------------------------------------------- /.gemspec: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'yaml' 4 | require 'pathname' 5 | 6 | module Indexer 7 | 8 | # Convert index data into a gemspec. 9 | # 10 | # Notes: 11 | # * Assumes all executables are in bin/. 12 | # * Does not yet handle default_executable setting. 13 | # * Does not yet handle platform setting. 14 | # * Does not yet handle required_ruby_version. 15 | # * Support for rdoc entries is weak. 16 | # 17 | class GemspecExporter 18 | 19 | # File globs to include in package --unless a manifest file exists. 20 | FILES = ".index .yardopts alt bin data demo ext features lib man spec test try* [A-Z]*.*" unless defined?(FILES) 21 | 22 | # File globs to omit from FILES. 23 | OMIT = "Config.rb" unless defined?(OMIT) 24 | 25 | # Standard file patterns. 26 | PATTERNS = { 27 | :root => '{.index,Gemfile}', 28 | :bin => 'bin/*', 29 | :lib => 'lib/{**/}*', #.rb', 30 | :ext => 'ext/{**/}extconf.rb', 31 | :doc => '*.{txt,rdoc,md,markdown,tt,textile}', 32 | :test => '{test,spec}/{**/}*.rb' 33 | } unless defined?(PATTERNS) 34 | 35 | # For which revision of indexer spec is this converter intended? 36 | REVISION = 2013 unless defined?(REVISION) 37 | 38 | # 39 | def self.gemspec 40 | new.to_gemspec 41 | end 42 | 43 | # 44 | attr :metadata 45 | 46 | # 47 | def initialize(metadata=nil) 48 | @root_check = false 49 | 50 | if metadata 51 | root_dir = metadata.delete(:root) 52 | if root_dir 53 | @root = root_dir 54 | @root_check = true 55 | end 56 | metadata = nil if metadata.empty? 57 | end 58 | 59 | @metadata = metadata || YAML.load_file(root + '.index') 60 | 61 | if @metadata['revision'].to_i != REVISION 62 | warn "This gemspec exporter was not designed for this revision of index metadata." 63 | end 64 | end 65 | 66 | # 67 | def has_root? 68 | root ? true : false 69 | end 70 | 71 | # 72 | def root 73 | return @root if @root || @root_check 74 | @root_check = true 75 | @root = find_root 76 | end 77 | 78 | # 79 | def manifest 80 | return nil unless root 81 | @manifest ||= Dir.glob(root + 'manifest{,.txt}', File::FNM_CASEFOLD).first 82 | end 83 | 84 | # 85 | def scm 86 | return nil unless root 87 | @scm ||= %w{git hg}.find{ |m| (root + ".#{m}").directory? }.to_sym 88 | end 89 | 90 | # 91 | def files 92 | return [] unless root 93 | @files ||= \ 94 | if manifest 95 | File.readlines(manifest). 96 | map{ |line| line.strip }. 97 | reject{ |line| line.empty? || line[0,1] == '#' } 98 | else 99 | list = [] 100 | Dir.chdir(root) do 101 | FILES.split(/\s+/).each do |pattern| 102 | list.concat(glob(pattern)) 103 | end 104 | OMIT.split(/\s+/).each do |pattern| 105 | list = list - glob(pattern) 106 | end 107 | end 108 | list 109 | end.select{ |path| File.file?(path) }.uniq 110 | end 111 | 112 | # 113 | def glob_files(pattern) 114 | return [] unless root 115 | Dir.chdir(root) do 116 | Dir.glob(pattern).select do |path| 117 | File.file?(path) && files.include?(path) 118 | end 119 | end 120 | end 121 | 122 | def patterns 123 | PATTERNS 124 | end 125 | 126 | def executables 127 | @executables ||= \ 128 | glob_files(patterns[:bin]).map do |path| 129 | File.basename(path) 130 | end 131 | end 132 | 133 | def extensions 134 | @extensions ||= \ 135 | glob_files(patterns[:ext]).map do |path| 136 | File.basename(path) 137 | end 138 | end 139 | 140 | def name 141 | metadata['name'] || metadata['title'].downcase.gsub(/\W+/,'_') 142 | end 143 | 144 | def homepage 145 | page = ( 146 | metadata['resources'].find{ |r| r['type'] =~ /^home/i } || 147 | metadata['resources'].find{ |r| r['name'] =~ /^home/i } || 148 | metadata['resources'].find{ |r| r['name'] =~ /^web/i } 149 | ) 150 | page ? page['uri'] : false 151 | end 152 | 153 | def licenses 154 | metadata['copyrights'].map{ |c| c['license'] }.compact 155 | end 156 | 157 | def require_paths 158 | paths = metadata['paths'] || {} 159 | paths['load'] || ['lib'] 160 | end 161 | 162 | # 163 | # Convert to gemnspec. 164 | # 165 | def to_gemspec 166 | if has_root? 167 | Gem::Specification.new do |gemspec| 168 | to_gemspec_data(gemspec) 169 | to_gemspec_paths(gemspec) 170 | end 171 | else 172 | Gem::Specification.new do |gemspec| 173 | to_gemspec_data(gemspec) 174 | to_gemspec_paths(gemspec) 175 | end 176 | end 177 | end 178 | 179 | # 180 | # Convert pure data settings. 181 | # 182 | def to_gemspec_data(gemspec) 183 | gemspec.name = name 184 | gemspec.version = metadata['version'] 185 | gemspec.summary = metadata['summary'] 186 | gemspec.description = metadata['description'] 187 | 188 | metadata['authors'].each do |author| 189 | gemspec.authors << author['name'] 190 | 191 | if author.has_key?('email') 192 | if gemspec.email 193 | gemspec.email << author['email'] 194 | else 195 | gemspec.email = [author['email']] 196 | end 197 | end 198 | end 199 | 200 | gemspec.licenses = licenses 201 | 202 | requirements = metadata['requirements'] || [] 203 | requirements.each do |req| 204 | next if req['optional'] 205 | next if req['external'] 206 | 207 | name = req['name'] 208 | groups = req['groups'] || [] 209 | 210 | version = gemify_version(req['version']) 211 | 212 | if groups.empty? or groups.include?('runtime') 213 | # populate runtime dependencies 214 | if gemspec.respond_to?(:add_runtime_dependency) 215 | gemspec.add_runtime_dependency(name,*version) 216 | else 217 | gemspec.add_dependency(name,*version) 218 | end 219 | else 220 | # populate development dependencies 221 | if gemspec.respond_to?(:add_development_dependency) 222 | gemspec.add_development_dependency(name,*version) 223 | else 224 | gemspec.add_dependency(name,*version) 225 | end 226 | end 227 | end 228 | 229 | # convert external dependencies into gemspec requirements 230 | requirements.each do |req| 231 | next unless req['external'] 232 | gemspec.requirements << ("%s-%s" % req.values_at('name', 'version')) 233 | end 234 | 235 | gemspec.homepage = homepage 236 | gemspec.require_paths = require_paths 237 | gemspec.post_install_message = metadata['install_message'] 238 | end 239 | 240 | # 241 | # Set gemspec settings that require a root directory path. 242 | # 243 | def to_gemspec_paths(gemspec) 244 | gemspec.files = files 245 | gemspec.extensions = extensions 246 | gemspec.executables = executables 247 | 248 | if Gem::VERSION < '1.7.' 249 | gemspec.default_executable = gemspec.executables.first 250 | end 251 | 252 | gemspec.test_files = glob_files(patterns[:test]) 253 | 254 | unless gemspec.files.include?('.document') 255 | gemspec.extra_rdoc_files = glob_files(patterns[:doc]) 256 | end 257 | end 258 | 259 | # 260 | # Return a copy of this file. This is used to generate a local 261 | # .gemspec file that can automatically read the index file. 262 | # 263 | def self.source_code 264 | File.read(__FILE__) 265 | end 266 | 267 | private 268 | 269 | def find_root 270 | root_files = patterns[:root] 271 | if Dir.glob(root_files).first 272 | Pathname.new(Dir.pwd) 273 | elsif Dir.glob("../#{root_files}").first 274 | Pathname.new(Dir.pwd).parent 275 | else 276 | #raise "Can't find root of project containing `#{root_files}'." 277 | warn "Can't find root of project containing `#{root_files}'." 278 | nil 279 | end 280 | end 281 | 282 | def glob(pattern) 283 | if File.directory?(pattern) 284 | Dir.glob(File.join(pattern, '**', '*')) 285 | else 286 | Dir.glob(pattern) 287 | end 288 | end 289 | 290 | def gemify_version(version) 291 | case version 292 | when /^(.*?)\+$/ 293 | ">= #{$1}" 294 | when /^(.*?)\-$/ 295 | "< #{$1}" 296 | when /^(.*?)\~$/ 297 | "~> #{$1}" 298 | else 299 | version 300 | end 301 | end 302 | 303 | end 304 | 305 | end 306 | 307 | Indexer::GemspecExporter.gemspec --------------------------------------------------------------------------------