dir_model 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile +3 -1
- data/README.md +37 -67
- data/UPGRADE.md +26 -0
- data/dir_model.gemspec +3 -0
- data/lib/dir_model/core_ext/dir.rb +3 -3
- data/lib/dir_model/core_ext/zip_dir/zipper.rb +1 -1
- data/lib/dir_model/export/aggregate_dir.rb +33 -0
- data/lib/dir_model/export.rb +24 -61
- data/lib/dir_model/utils.rb +7 -0
- data/lib/dir_model/version.rb +1 -1
- data/lib/dir_model.rb +29 -2
- metadata +35 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 446e930d0a4af01c2fd5a48cbfb3e742e0a35b32
|
4
|
+
data.tar.gz: 8abc4fe852ccdfb4a259babd81e3ee080f33e8ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58278e1b4641a695da21cc21f91c696517d85807ff0824175cfe32884e097863b5658871571498092c2b2b46a3990c9f65d7d27be6b5ee5a7ad7cc948a89206d
|
7
|
+
data.tar.gz: 409e249c4c01de393961daef38df9c464ee8c79870efa7fc4633a65fba65743d0d57248b6bf59a709c17d6f82f7e698c50912e10e52825ba8196707542c47892
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -3,6 +3,8 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in dir_model.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
+
# gem 'inherited_class_var', path: '../inherited_class_var'
|
7
|
+
|
6
8
|
gem "bundler", "> 1.3"
|
7
9
|
gem "rake", "~> 10.0"
|
8
10
|
|
@@ -10,4 +12,4 @@ group :test do
|
|
10
12
|
gem "rspec"
|
11
13
|
gem "codeclimate-test-reporter"
|
12
14
|
gem 'pry-byebug'
|
13
|
-
end
|
15
|
+
end
|
data/README.md
CHANGED
@@ -21,28 +21,50 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
### Export
|
24
|
+
|
24
25
|
```ruby
|
25
|
-
class ImageDir
|
26
|
-
|
26
|
+
class ImageDir
|
27
|
+
include DirModel
|
28
|
+
|
29
|
+
file :image, path: -> { "#{dir}/#{sub_dir}" }, name: -> { "#{image_name}.png" }
|
30
|
+
end
|
27
31
|
|
28
|
-
|
29
|
-
|
32
|
+
class ImageExportDir < ImageDir
|
33
|
+
include DirModel::Export
|
34
|
+
|
35
|
+
def dir
|
36
|
+
'Sectors'
|
30
37
|
end
|
31
|
-
|
32
|
-
|
38
|
+
|
39
|
+
def sub_dir
|
40
|
+
source_model.sector_name
|
33
41
|
end
|
34
42
|
|
35
|
-
def
|
36
|
-
|
37
|
-
mk_chdir "level2" do
|
38
|
-
copy_file :zone_image
|
39
|
-
end
|
40
|
-
end
|
43
|
+
def image_name
|
44
|
+
source_model.zone_name
|
41
45
|
end
|
46
|
+
|
47
|
+
def image
|
48
|
+
source_model.zone
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
fixture_models = [
|
53
|
+
OpenStruct.new({
|
54
|
+
id: 42,
|
55
|
+
sector_name: 'sector_name',
|
56
|
+
zone_name: 'zone_name',
|
57
|
+
zone: File.new('spec/fixtures/image.png'),
|
58
|
+
})
|
59
|
+
]
|
60
|
+
|
61
|
+
exporter = DirModel::Export::AggregateDir.new(ImageExportDir)
|
62
|
+
|
63
|
+
exporter.generate do |dir|
|
64
|
+
models.each { |model| dir << model }
|
42
65
|
end
|
43
66
|
|
44
|
-
|
45
|
-
image_dir.path # => path representing the above: "#{image_dir.path}/level1/level2/testing.png"
|
67
|
+
exporter.dir_path # => path of generated dir .../Sectors
|
46
68
|
```
|
47
69
|
|
48
70
|
## zip_dir
|
@@ -55,56 +77,4 @@ zip_file = zipper.generate do |z|
|
|
55
77
|
end
|
56
78
|
```
|
57
79
|
|
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
|
-
```
|
80
|
+
**Ensure that `require zip_dir` occurs before `dir_model` (for now)**
|
data/UPGRADE.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Upgrading
|
2
|
+
|
3
|
+
# Upgrading from 0.1.0 to 0.2.0
|
4
|
+
|
5
|
+
You have to change
|
6
|
+
|
7
|
+
```
|
8
|
+
def _generate
|
9
|
+
mk_chdir "level1" do
|
10
|
+
mk_chdir "level2" do
|
11
|
+
copy_file :zone_image
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
for a DirModel
|
18
|
+
|
19
|
+
```
|
20
|
+
class ImageDir
|
21
|
+
include DirModel
|
22
|
+
|
23
|
+
file :image, path: -> { "#{dir}/#{sub_dir}" }, name: -> { "#{image_name}.png" }
|
24
|
+
end
|
25
|
+
```
|
26
|
+
see the README
|
data/dir_model.gemspec
CHANGED
@@ -17,4 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.bindir = "exe"
|
18
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
19
|
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport', '~> 4.2'
|
22
|
+
spec.add_dependency 'inherited_class_var', '~> 0.1'
|
20
23
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/hash_with_indifferent_access'
|
2
|
+
|
3
|
+
module DirModel
|
4
|
+
module Export
|
5
|
+
class AggregateDir
|
6
|
+
include Utils
|
7
|
+
|
8
|
+
attr_reader :export_dir_model_class, :context, :dir_path
|
9
|
+
|
10
|
+
# @param [Export] export_model export model class
|
11
|
+
def initialize(export_dir_model_class, context={})
|
12
|
+
@export_dir_model_class = export_dir_model_class
|
13
|
+
@context = context.to_h.symbolize_keys
|
14
|
+
@dir_path = Dir.mktmpdir
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add a row_model to the
|
18
|
+
# @param [] source_model the source model of the export file model
|
19
|
+
# @param [Hash] context the extra context given to the instance of the file model
|
20
|
+
def append_model(source_model, context={})
|
21
|
+
source_path = export_dir_model_class.new(source_model, context.reverse_merge(self.context)).path
|
22
|
+
FileUtils.cp_r Dir.glob("#{source_path}/*"), dir_path
|
23
|
+
end
|
24
|
+
alias_method :<<, :append_model
|
25
|
+
|
26
|
+
def generate
|
27
|
+
yield self
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/dir_model/export.rb
CHANGED
@@ -1,82 +1,45 @@
|
|
1
1
|
module DirModel
|
2
|
-
|
3
|
-
def path
|
4
|
-
generate unless generated?
|
5
|
-
@temp_path
|
6
|
-
end
|
2
|
+
module Export
|
7
3
|
|
8
|
-
|
9
|
-
Dir.clean_entries(path).map {|entry| File.join path, entry }
|
10
|
-
end
|
4
|
+
attr_reader :source_model, :context
|
11
5
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
6
|
+
# @param [Model] source_model object to export to files
|
7
|
+
# @param [Hash] context
|
8
|
+
def initialize(source_model, context={})
|
9
|
+
@source_model = source_model
|
10
|
+
@context = OpenStruct.new(context)
|
11
|
+
@root_path = Dir.mktmpdir
|
16
12
|
end
|
17
13
|
|
18
|
-
def
|
19
|
-
|
20
|
-
|
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)))
|
14
|
+
def path
|
15
|
+
generate unless generated?
|
16
|
+
@root_path
|
26
17
|
end
|
27
18
|
|
19
|
+
private
|
20
|
+
|
28
21
|
def generated?
|
29
22
|
!!@generated
|
30
23
|
end
|
24
|
+
|
31
25
|
def generate
|
32
26
|
cleanup if generated?
|
33
|
-
@temp_path = Dir.mktmpdir
|
34
27
|
|
35
|
-
|
36
|
-
|
28
|
+
self.class.files.each do |file_name, options|
|
29
|
+
dir_path = instance_exec(&options[:path])
|
30
|
+
file_path = File.join(dir_path, instance_exec(&options[:name]))
|
31
|
+
|
32
|
+
mkdir { File.join(@root_path, dir_path) }
|
33
|
+
|
34
|
+
File.open(File.join(@root_path, file_path), 'wb') {|f| f.write(self.public_send(file_name).read) }
|
35
|
+
end
|
37
36
|
ensure
|
38
37
|
@generated = true
|
39
38
|
end
|
40
39
|
|
41
40
|
def cleanup
|
42
|
-
FileUtils.
|
41
|
+
FileUtils.remove_entry @root_path
|
43
42
|
@generated = false
|
44
43
|
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
44
|
end
|
82
|
-
end
|
45
|
+
end
|
data/lib/dir_model/version.rb
CHANGED
data/lib/dir_model.rb
CHANGED
@@ -1,10 +1,37 @@
|
|
1
|
-
require
|
1
|
+
require 'dir_model/version'
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
require 'dir_model/utils'
|
2
6
|
|
3
7
|
require 'dir_model/core_ext/dir'
|
4
8
|
require 'dir_model/core_ext/zip_dir/zipper'
|
5
9
|
|
6
10
|
require 'dir_model/export'
|
11
|
+
require 'dir_model/export/aggregate_dir'
|
12
|
+
|
13
|
+
require 'inherited_class_var'
|
7
14
|
|
8
15
|
module DirModel
|
9
|
-
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
included do
|
19
|
+
include Utils
|
20
|
+
include InheritedClassVar
|
21
|
+
inherited_class_hash :files
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
|
26
|
+
# @return [Array<Symbol>] file names
|
27
|
+
def file_names
|
28
|
+
files.keys
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def file(file_name, options={})
|
34
|
+
merge_files(file_name.to_sym => options)
|
35
|
+
end
|
36
|
+
end
|
10
37
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dir_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Chung
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
12
|
-
dependencies:
|
11
|
+
date: 2015-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: inherited_class_var
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.1'
|
13
41
|
description:
|
14
42
|
email:
|
15
43
|
- hello@stevenchung.ca
|
@@ -20,10 +48,12 @@ files:
|
|
20
48
|
- ".gitignore"
|
21
49
|
- ".rspec"
|
22
50
|
- ".travis.yml"
|
51
|
+
- CHANGELOG.md
|
23
52
|
- Gemfile
|
24
53
|
- LICENSE.txt
|
25
54
|
- README.md
|
26
55
|
- Rakefile
|
56
|
+
- UPGRADE.md
|
27
57
|
- bin/console
|
28
58
|
- bin/setup
|
29
59
|
- dir_model.gemspec
|
@@ -31,6 +61,8 @@ files:
|
|
31
61
|
- lib/dir_model/core_ext/dir.rb
|
32
62
|
- lib/dir_model/core_ext/zip_dir/zipper.rb
|
33
63
|
- lib/dir_model/export.rb
|
64
|
+
- lib/dir_model/export/aggregate_dir.rb
|
65
|
+
- lib/dir_model/utils.rb
|
34
66
|
- lib/dir_model/version.rb
|
35
67
|
homepage: https://github.com/FinalCAD/dir_model
|
36
68
|
licenses:
|