dir_model 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .idea/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dir_model.gemspec
4
+ gemspec
5
+
6
+ gem "bundler", "> 1.3"
7
+ gem "rake", "~> 10.0"
8
+
9
+ group :test do
10
+ gem "rspec"
11
+ gem "codeclimate-test-reporter"
12
+ gem 'pry-byebug'
13
+ end
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
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
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,10 @@
1
+ class Dir
2
+ class << self
3
+ def clean_entries(dir)
4
+ entries = entries(dir)
5
+ entries.delete "."
6
+ entries.delete ".."
7
+ entries
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ if defined?(ZipDir)
2
+ class ZipDir::Zipper
3
+ def add_and_cleanup_dir(dir)
4
+ return unless dir.valid?
5
+
6
+ dir.entry_paths.each { |path| add_path path }
7
+ dir.cleanup
8
+ end
9
+ end
10
+ 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
@@ -0,0 +1,3 @@
1
+ module DirModel
2
+ VERSION = "0.1.0"
3
+ end
data/lib/dir_model.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "dir_model/version"
2
+
3
+ require 'dir_model/core_ext/dir'
4
+ require 'dir_model/core_ext/zip_dir/zipper'
5
+
6
+ require 'dir_model/export'
7
+
8
+ module DirModel
9
+ # Your code goes here...
10
+ end
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: []