real_zip 0.0.8

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ec8f605586197e13af8255a2eefcd53029f6f63
4
+ data.tar.gz: 15841b180415d2349c1d78c1f1ebd20674cbdb44
5
+ SHA512:
6
+ metadata.gz: f7bfeec3f16273e3b6af791e7715ac333ffecfcdeeacb362c1855b76550710e07f4772187f0d355a3a9acf53324e9d0554f22b8a7e1cf1939dcab88d1d4d2af9
7
+ data.tar.gz: e82bdabf8248d5cbc66ba02c1c17f032a834a4dbd2a2e010a3088d7d01cc4b9745a3c54895ed398c3c764bdc5c464ab084349296cfd161a5655debc79baaf25e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+ -r spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fake_zip.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Prasad R.
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,50 @@
1
+ # realZip
2
+
3
+ ## Warning
4
+ This gem is currently under development. Please refrain from using it.
5
+
6
+ ## Usage
7
+
8
+ ```ruby
9
+ require 'real_zip'
10
+
11
+ # create zip file with given file structure and transfer files to the directory of your choice within the zip file
12
+
13
+ RealZip 'temp.zip', {dir1:[:f1,:f2],dir2:[:f3,{dir4:[:f4]}]}
14
+
15
+ # ensure it works
16
+ puts `unzip -l temp.zip | awk '{print $4}'`
17
+ ===========================================
18
+ dir1/
19
+ dir2/
20
+ dir2/dir4/
21
+ dir1/f1
22
+ dir1/f2
23
+ dir2/f3
24
+ dir2/dir4/f4
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ gem 'real_zip'
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install real_zip
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
48
+
49
+ ## Acknowledgement
50
+ This is software is built upon Alexander K's Fake_Zip gem.A hearty thanks to him for making it freely available.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module RealZip
2
+ VERSION = "0.0.8"
3
+ end
data/lib/real_zip.rb ADDED
@@ -0,0 +1,150 @@
1
+ require "real_zip/version"
2
+ require 'yaml'
3
+ require 'forwardable'
4
+ require 'zip/zipfilesystem'
5
+ def RealZip file, structure
6
+ RealZip.new file, structure
7
+ end
8
+
9
+ module RealZip
10
+
11
+ ####################
12
+
13
+ module Helpers
14
+ extend self
15
+
16
+ def traverse array_or_hash, path=[], &block
17
+ given = array_or_hash
18
+ case given
19
+ when Hash
20
+ given.each_pair do |k,v|
21
+ block.call path+[k], :dir # entering directory
22
+ traverse v,path+[k],&block
23
+ end
24
+ when Array then given.each { |x| traverse x,path,&block }
25
+ else
26
+ block.call path+[given], :file
27
+ end
28
+ end
29
+
30
+ def collect_all(given)
31
+ found = []
32
+ traverse given do |x,y| found << [x,y] end
33
+ found
34
+ end
35
+
36
+ def collect_only(given, kind)
37
+ collect_all(given).map { |(name,type)| name if type == kind }.compact
38
+ end
39
+
40
+ def files(given)
41
+ collect_only(given, :file).map { |x| x.join ?/ }
42
+ var = File.open(given)
43
+
44
+ end
45
+
46
+ def dirs(given)
47
+ collect_only(given, :dir).map { |x| x.join ?/ }
48
+ end
49
+ end
50
+
51
+
52
+
53
+ ####################
54
+
55
+ class RealZip < Struct.new :file_structure
56
+ def save file
57
+ File.delete file if File.exist? file
58
+ Zip::ZipFile.open file, Zip::ZipFile::CREATE do |z|
59
+ dirs(struct).each do |dir|
60
+ z.dir.mkdir(dir) unless z.file.exist? dir
61
+ end
62
+ files(struct).each do |file|
63
+ z.file.open(file, "w") { |f| f.write File.new(file, "r") }
64
+ end
65
+ end
66
+ end
67
+
68
+ # def save file
69
+ # File.delete file if File.exist? file
70
+ # Zip::ZipFile.open file, Zip::ZipFile::CREATE do |z|
71
+ # dirs(struct).each do |dir|
72
+ # z.dir.mkdir(dir) unless z.file.exist? dir
73
+ # end
74
+ # files(struct).each do |file|
75
+ # z.file.open(file, "w") { |f| f.write file }
76
+ # end
77
+ # end
78
+
79
+ def struct given=file_structure
80
+ given.is_a?(String) ? YAML.load(given) : given
81
+ end
82
+
83
+ private
84
+
85
+ extend Forwardable
86
+ def_delegators Helpers, :files, :dirs
87
+ end
88
+
89
+ def new file, structure
90
+ RealZip.new(structure).save file
91
+ end
92
+
93
+ module_function :new
94
+ end
95
+
96
+
97
+ __END__
98
+ # Zippy.create 'my.zip' do |z|
99
+ # add_dir z, 'my', 'app'
100
+ # add_dir z, 'my', 'config'
101
+ # add_file z, 'my', 'config.ru'
102
+ # add_file z, 'my', 'Gemfile'
103
+ # end
104
+
105
+
106
+ # Other tests...
107
+
108
+ found = []
109
+ RealZip::Helpers.traverse( {a: [{a: 1, b: 2}, 3]}, &proc{ |x| found << x } )
110
+ found.map { |x| x.join ?/ } == ["a", "a/a", "a/a/1", "a/b", "a/b/2", "a/3"] or raise
111
+
112
+ RealZip::Helpers.files({a: [{a: 1, b: 2}, 3]}) == %w[ a/a/1 a/b/2 a/3 ] or raise
113
+
114
+
115
+ require 'yaml'
116
+
117
+ raise unless RealZip('empty_dir: []').contents == {'empty_dir' => []}
118
+ raise unless RealZip('dir: [file1, file2]').contents == {'dir' => ['file1','file2']}
119
+
120
+ tree = 'root_dir: [file1, file2, nested_dir: []]'
121
+ raise unless RealZip(tree).contents == {'root_dir' => ['file1','file2',{'nested_dir' => [] }]}
122
+
123
+ file_name = 'temp-test-Real.zip'
124
+ tree = 'root_dir: [file1, file2, nested_dir: [nested_file.any], empty_dir: []]'
125
+ RealZip(tree).save file_name
126
+
127
+ Zip::ZipFile.open file_name do |z|
128
+ z.file.exist? 'root_dir/file1' or raise
129
+ z.file.exist? 'root_dir/file2' or raise
130
+ z.file.exist? 'root_dir/nested_dir/nested_file.any' or raise
131
+ z.file.exist? 'root_dir/empty_dir' or raise # no empty dirs
132
+ end
133
+ File.delete file_name
134
+
135
+
136
+ # def add_file( zippyfile, dst_dir, f )
137
+ # zippyfile["#{dst_dir}/#{f}"] = File.open(f)
138
+ # if File.file?(f)
139
+ # FileUtils.copy_file f, dst_dir
140
+ # end
141
+ # end
142
+
143
+ # def add_dir( zippyfile, dst_dir, d )
144
+ # glob = "#{d}/**/*"
145
+ # FileList.new( glob ).each { |f|
146
+ # if (File.file?(f))
147
+ # add_file zippyfile, dst_dir, f
148
+ # end
149
+ # }
150
+ # end
data/real_zip.gemspec 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 'real_zip/version'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "real_zip"
7
+ spec.version = RealZip::VERSION
8
+ spec.authors = ["Prasad R."]
9
+ spec.email = ["thelaptopsage@gmail.com"]
10
+ spec.description = %q{ build zip files with given file structure and transfer files to it for testing }.strip
11
+ spec.summary = %q{ build zip files with given file structure and transfer files to it for testing }.strip
12
+ spec.homepage = "https://github.com/OskarSchindler/real_zip"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+
23
+ # deps:
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_runtime_dependency "rubyzip"
26
+ end
@@ -0,0 +1,36 @@
1
+ describe FakeZip do
2
+ extend MyTemp.new(:temp, 'tmp/testing.zip')
3
+
4
+ describe '.new' do
5
+ it 'takes file name and yaml string' do
6
+ expect { FakeZip temp, '{}' }.to change { File.exist? temp }.from(false).to(true)
7
+ end
8
+
9
+ it 'takes file name and hash' do
10
+ expect { FakeZip temp, {} }.to change { File.exist? temp }.from(false).to(true)
11
+ end
12
+
13
+ it 'recreates file if exist' do
14
+ FakeZip temp, {dir:[:file]}
15
+ FakeZip temp, {other:[:any]}
16
+ zip_entries(temp).should == %w[ other/ other/any ]
17
+ end
18
+ end
19
+
20
+ describe 'examples (given structure --- files in archive)' do
21
+ examples = <<-END.lines.map(&:strip).map { |x| x.split(' --- ',2).map { |x| eval x } }
22
+ "[]" --- []
23
+ {} --- []
24
+ {root: []} --- ['root/']
25
+ "root: []" --- ['root/']
26
+ 'root_dir: [file1, file2, nested_dir: [nested_file.any], empty_dir: []]' --- ["root_dir/", "root_dir/nested_dir/", "root_dir/empty_dir/", "root_dir/file1", "root_dir/file2", "root_dir/nested_dir/nested_file.any"]
27
+ END
28
+
29
+ examples.each do |input,output|
30
+ specify "#{input.inspect} --- #{output.inspect}" do
31
+ FakeZip temp, input
32
+ zip_entries(temp).should == output
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
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
+
8
+ Dir["./spec/support/**/*.rb"].each {|f| require f} # no comment
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ config.order = 'random'
15
+ end
@@ -0,0 +1,69 @@
1
+ # this file gona be gem!
2
+ #
3
+ # TODO:
4
+ # use Modules.call as a default method, instead of using used do
5
+
6
+
7
+ class MetaModule < Module
8
+ # use .new so I don't bother user to use super in #initialize
9
+ def self.new(*)
10
+ super.tap { |x| x.send :include, self::Methods }
11
+ end
12
+
13
+ def self.used &block
14
+ define_method :included, &block
15
+ define_method :extended, &block
16
+ end
17
+ end
18
+
19
+
20
+
21
+ class MetaModule2 #< Class
22
+ def self.new *params
23
+ _params = params.join ?,
24
+ a_params = params.map{|x|"@#{x}"}.join ?,
25
+
26
+ Class.new(MetaModule) do
27
+ eval "def initialize(#{_params}); #{a_params} = #{_params} end"
28
+ private; attr_reader *params
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ class My2 < MetaModule2.new :x, :y
35
+ used do |target|
36
+ target.check x
37
+ target.check y
38
+ target.check [x,y]
39
+ end
40
+
41
+ module Methods
42
+ def check given
43
+ p given
44
+ end
45
+ end
46
+ end
47
+
48
+ # module Context
49
+ # extend My2.new 1, 2
50
+ # end
51
+
52
+ # class My < MetaModule
53
+ # def initialize name
54
+ # @name = name
55
+ # end
56
+
57
+ # used do |at|
58
+ # at.abc @name
59
+ # end
60
+
61
+ # module Methods
62
+ # def abc given; p given end
63
+ # end
64
+ # end
65
+
66
+ # module A
67
+ # extend My.new 123
68
+ # end
69
+ # include My.new 234
@@ -0,0 +1,49 @@
1
+ class MyTemp < MetaModule2.new :getter, :file
2
+ used do |at|
3
+ at.def_temp_file getter, file
4
+ end
5
+
6
+ module Methods
7
+ def def_temp_file getter, file
8
+ let(getter) { file }
9
+ after(:each) { File.delete file if File.exist? file }
10
+ end
11
+ end
12
+ end
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+ __END__
32
+ # def initialize getter, file
33
+ # @getter, @file = getter, file
34
+ # end
35
+
36
+
37
+ # def extended target
38
+ # target.def_temp_file @getter, @file
39
+ # end
40
+
41
+ # def temp_file getter, file
42
+ # let(getter) { file }
43
+ # after(:each) { File.delete file if File.exist? file }
44
+ # end
45
+
46
+ # def self.extended target
47
+ # target.instance_eval do
48
+ # end
49
+ # end
@@ -0,0 +1 @@
1
+ require 'real_zip'
@@ -0,0 +1,11 @@
1
+ require 'zip/zip'
2
+
3
+ def zip_entries file # to wrap this crap
4
+ result = []
5
+ Zip::ZipFile.open file do |z|
6
+ z.map do |entry|
7
+ result << entry.name # !entry.file?
8
+ end
9
+ end
10
+ result
11
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: real_zip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Prasad R.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubyzip
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: build zip files with given file structure and transfer files to it for
70
+ testing
71
+ email:
72
+ - thelaptopsage@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/real_zip.rb
84
+ - lib/real_zip/version.rb
85
+ - real_zip.gemspec
86
+ - spec/examples_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/support/meta_module.rb
89
+ - spec/support/my_temp.rb
90
+ - spec/support/require.rb
91
+ - spec/support/zip_entries.rb.rb
92
+ homepage: https://github.com/OskarSchindler/real_zip
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.3.0
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: build zip files with given file structure and transfer files to it for testing
116
+ test_files:
117
+ - spec/examples_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/support/meta_module.rb
120
+ - spec/support/my_temp.rb
121
+ - spec/support/require.rb
122
+ - spec/support/zip_entries.rb.rb