wriggle 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ coverage
2
+ rdoc
3
+ pkg
4
+ .yardoc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 rspeicher
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.md ADDED
@@ -0,0 +1,39 @@
1
+ # Wriggle
2
+
3
+ A simple directory crawler DSL.
4
+
5
+ ## Usage
6
+
7
+ require 'wriggle'
8
+
9
+ wriggle '/path/to/files' do
10
+
11
+ # Build an array of Ruby code files
12
+ ruby_files = []
13
+ file :rb do |path|
14
+ ruby_files << path
15
+ end
16
+
17
+ # Build an array of video files
18
+ video_files = []
19
+ file %w(mpg mpeg wmv avi mkv) do |path|
20
+ video_files << path
21
+ end
22
+
23
+ # Delete directories that are empty
24
+ directory do |path|
25
+ Dir.rmdir(path) unless Dir.entries(path).length > 2
26
+ end
27
+ end
28
+
29
+ ## Note on Patches/Pull Requests
30
+
31
+ * Fork
32
+ * Code
33
+ * Commit
34
+ * Push
35
+ * Pull Request
36
+
37
+ ## Copyright
38
+
39
+ Copyright (c) 2010 rspeicher. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "wriggle"
8
+ gem.summary = %Q{A simple directory crawler DSL.}
9
+ gem.description = %Q{A simple directory crawler DSL.}
10
+ gem.email = "rspeicher@gmail.com"
11
+ gem.homepage = "http://github.com/tsigo/wriggle"
12
+ gem.authors = ["rspeicher"]
13
+ gem.add_development_dependency "rspec", "~> 1.3.0"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :spec => :check_dependencies
33
+
34
+ task :default => :spec
35
+
36
+ begin
37
+ require 'yard'
38
+ YARD::Rake::YardocTask.new
39
+ rescue LoadError
40
+ task :yardoc do
41
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
42
+ end
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/wriggle.rb ADDED
@@ -0,0 +1,112 @@
1
+ require 'find'
2
+
3
+ # = Wriggle
4
+ #
5
+ # A simple directory crawler DSL.
6
+ #
7
+ # == Usage
8
+ #
9
+ # require 'wriggle'
10
+ #
11
+ # wriggle '/path/to/files' do
12
+ #
13
+ # # Build an array of Ruby code files
14
+ # ruby_files = []
15
+ # file :rb do |path|
16
+ # ruby_files << path
17
+ # end
18
+ #
19
+ # # Build an array of video files
20
+ # video_files = []
21
+ # file %w(mpg mpeg wmv avi mkv) do |path|
22
+ # video_files << path
23
+ # end
24
+ #
25
+ # # Delete directories that are empty
26
+ # directory do |path|
27
+ # Dir.rmdir(path) unless Dir.entries(path).length > 2
28
+ # end
29
+ # end
30
+ module Wriggle
31
+ # Crawl the given +path+
32
+ #
33
+ # @raise ArgumentError Given path does not exist or is not a directory
34
+ def wriggle(path, &block)
35
+ raise ArgumentError, "#{path} does not exist or is not a directory" unless File.directory?(path)
36
+
37
+ Wriggle.new(path, &block)
38
+ end
39
+
40
+ class Wriggle
41
+ attr_accessor :root, :file_blocks, :directory_blocks
42
+
43
+ def initialize(root, &block)
44
+ @root = root
45
+ @file_blocks = []
46
+ @directory_blocks = []
47
+
48
+ crawl(&block)
49
+ end
50
+
51
+ def crawl(&block)
52
+ Find.find(root) do |current|
53
+ instance_eval(&block)
54
+
55
+ if File.file?(current)
56
+ dispatch_file(current)
57
+ elsif File.directory?(current)
58
+ dispatch_directory(current)
59
+ end
60
+ end
61
+ end
62
+
63
+ # Define a block to be called when a file is encountered
64
+ #
65
+ # Provide one or more extensions to limit the files yielded.
66
+ #
67
+ # == Examples:
68
+ # file :rb { |file| ... }
69
+ # file :rb, :rdoc { |file| ... }
70
+ # file %w(mpeg mpeg wmv avi mkv) { |file| ... }
71
+ #
72
+ # @raise ArgumentError When no block provided
73
+ def file(*extensions, &block)
74
+ raise ArgumentError, "a block is required" unless block_given?
75
+ file_blocks << {:ext => extensions.flatten, :block => block}
76
+ end
77
+
78
+ # Define a block to be called when a directory is encountered
79
+ #
80
+ # @raise ArgumentError When no block provided
81
+ def directory(&block)
82
+ raise ArgumentError, "a block is required" unless block_given?
83
+ directory_blocks << {:block => block}
84
+ end
85
+
86
+ private
87
+
88
+ def dispatch_file(path)
89
+ extension = File.extname(path)
90
+
91
+ file_blocks.each do |group|
92
+ if group[:ext].empty?
93
+ group[:block].call(path)
94
+ else
95
+ # Requested specific extensions only
96
+ # Check if any of the extensions match the current file's extension (with or without the period)
97
+ if group[:ext].any? { |v| v.to_s == extension or v.to_s == extension[1..-1] }
98
+ group[:block].call(path)
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ def dispatch_directory(path)
105
+ directory_blocks.each do |group|
106
+ group[:block].call(path)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ self.send(:include, Wriggle)
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=progress
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'wriggle'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe Wriggle do
4
+ # 1.8.7's Find uses relative paths, while 1.9.2's uses absolutes,
5
+ # so we'll call expand_path and make sure our specs use this method
6
+ def valid_dir
7
+ File.expand_path(File.dirname(__FILE__))
8
+ end
9
+
10
+ it "should require a path argument" do
11
+ lambda { wriggle() {} }.should raise_error(ArgumentError)
12
+ end
13
+
14
+ it "should raise an ArgumentError when given an invalid path" do
15
+ lambda { wriggle('/path/to/nothing') {} }.should raise_error(ArgumentError, /does not exist/)
16
+ end
17
+
18
+ it "should raise an ArgumentError when given a non-directory" do
19
+ lambda { wriggle(__FILE__) {} }.should raise_error(ArgumentError, /is not a directory/)
20
+ end
21
+
22
+ context "given a valid path" do
23
+ describe "#file" do
24
+ it "should raise an ArgumentError when not given a block" do
25
+ lambda { wriggle(valid_dir) { file } }.should raise_error(ArgumentError, /a block is required/)
26
+ end
27
+
28
+ it "should not include directories when only a file block is provided" do
29
+ actual = []
30
+ wriggle(valid_dir) do
31
+ file do |path|
32
+ actual << path
33
+ end
34
+ end
35
+
36
+ actual.should include("#{valid_dir}/spec.opts")
37
+ actual.should include("#{valid_dir}/wriggle_spec.rb")
38
+ actual.should_not include("#{valid_dir}/spec")
39
+ end
40
+
41
+ it "should only include files of the type specified" do
42
+ actual = []
43
+ wriggle(valid_dir) do
44
+ file :rb do |path|
45
+ actual << path
46
+ end
47
+ end
48
+
49
+ actual.should include("#{valid_dir}/wriggle_spec.rb")
50
+ actual.should_not include("#{valid_dir}/spec.opts")
51
+ actual.should_not include("#{valid_dir}")
52
+ end
53
+ end
54
+
55
+ describe "#directory" do
56
+ it "should raise an ArgumentError when not given a block" do
57
+ lambda { wriggle(valid_dir) { directory } }.should raise_error(ArgumentError, /a block is required/)
58
+ end
59
+
60
+ it "should not include files when only a directory block is provided" do
61
+ actual = []
62
+ wriggle(valid_dir) do
63
+ directory do |path|
64
+ actual << path
65
+ end
66
+ end
67
+
68
+ actual.should include("#{valid_dir}")
69
+ actual.should_not include("#{valid_dir}/wriggle_spec.rb")
70
+ end
71
+ end
72
+ end
73
+ end
data/wriggle.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wriggle}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["rspeicher"]
12
+ s.date = %q{2010-10-09}
13
+ s.description = %q{A simple directory crawler DSL.}
14
+ s.email = %q{rspeicher@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/wriggle.rb",
27
+ "spec/spec.opts",
28
+ "spec/spec_helper.rb",
29
+ "spec/wriggle_spec.rb",
30
+ "wriggle.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/tsigo/wriggle}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{A simple directory crawler DSL.}
37
+ s.test_files = [
38
+ "spec/spec_helper.rb",
39
+ "spec/wriggle_spec.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, ["~> 1.3.0"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 1.3.0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, ["~> 1.3.0"])
53
+ end
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wriggle
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - rspeicher
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-09 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 0
34
+ version: 1.3.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: A simple directory crawler DSL.
38
+ email: rspeicher@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.md
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/wriggle.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - spec/wriggle_spec.rb
57
+ - wriggle.gemspec
58
+ has_rdoc: true
59
+ homepage: http://github.com/tsigo/wriggle
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A simple directory crawler DSL.
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/wriggle_spec.rb