├── var ├── name ├── version ├── created ├── license ├── organization ├── title ├── authors ├── copyrights ├── repositories ├── summary ├── requirements ├── resources └── description ├── pkg └── .gitignore ├── Gemfile ├── .gitignore ├── .travis.yml ├── Rakefile ├── MANIFEST ├── HISTORY.md ├── Assembly ├── LICENSE ├── .index ├── test └── test_enumargs.rb ├── README.md ├── lib └── enumargs.rb └── .gemspec /var/name: -------------------------------------------------------------------------------- 1 | enumargs 2 | -------------------------------------------------------------------------------- /var/version: -------------------------------------------------------------------------------- 1 | 2.0.0 2 | -------------------------------------------------------------------------------- /pkg/.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | -------------------------------------------------------------------------------- /var/created: -------------------------------------------------------------------------------- 1 | 2004-08-01 2 | -------------------------------------------------------------------------------- /var/license: -------------------------------------------------------------------------------- 1 | BSD-2-Clause 2 | -------------------------------------------------------------------------------- /var/organization: -------------------------------------------------------------------------------- 1 | rubyworks 2 | -------------------------------------------------------------------------------- /var/title: -------------------------------------------------------------------------------- 1 | Enumerable::Arguments 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | gemspec 3 | -------------------------------------------------------------------------------- /var/authors: -------------------------------------------------------------------------------- 1 | --- 2 | - 7rans 3 | -------------------------------------------------------------------------------- /var/copyrights: -------------------------------------------------------------------------------- 1 | --- 2 | - 2004 Rubyworks (BSD-2-Clause) 3 | -------------------------------------------------------------------------------- /var/repositories: -------------------------------------------------------------------------------- 1 | --- 2 | upstream: git://github.com/rubyworks/enumargs.git 3 | -------------------------------------------------------------------------------- /var/summary: -------------------------------------------------------------------------------- 1 | When your #each has parameters and you need Enumerable too. 2 | -------------------------------------------------------------------------------- /var/requirements: -------------------------------------------------------------------------------- 1 | --- 2 | - minitest (test) 3 | - rake (build) 4 | - detroit (build) 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .fire/digest 2 | .yardoc 3 | doc 4 | log 5 | tmp 6 | web 7 | *.lock 8 | work/sandbox 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | script: "rake test" 3 | rvm: 4 | - 1.8.7 5 | - 1.9.2 6 | - 1.9.3 7 | - rbx 8 | - rbx-19mode 9 | - jruby 10 | - jruby-19mode 11 | - ree 12 | 13 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'rake/testtask' 4 | 5 | Rake::TestTask.new do |t| 6 | t.pattern = "test/test_*.rb" 7 | end 8 | 9 | task :default => [:test] 10 | 11 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | #!mast .index .ruby .yardopts bin demo lib share test *.md *.rdoc *.txt [A-Z][A-Z]* 2 | .index 3 | lib/enumargs.rb 4 | test/test_enumargs.rb 5 | HISTORY.md 6 | README.md 7 | LICENSE 8 | -------------------------------------------------------------------------------- /var/resources: -------------------------------------------------------------------------------- 1 | --- 2 | home: http://rubyworks.github.com/enumargs 3 | code: http://github.com/rubyworks/enumargs 4 | bugs: http://github.com/rubyworks/enumargs/issues 5 | chat: http://chat.us.freenode.net/rubyworks 6 | 7 | -------------------------------------------------------------------------------- /HISTORY.md: -------------------------------------------------------------------------------- 1 | # HISTORY 2 | 3 | ## 1.0.0 / 2009-07-16 4 | 5 | This is the initial release of Enumerable::Arguments, 6 | ported from the Ruby Facets' library EnumerablePass. 7 | 8 | Changes: 9 | 10 | * Happy Birthday! 11 | 12 | -------------------------------------------------------------------------------- /var/description: -------------------------------------------------------------------------------- 1 | This is a simple reimplementation of the core Enumerable module 2 | to allow its methods to take and pass-on arbitrary arguments to the 3 | underlying #each call. This library uses Enumerator and scans 4 | Enumerable so it can alwasy stay in sync. 5 | -------------------------------------------------------------------------------- /Assembly: -------------------------------------------------------------------------------- 1 | --- 2 | gem: 3 | gemspec: .gemspec 4 | 5 | github: 6 | gh_pages: web 7 | 8 | dnote: 9 | title: Source Notes 10 | output: log/notes.html 11 | labels: ~ 12 | 13 | #yard: 14 | # yardopts: true 15 | # priority: 2 16 | 17 | #vclog: 18 | # output: 19 | # - log/history.html 20 | # - log/changes.html 21 | 22 | email: 23 | file: ~ 24 | subject: ~ 25 | mailto: 26 | - ruby-talk@ruby-lang.org 27 | - rubyworks-mailinglist@googlegroups.com 28 | #parts: [readme] 29 | 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD-2-Clause License 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 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 14 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 15 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 16 | 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 | -------------------------------------------------------------------------------- /.index: -------------------------------------------------------------------------------- 1 | --- 2 | type: ruby 3 | revision: 2013 4 | sources: 5 | - var 6 | authors: 7 | - name: 7rans 8 | email: transfire@gmail.com 9 | organizations: [] 10 | requirements: 11 | - groups: 12 | - test 13 | development: true 14 | name: minitest 15 | - groups: 16 | - build 17 | development: true 18 | name: rake 19 | - groups: 20 | - build 21 | development: true 22 | name: detroit 23 | conflicts: [] 24 | alternatives: [] 25 | resources: 26 | - type: home 27 | uri: http://rubyworks.github.com/enumargs 28 | label: Homepage 29 | - type: code 30 | uri: http://github.com/rubyworks/enumargs 31 | label: Source Code 32 | - type: bugs 33 | uri: http://github.com/rubyworks/enumargs/issues 34 | label: Issue Tracker 35 | - type: chat 36 | uri: http://chat.us.freenode.net/rubyworks 37 | label: IRC Channel 38 | repositories: 39 | - name: upstream 40 | scm: git 41 | uri: git://github.com/rubyworks/enumargs.git 42 | categories: [] 43 | paths: 44 | load: 45 | - lib 46 | copyrights: 47 | - holder: Rubyworks 48 | year: '2004' 49 | license: BSD-2-Clause 50 | created: '2004-08-01' 51 | summary: ! 'When your #each has parameters and you need Enumerable too.' 52 | title: Enumerable::Arguments 53 | version: 2.0.0 54 | name: enumargs 55 | description: ! 'This is a simple reimplementation of the core Enumerable module 56 | 57 | to allow its methods to take and pass-on arbitrary arguments to the 58 | 59 | underlying #each call. This library uses Enumerator and scans 60 | 61 | Enumerable so it can alwasy stay in sync.' 62 | date: '2012-12-21' 63 | -------------------------------------------------------------------------------- /test/test_enumargs.rb: -------------------------------------------------------------------------------- 1 | require 'enumargs' 2 | require 'test/unit' 3 | 4 | # fixture 5 | 6 | class PlusArray 7 | include Enumerable::Arguments 8 | def initialize(arr) 9 | @arr = arr 10 | end 11 | def each(n=0) 12 | @arr.each{ |e| yield(e+n) } 13 | end 14 | end 15 | 16 | class TC_Enumerable < Test::Unit::TestCase 17 | 18 | def test_collect 19 | t = PlusArray.new([1,2,3]) 20 | assert_equal( [5,6,7], t.collect(4){ |e| e } ) 21 | end 22 | 23 | #def test_each_slice 24 | # t = PlusArray.new([1,2,3,4]) 25 | # a = [] 26 | # t.each_slice(2,4){ |e,f| a << [e,f] } 27 | # assert_equal( [[5,6],[7,8]], a ) 28 | #end 29 | 30 | #def test_find 31 | # t = PlusArray.new([1,2,3,4]) 32 | # f = t.find(2, :ifnone=>lambda{:NOPE}) { |a| a == 10 } 33 | # assert_equal(:NOPE, f) 34 | #end 35 | 36 | def test_grep 37 | # TODO 38 | end 39 | 40 | def test_to_a 41 | t = PlusArray.new([1,2,3]) 42 | assert_equal( [5,6,7], t.to_a(4) ) 43 | end 44 | 45 | def test_min 46 | t = PlusArray.new([1,2,3]) 47 | assert_equal( 5, t.min(4) ) 48 | end 49 | 50 | def test_max 51 | t = PlusArray.new([1,2,3]) 52 | assert_equal( 7, t.max(4) ) 53 | end 54 | 55 | def test_include? 56 | t = PlusArray.new([1,2,3]) 57 | assert( t.include?(7,4) ) 58 | end 59 | 60 | def test_select 61 | t = PlusArray.new([1,2,3]) 62 | assert_equal( [6], t.select(4){ |x| x == 6 } ) 63 | end 64 | 65 | def test_reject 66 | t = PlusArray.new([1,2,3]) 67 | assert_equal( [5,7], t.reject(4){ |x| x == 6 } ) 68 | end 69 | 70 | end 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Enumerable::Arguments 2 | 3 | [Website](http://rubyworks.github.com/enumargs) / 4 | [Report Issue](http://github.com/rubyworks/enumargs/issues) / 5 | [Gem Package](http://rubygems.org/gems/enumargs) / 6 | [Source Code](http://github.com/rubyworks/enumargs) 7 | ( [![Build Status](https://secure.travis-ci.org/rubyworks/enumargs.png)](http://travis-ci.org/rubyworks/enumargs) ) 8 | 9 | 10 | This is a simple reimplementation of the core Enumerable module 11 | to allow its methods to take and pass on arbitrary arguments to the 12 | underlying `#each` call. This library uses Enumerator and scans 13 | Enumerable to dynamically construct its methods, so it should always 14 | be in sync with the current implementation of Ruby's Enumerable module. 15 | 16 | 17 | ## Limitations 18 | 19 | * Methods `#inject` and `#zip` cannot use pass-on parameters, b/c they already have their own. 20 | * Methods `#find` and `#detect` have a modified interface so they can support pass-on parameters. 21 | 22 | 23 | ## Synopsis 24 | 25 | ```ruby 26 | class T 27 | include Enumerable::Arguments 28 | 29 | def initialize(arr) 30 | @arr = arr 31 | end 32 | 33 | def each(n) 34 | arr.each{ |e| yield(e+n) } 35 | end 36 | end 37 | 38 | t = T.new([1,2,3]) 39 | 40 | t.map(4) #=> [5,6,7] 41 | ``` 42 | 43 | ## Installation 44 | 45 | To install with RubyGems simply open a console and type: 46 | 47 | gem install enumargs 48 | 49 | Local installation requires Setup.rb (gem install setup), 50 | then download the tarball package and type: 51 | 52 | tar -xvzf enumargs-2.0.0.tgz 53 | cd enumargs-2.0.0 54 | sudo setup.rb all 55 | 56 | Windows users use 'ruby setup.rb all'. 57 | 58 | 59 | ## Copyrights 60 | 61 | Enumargs is copyrighted open source software. 62 | 63 | Copyright (c) 2004 Rubyworks 64 | 65 | This program is distributed under the terms of the **BSD-2-Clause** license. 66 | 67 | See LICENSE file for details. 68 | -------------------------------------------------------------------------------- /lib/enumargs.rb: -------------------------------------------------------------------------------- 1 | begin 2 | require 'enumerator' 3 | rescue LoadError 4 | end 5 | 6 | module Enumerable 7 | 8 | # TODO: The name of the module never felt quite right. 9 | # Other options are: 10 | # * `EnumerableWithArguments` 11 | # * `Enumerable::WithArguments` 12 | # * `ParametricEnumerable` 13 | # Although the later would lead to name change for the project too. 14 | 15 | # This is a simple reimplementation of the core Enumerable module 16 | # to allow the methods to take and pass-on arbitrary arguments to the 17 | # underlying #each call. This library uses Enumerator and scans 18 | # Enumerable so it can always stay in sync with the current module. 19 | # 20 | # Note that any Enumerable method with a negative arity cannot pass arguments 21 | # due to ambiguity in the argument count. So the methods #inject and #zip 22 | # do NOT work this way, but simply work as they do in Enumerable. 23 | # The method #find (and #detect) though has been made to work by removing 24 | # its rarely used optional parameter and providing instead an optional 25 | # keyword parameter (:ifnone => ...). Please keep these difference in mind. 26 | # 27 | # require 'enumargs' 28 | # 29 | # class T 30 | # include Enumerable::Arguments 31 | # 32 | # def initialize(arr) 33 | # @arr = arr 34 | # end 35 | # def each(n) 36 | # arr.each{ |e| yield(e+n) } 37 | # end 38 | # end 39 | # 40 | # t = T.new([1,2,3]) 41 | # t.collect(4) 42 | # #=> [5,6,7] 43 | # 44 | module Arguments 45 | 46 | def self.wrap_enumerable_method( methodname ) 47 | 48 | m = methodname 49 | meth = Enumerable.instance_method(m) 50 | arity = meth.arity 51 | 52 | case arity <=> 0 53 | when 0 54 | class_eval %{ 55 | def #{m}( *args, &yld ) 56 | enum_for(:each, *args).#{m}( &yld ) 57 | end 58 | } 59 | when 1 60 | class_eval %{ 61 | def #{m}( *args, &yld ) 62 | args, each_args = args[0...#{arity}], args[#{arity}..-1] 63 | enum_for(:each, *each_args).#{m}( *args, &yld ) 64 | end 65 | } 66 | else 67 | class_eval %{ 68 | def #{m}( *args, &yld ) 69 | enum_for(:each).#{m}( *args, &yld ) 70 | end 71 | } 72 | end 73 | end 74 | 75 | Enumerable.instance_methods(false).each do |m| 76 | wrap_enumerable_method( m ) 77 | end 78 | 79 | # Make exception for #find (a negative arity method) to accept 80 | # keyword argument. 81 | # 82 | # ObjectSpace.find(Class, :ifnone=>lambda{1}) { |e| ... } 83 | # ObjectSpace.find(Class, :ifnone=>lambda{1}) { |e| ... } 84 | # 85 | def find(*args, &yld) # future use **keys ? 86 | if Hash === args.last and args.last.key?(:ifnone) 87 | ifnone = args.last.delete(:ifnone) 88 | args.pop if args.last.empty? 89 | enum_for(:each, *args).find( ifnone, &yld ) 90 | else 91 | enum_for(:each, *args).find( &yld ) 92 | end 93 | end 94 | alias_method :detect, :find 95 | 96 | # Support for #to_a. 97 | # 98 | def to_a(*args) 99 | enum_for(:each, *args).to_a 100 | end 101 | 102 | end 103 | end 104 | 105 | 106 | =begin OLD CODE 107 | module EnumerableArgs 108 | 109 | def collect(*args) # :yield: 110 | a = [] 111 | each(*args){ |n| a << yield(n) } 112 | a 113 | end 114 | alias_method( :map, :collect ) 115 | 116 | def detect(*args) # :yield: 117 | each(*args){ |n| return n if yield(n) } 118 | nil 119 | end 120 | alias_method( :find, :detect ) 121 | 122 | def each_with_index(*args) 123 | i=0 124 | each(*args){ |*n| n << i; yield(*n); i+=1 } 125 | self 126 | end 127 | 128 | def to_a(*args) 129 | a = [] 130 | each(*args){ |n| a << n } 131 | a 132 | end 133 | alias_method( :entries, :to_a ) 134 | 135 | # An additional method not part of standard Enumerable. 136 | # The regular version of this method can be found in Facets, 137 | # but it is a bit more advanced then this one. 138 | # At some point they need to be put into sync. 139 | def each_slice(*args, &yld) 140 | a = []; s = [] 141 | ar = yld.arity.abs 142 | each(*args){ |n| 143 | s << n 144 | if s.length >= ar 145 | yld.call(*s) 146 | s = [] 147 | end 148 | } 149 | a 150 | end 151 | alias_method( :each_by, :each_slice ) 152 | 153 | def select(*args) # :yield: 154 | a = [] 155 | each(*args){ |n| a << n if yield(n) } 156 | a 157 | end 158 | alias_method( :find_all, :select ) 159 | 160 | def grep(pattern, *args) 161 | a = [] 162 | each(*args){ |n| a << (block_given? ? yield(n) : n) if pattern === n } 163 | a 164 | end 165 | 166 | def include?(anObj, *args) 167 | each(*args){ |n| return true if anObj == n } 168 | false 169 | end 170 | alias_method( :member?, :include? ) 171 | 172 | def max(*args) 173 | to_a(*args).max 174 | end 175 | 176 | def min(*args) 177 | to_a(*args).min 178 | end 179 | 180 | def reject(*args) 181 | a = [] 182 | each(*args){ |n| a << n if ! yield(n) } 183 | a 184 | end 185 | 186 | def sort(*args) 187 | # TODO 188 | end 189 | 190 | end 191 | =end 192 | 193 | # Copyright (c) 2004 Rubyworks (BSD-2-Clause) 194 | -------------------------------------------------------------------------------- /.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 --------------------------------------------------------------------------------