dir_processor 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.
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +5 -0
- data/dir_processor.gemspec +26 -0
- data/lib/dir_processor/version.rb +3 -0
- data/lib/dir_processor.rb +133 -0
- data/spec/example_spec.rb +58 -0
- data/spec/support/file_utils.rb +14 -0
- data/spec/support/require_self.rb +1 -0
- data/spec/support/spec_helper.rb +19 -0
- metadata +133 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alexander K
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# DirProcessor
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dir_processor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dir_processor
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dir_processor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dir_processor"
|
8
|
+
spec.version = DirProcessor::VERSION
|
9
|
+
spec.authors = ["Alexander K"]
|
10
|
+
spec.email = ["xpyro@ya.ru"]
|
11
|
+
spec.description = %q{ dsl to define processor for directories/files structure }.strip
|
12
|
+
spec.summary = %q{ dsl to define processor for directories/files structure }.strip
|
13
|
+
spec.homepage = "https://github.com/sowcow/dir_processor"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_runtime_dependency "def_dsl"
|
26
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require "dir_processor/version"
|
2
|
+
require 'def_dsl'
|
3
|
+
|
4
|
+
def DirProcessor &block
|
5
|
+
DirProcessor.new &block
|
6
|
+
end
|
7
|
+
|
8
|
+
module DirProcessor
|
9
|
+
def new &block
|
10
|
+
DirProcessor.new &block
|
11
|
+
end
|
12
|
+
module_function :new
|
13
|
+
|
14
|
+
class DirProcessor
|
15
|
+
|
16
|
+
module RemembersBlock
|
17
|
+
# def instance_eval █ @block = block end
|
18
|
+
def feed_block █ @block = block end
|
19
|
+
end
|
20
|
+
|
21
|
+
module AcceptPattern
|
22
|
+
def accept? file
|
23
|
+
name = File.basename file
|
24
|
+
regexp_pattern =~ name
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def regexp_pattern
|
29
|
+
/^#{ Regexp.escape(pattern).gsub('\*','.*') }$/
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# module UseBlockAndPattern
|
34
|
+
# def feed given
|
35
|
+
# @block.call given if accept? given
|
36
|
+
# end
|
37
|
+
# private
|
38
|
+
# end
|
39
|
+
|
40
|
+
# DSL definition
|
41
|
+
class AFile < Struct.new :pattern
|
42
|
+
@dsl = :file
|
43
|
+
include RemembersBlock, AcceptPattern #UseBlockAndPattern
|
44
|
+
def feed given
|
45
|
+
@block.call given if accept? given
|
46
|
+
end
|
47
|
+
end
|
48
|
+
class ADir < Struct.new :pattern
|
49
|
+
@dsl = :dir
|
50
|
+
ADir = ADir
|
51
|
+
AFile = AFile
|
52
|
+
include AcceptPattern
|
53
|
+
|
54
|
+
def feed dir
|
55
|
+
return unless accept? dir
|
56
|
+
|
57
|
+
# Copy/Paste:
|
58
|
+
crap = proc { |x| x == '.' || x == '..' }
|
59
|
+
all = Dir.entries(dir).reject(&crap).group_by { |x| File.directory?(x) ? :dirs : :files }
|
60
|
+
all_files = all[:files]
|
61
|
+
all_dirs = all[:dirs]
|
62
|
+
|
63
|
+
so2(:file).each do |processor|
|
64
|
+
(all[:files] || []).each { |file| processor.feed long_name(dir,file) }
|
65
|
+
end
|
66
|
+
so2(:dir).each do |processor|
|
67
|
+
(all[:dirs] || []).each { |f| processor.feed long_name(dir,f) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
def long_name dir,file
|
73
|
+
File.join(dir,file).sub /^\.\//, ''
|
74
|
+
end
|
75
|
+
end
|
76
|
+
extend DefDSL
|
77
|
+
def_dsl ADir, AFile
|
78
|
+
|
79
|
+
def initialize &block
|
80
|
+
# extend EasyDsl
|
81
|
+
# def_dsl self.class
|
82
|
+
raise unless block
|
83
|
+
# dir '.', &block
|
84
|
+
# @dir = so1 :dir
|
85
|
+
instance_eval &block #yield
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
def feed dir
|
90
|
+
# return unless accept? dir
|
91
|
+
|
92
|
+
# Copy/Paste:
|
93
|
+
crap = proc { |x| x == '.' || x == '..' }
|
94
|
+
all = Dir.entries(dir).reject(&crap).group_by { |x| File.directory?(x) ? :dirs : :files }
|
95
|
+
all_files = all[:files]
|
96
|
+
all_dirs = all[:dirs]
|
97
|
+
|
98
|
+
so2(:file).each do |processor|
|
99
|
+
(all[:files] || []).each { |file| processor.feed long_name(dir,file) }
|
100
|
+
end
|
101
|
+
so2(:dir).each do |processor|
|
102
|
+
(all[:dirs] || []).each { |f| processor.feed long_name(dir,f) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
def long_name dir,file
|
108
|
+
File.join(dir,file).sub /^\.\//, ''
|
109
|
+
end
|
110
|
+
|
111
|
+
# def feed dir
|
112
|
+
# # @so = @dir.feed(dir).send(:so)
|
113
|
+
# # p 123
|
114
|
+
# # p @so = ADir.new('.').feed(dir).send(:so)
|
115
|
+
# # ; crap = proc { |x| x == '.' || x == '..' }
|
116
|
+
# # all = Dir.entries(dir).reject(&crap).group_by { |x| File.directory?(x) ? :dirs : :files }
|
117
|
+
# # all_files = all[:files]
|
118
|
+
# # all_dirs = all[:dirs]
|
119
|
+
|
120
|
+
# # so2(:file).each do |processor|
|
121
|
+
# # (all[:files] || []).each { |file| processor.feed long_name(dir,file) }
|
122
|
+
# # end
|
123
|
+
# # so2(:dir).each do |processor|
|
124
|
+
# # (all[:dirs] || []).each { |f| processor.feed long_name(dir,f) }
|
125
|
+
# # end
|
126
|
+
# end
|
127
|
+
|
128
|
+
# private
|
129
|
+
# def long_name dir,file
|
130
|
+
# File.join(dir,file).sub /^\.\//, ''
|
131
|
+
# end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# TODO: rename instance_eval -> feed_block or so
|
2
|
+
# (...)
|
3
|
+
# so1,so2: returns just last and array
|
4
|
+
# file,files: yields one by one and all as array
|
5
|
+
|
6
|
+
# file - also one, or just one by one? and files - also array?
|
7
|
+
# each_file '*.mp3' do
|
8
|
+
# end
|
9
|
+
# files ['*.mp3'] do # yields array of files?
|
10
|
+
# end
|
11
|
+
# file 'about.png' do |file| # optional - more dsl-ish?
|
12
|
+
|
13
|
+
|
14
|
+
describe 'DirProcessor method' do
|
15
|
+
it 'takes block' do
|
16
|
+
expect { DirProcessor() }.to raise_error
|
17
|
+
DirProcessor(){}
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns instance of dir processor' do
|
21
|
+
DirProcessor(){}.class.should == DirProcessor::DirProcessor
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'is configured by block' do
|
25
|
+
DirProcessor(){}.send(:so).should_not == DirProcessor(){ dir('.'){} }.send(:so)
|
26
|
+
end
|
27
|
+
|
28
|
+
example 'basic' do
|
29
|
+
data = {}
|
30
|
+
this = DirProcessor do
|
31
|
+
file 'about.png' do |file|
|
32
|
+
data[:about] = file
|
33
|
+
end
|
34
|
+
dir 'mp3s' do
|
35
|
+
file '*.mp3' do |file|
|
36
|
+
File.exist?(file).should == true
|
37
|
+
(data[:mp3s] ||= []) << file
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
temp_dir do
|
43
|
+
mkfile 'about.png'
|
44
|
+
mkfile 'any.other'
|
45
|
+
|
46
|
+
mkfile 'mp3s/one.mp3'
|
47
|
+
mkfile 'mp3s/two.mp3'
|
48
|
+
mkfile 'mp3s/three.mp3'
|
49
|
+
|
50
|
+
this.feed '.'
|
51
|
+
end
|
52
|
+
|
53
|
+
data.keys.should =~ [:about, :mp3s]
|
54
|
+
data[:about].should == 'about.png'
|
55
|
+
data[:mp3s].should =~ %w[ mp3s/one.mp3 mp3s/two.mp3 mp3s/three.mp3 ]
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
# runs block in temp dir, purge it after all
|
4
|
+
def temp_dir temp='tmp/trash'
|
5
|
+
FileUtils.mkpath temp
|
6
|
+
Dir.chdir(temp) { yield }
|
7
|
+
FileUtils.rm_rf temp
|
8
|
+
end
|
9
|
+
|
10
|
+
# mkpath + touch
|
11
|
+
def mkfile file
|
12
|
+
FileUtils.mkpath File.split(file)[0]
|
13
|
+
FileUtils.touch file
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'dir_processor'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
Dir["./spec/support/**/*.rb"].each {|f| require f} # no comment
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
|
14
|
+
# Run specs in random order to surface order dependencies. If you find an
|
15
|
+
# order dependency and want to debug it, you can fix the order by providing
|
16
|
+
# the seed, which is printed after each run.
|
17
|
+
# --seed 1234
|
18
|
+
config.order = 'random'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dir_processor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander K
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: def_dsl
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: dsl to define processor for directories/files structure
|
79
|
+
email:
|
80
|
+
- xpyro@ya.ru
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- dir_processor.gemspec
|
92
|
+
- lib/dir_processor.rb
|
93
|
+
- lib/dir_processor/version.rb
|
94
|
+
- spec/example_spec.rb
|
95
|
+
- spec/support/file_utils.rb
|
96
|
+
- spec/support/require_self.rb
|
97
|
+
- spec/support/spec_helper.rb
|
98
|
+
homepage: https://github.com/sowcow/dir_processor
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
hash: -1129910543518694584
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
hash: -1129910543518694584
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 1.8.25
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: dsl to define processor for directories/files structure
|
129
|
+
test_files:
|
130
|
+
- spec/example_spec.rb
|
131
|
+
- spec/support/file_utils.rb
|
132
|
+
- spec/support/require_self.rb
|
133
|
+
- spec/support/spec_helper.rb
|