wildcard 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +26 -0
  3. data/Rakefile +52 -0
  4. data/lib/wildcard.rb +36 -0
  5. data/spec/wildcard_spec.rb +61 -0
  6. metadata +67 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [maiha@wota.jp]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,26 @@
1
+ Wildcard
2
+ ========
3
+
4
+ A ruby library to expand wildcard string like shell command line
5
+
6
+
7
+ Usage
8
+ =====
9
+
10
+ Wildcard.expand("foo.{rb,yml}")
11
+ => ["foo.rb", "foo.yml"]
12
+
13
+ Wildcard.expand("file0{08..11}.dat")
14
+ => ["file008.dat", "file009.dat", "file010.dat", "file011.dat"]
15
+
16
+
17
+ # same as "% wget http://.../dance{01..16}.flv" on shell
18
+ Wildcard.expand("http://.../dance{01..16}.flv").each do |url|
19
+ system("wget #{url}")
20
+ end
21
+
22
+
23
+ Author
24
+ ======
25
+
26
+ maiha@wota.jp
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "wildcard"
5
+ AUTHOR = "maiha"
6
+ EMAIL = "maiha@wota.jp"
7
+ HOMEPAGE = "http://github.com/maiha/wildcard"
8
+ SUMMARY = "A ruby library to expand wildcard string like shell command line"
9
+ GEM_VERSION = "0.0.1"
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.rubyforge_project = 'asakusarb'
13
+ s.executables = []
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.require_path = 'lib'
25
+ # s.add_dependency('dsl_accessor', '>= 0.4.0')
26
+ s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ end
32
+
33
+ desc "Install the gem"
34
+ task :install do
35
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
36
+ end
37
+
38
+ desc "Uninstall the gem"
39
+ task :uninstall do
40
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
41
+ end
42
+
43
+ desc "Create a gemspec file"
44
+ task :gemspec do
45
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
46
+ file.puts spec.to_ruby
47
+ end
48
+ end
49
+
50
+ require 'spec/rake/spectask'
51
+ desc 'Default: run spec examples'
52
+ task :default => 'spec'
@@ -0,0 +1,36 @@
1
+ class Wildcard
2
+ def self.expand(*args)
3
+ new(*args).expand
4
+ end
5
+
6
+ def initialize(text)
7
+ @text = Array(text)
8
+ end
9
+
10
+ def expand
11
+ texts = @text
12
+ texts = texts.map{|i| expand_range(i)}.flatten.uniq
13
+ texts = texts.map{|i| expand_selection(i)}.flatten.uniq
14
+ texts
15
+ end
16
+
17
+ private
18
+ def expand_range(text)
19
+ if text =~ /\{(.*?)\.\.(.*?)\}/
20
+ ($1..$2).map{|i| expand_range("#{$`}#{i}#{$'}")}
21
+ else
22
+ text
23
+ end
24
+ end
25
+
26
+ def expand_selection(text)
27
+ if text =~ /\{(.*?,.*?)\}/
28
+ b,text,a = $`,$1,$'
29
+ array = text.split(/,/).map{|i| expand_range("#{b}#{i}#{a}") }
30
+ array << "#{b}#{a}" if text[-1] == ?,
31
+ array
32
+ else
33
+ text
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec'
2
+ require 'rr'
3
+ Spec::Runner.configure do |config|
4
+ config.mock_with RR::Adapters::Rspec
5
+ end
6
+
7
+ require File.join(File.dirname(__FILE__), '../lib/wildcard')
8
+
9
+ module Spec::Example::Subject::ExampleGroupMethods
10
+ def expand(src, expected, &block)
11
+ it "expand(#{src.inspect})" do
12
+ Wildcard.new(src).expand.should == (expected || block.call)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe Wildcard do
18
+ ######################################################################
19
+ ### api
20
+
21
+ it "should provide .expand" do
22
+ Wildcard.should respond_to(:expand)
23
+ end
24
+
25
+ describe ".expand" do
26
+ it "should delegate to #expand" do
27
+ mock.instance_of(Wildcard).expand
28
+ Wildcard.expand('')
29
+ end
30
+ end
31
+
32
+ it "should provide #expand" do
33
+ Wildcard.new('').should respond_to(:expand)
34
+ end
35
+
36
+ ######################################################################
37
+ ### expand
38
+
39
+ # accept string
40
+ expand "a", ["a"]
41
+
42
+ # accept array
43
+ expand ["a","b"], ["a","b"]
44
+
45
+ # range
46
+ expand "{1..3}", ["1","2","3"]
47
+ expand "a{1..3}", ["a1","a2","a3"]
48
+ expand "a{1..3}b", ["a1b","a2b","a3b"]
49
+ expand "{09..11}", ["09", "10", "11"]
50
+ expand "a{09..11}b", ["a09b", "a10b", "a11b"]
51
+
52
+ # selection
53
+ expand "{1,3,5}", ["1","3","5"]
54
+ expand "a{,x,y}b", ["ab", "axb", "ayb"]
55
+ expand "a{x,,y}b", ["axb", "ab", "ayb"]
56
+ expand "a{1,3,x,y,}b", ["a1b", "a3b", "axb", "ayb", "ab"]
57
+ expand "a{,x,,,y,,}b", ["ab", "axb", "ayb"]
58
+
59
+ # complexed
60
+ expand "X{a..c}Y{0,5}Z", ["XaY0Z", "XaY5Z", "XbY0Z", "XbY5Z", "XcY0Z", "XcY5Z"]
61
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wildcard
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - maiha
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-02 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A ruby library to expand wildcard string like shell command line
22
+ email: maiha@wota.jp
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - MIT-LICENSE
30
+ files:
31
+ - MIT-LICENSE
32
+ - README
33
+ - Rakefile
34
+ - lib/wildcard.rb
35
+ - spec/wildcard_spec.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/maiha/wildcard
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project: asakusarb
62
+ rubygems_version: 1.3.6
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: A ruby library to expand wildcard string like shell command line
66
+ test_files: []
67
+