dir_model 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/dir_model.gemspec +20 -0
- data/lib/dir_model/core_ext/dir.rb +10 -0
- data/lib/dir_model/core_ext/zip_dir/zipper.rb +10 -0
- data/lib/dir_model/export.rb +82 -0
- data/lib/dir_model/version.rb +3 -0
- data/lib/dir_model.rb +10 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9e2aa05d706d3f1d0062abcabac6716361245c35
|
4
|
+
data.tar.gz: 35ef12280756a22d8c6be20be30fe5530478a73b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6e1ed34d9a1a8f5a5b1fe6a28adc19ab7ecbc0679972cada464e4b80828a4306c41505387ad0e09b85822168d8f4bac4fcd997c486b33b125c402551e3057a73
|
7
|
+
data.tar.gz: 8765c79b182f2c5b6d38a676b4bb1bf678f30e85b5b1dd3a5073ac17c69b13e5ceb18916d3f7c1745db9798516515a819cfa90256353c54954be56d2fc44b78b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Steve Chung
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# DirModel
|
2
|
+
|
3
|
+
Import and export directories with an ORM-like interface.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'dir_model'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install dir_model
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Export
|
24
|
+
```ruby
|
25
|
+
class ImageDir < DirModel::Export
|
26
|
+
file :zone_image
|
27
|
+
|
28
|
+
def zone_image_source
|
29
|
+
File.new("spec/fixtures/image.png")
|
30
|
+
end
|
31
|
+
def zone_image_name
|
32
|
+
"testing.png"
|
33
|
+
end
|
34
|
+
|
35
|
+
def _generate
|
36
|
+
mk_chdir "level1" do
|
37
|
+
mk_chdir "level2" do
|
38
|
+
copy_file :zone_image
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
image_dir = ImageDir.new
|
45
|
+
image_dir.path # => path representing the above: "#{image_dir.path}/level1/level2/testing.png"
|
46
|
+
```
|
47
|
+
|
48
|
+
## zip_dir
|
49
|
+
Use [`zip_dir`](https://github.com/FinalCAD/zip_dir) to zip DirModel::Export instances:
|
50
|
+
```ruby
|
51
|
+
# Zip
|
52
|
+
zipper = ZipDir::Zipper.new
|
53
|
+
zip_file = zipper.generate do |z|
|
54
|
+
z.add_and_cleanup_dir __dir_model_export__
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
**Ensure that `require zip_dir` occurs before `dir_model`**
|
59
|
+
|
60
|
+
## Future
|
61
|
+
Define a schema for the directory.
|
62
|
+
```ruby
|
63
|
+
class ImageDir
|
64
|
+
include DirModel
|
65
|
+
|
66
|
+
file :zone_image, path: "{{root}}/{{branch}}", name: "{{zone_name}}"
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
To export, define your export model like [`ActiveModel::Serializer`](https://github.com/rails-api/active_model_serializers)
|
71
|
+
and generate the directory requirements:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
class ImageExportDir < ImageDir
|
75
|
+
include DirModel::Export
|
76
|
+
|
77
|
+
# below are method overrides with default implementation
|
78
|
+
#
|
79
|
+
# overriding is recommended
|
80
|
+
#
|
81
|
+
def root; source_model.root end
|
82
|
+
def branch; source_model.branch end
|
83
|
+
def zone_name; source_model.zone_name end
|
84
|
+
|
85
|
+
# define your image source
|
86
|
+
#
|
87
|
+
# override with something like: File.new("path/to/image.png")
|
88
|
+
#
|
89
|
+
def zone_image
|
90
|
+
source_model.zone_image
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
To import, define your import model, which works like [`ActiveRecord`](http://guides.rubyonrails.org/active_record_querying.html)
|
96
|
+
wrapping over a directory:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
class ImageImportDir < ImageDir
|
100
|
+
include DirModel::Import
|
101
|
+
|
102
|
+
#
|
103
|
+
# below are method overrides with default implementation
|
104
|
+
#
|
105
|
+
def root; "level1" end
|
106
|
+
def branch; "level2" end
|
107
|
+
def zone_name; "testing" end
|
108
|
+
def zone_image; File.new("....") end
|
109
|
+
end
|
110
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dir_model"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/dir_model.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dir_model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dir_model"
|
8
|
+
spec.version = DirModel::VERSION
|
9
|
+
spec.authors = ["Steve Chung"]
|
10
|
+
spec.email = ["hello@stevenchung.ca"]
|
11
|
+
|
12
|
+
spec.summary = "Import and export directories with an ORM-like interface."
|
13
|
+
spec.homepage = "https://github.com/FinalCAD/dir_model"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module DirModel
|
2
|
+
class Export
|
3
|
+
def path
|
4
|
+
generate unless generated?
|
5
|
+
@temp_path
|
6
|
+
end
|
7
|
+
|
8
|
+
def entry_paths
|
9
|
+
Dir.clean_entries(path).map {|entry| File.join path, entry }
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
!file_sources.any? { |file_source| file_source.nil? || file_source.read.nil? }
|
14
|
+
rescue
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def file_sources
|
19
|
+
self.class.files.map { |file| file_source(file) }
|
20
|
+
end
|
21
|
+
def file_source(file)
|
22
|
+
public_send(self.class.file_source_method_name(file))
|
23
|
+
end
|
24
|
+
def file_name(file)
|
25
|
+
File.join(*pwd, public_send(self.class.file_name_method_name(file)))
|
26
|
+
end
|
27
|
+
|
28
|
+
def generated?
|
29
|
+
!!@generated
|
30
|
+
end
|
31
|
+
def generate
|
32
|
+
cleanup if generated?
|
33
|
+
@temp_path = Dir.mktmpdir
|
34
|
+
|
35
|
+
@pwd = [@temp_path]
|
36
|
+
_generate
|
37
|
+
ensure
|
38
|
+
@generated = true
|
39
|
+
end
|
40
|
+
|
41
|
+
def cleanup
|
42
|
+
FileUtils.remove_entry_secure @temp_path
|
43
|
+
@generated = false
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
attr_reader :pwd
|
48
|
+
|
49
|
+
def copy_file(file)
|
50
|
+
File.open(file_name(file), 'wb') {|f| f.write(file_source(file).read) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def _generate
|
54
|
+
raise NotImplementedError.new("Missing #{self.class} implementation")
|
55
|
+
end
|
56
|
+
|
57
|
+
def mk_chdir(dir, *permissions_int)
|
58
|
+
@pwd << dir
|
59
|
+
Dir.mkdir(File.join(*pwd), *permissions_int)
|
60
|
+
yield
|
61
|
+
ensure
|
62
|
+
@pwd.pop
|
63
|
+
end
|
64
|
+
|
65
|
+
class << self
|
66
|
+
attr_reader :files
|
67
|
+
|
68
|
+
def file(*files)
|
69
|
+
@files ||= []
|
70
|
+
@files += files
|
71
|
+
end
|
72
|
+
|
73
|
+
def file_name_method_name(file)
|
74
|
+
"#{file}_name"
|
75
|
+
end
|
76
|
+
|
77
|
+
def file_source_method_name(file)
|
78
|
+
"#{file}_source"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/dir_model.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dir_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Chung
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- hello@stevenchung.ca
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- dir_model.gemspec
|
30
|
+
- lib/dir_model.rb
|
31
|
+
- lib/dir_model/core_ext/dir.rb
|
32
|
+
- lib/dir_model/core_ext/zip_dir/zipper.rb
|
33
|
+
- lib/dir_model/export.rb
|
34
|
+
- lib/dir_model/version.rb
|
35
|
+
homepage: https://github.com/FinalCAD/dir_model
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.4.5.1
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Import and export directories with an ORM-like interface.
|
59
|
+
test_files: []
|