dir_model 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 022ba69b362a22c860bcf8ef4ada5e8b9f91a10c
4
- data.tar.gz: efbbff6ca84ccdaae46894847f106f95778c4bab
3
+ metadata.gz: 55a68864ee18333c18a59e6e8a8331aedf90ed0f
4
+ data.tar.gz: cf8dee9636172ce581287536d1bc903ec155690a
5
5
  SHA512:
6
- metadata.gz: 2577ef987f2d30ae7b62c5ff8066bb84d29091614d423e81976248b9ae7366e8fd9d63adb3db15d9c0163f4251da97d7660d4e4200f08b0908891683876b795c
7
- data.tar.gz: 1dfa90b331e66a31ca222eef9e19bd8035330a1d77f5e9dea82d2af6d8255b9fffce3ec0fd1a95c42ae1b984ff4c99906b0e2b8bb894e166dea007a2e3b68557
6
+ metadata.gz: edbca7458a28490260d7f50495e10540ed6cfad0422bbdede0aaeb8d01393a33f7c4ddded2c4d2207784823b2615e0de887863c8217e3400e33c0f323882ce60
7
+ data.tar.gz: 73bd59243eaffcd38e75522434cf5f40a3014efc6556df4bb90214992a0066fb600d53f8037c8b7e8634d79407f45ace2e80c63a8ae6db20754896153da6db1b
data/README.md CHANGED
@@ -115,6 +115,47 @@ end
115
115
  exporter.dir_path # => path of generated dir .../Sectors
116
116
  ```
117
117
 
118
+ an skip? method based on the name of file :image is create, this method is named `image_skip?`
119
+
120
+ default implementation
121
+ ```
122
+ def image_skip?
123
+ image.present?
124
+ end
125
+ ```
126
+ NOTE Safe to override on your Exporter
127
+
128
+ In fact this is equivalent to
129
+
130
+ ```
131
+ def image_skip?
132
+ source_model.zone.present?
133
+ end
134
+ ```
135
+
136
+ by default Exporter provide a link between `source_model` and your `file` like
137
+ ```
138
+ def image
139
+ source_model.image
140
+ end
141
+ ```
142
+ NOTE Safe to override on your Exporter
143
+
144
+ In fact this play well with carrierwave and provide for you automatically
145
+ ```
146
+ def image
147
+ source_model.image.file
148
+ end
149
+ ```
150
+
151
+ as well Exporter provide extension method only for carrierwave uploader
152
+ ```
153
+ def image_extension
154
+ source_model.image.file.extension
155
+ end
156
+ ```
157
+ Otherwise return nil, safe to override on your Exporter
158
+
118
159
  ## zip_dir
119
160
  Use [`zip_dir`](https://github.com/FinalCAD/zip_dir) to zip DirModel::Export instances:
120
161
  ```ruby
@@ -0,0 +1,63 @@
1
+ module DirModel
2
+ module Export
3
+ module Files
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ self.file_names.each do |*args|
8
+ define_skip_method(*args)
9
+ define_file_method(*args)
10
+ define_extension_method(*args)
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ # Safe to override
17
+ #
18
+ # Define default skip method for a file
19
+ # @param file_name [Symbol] the file: name
20
+ def define_skip_method(file_name)
21
+ define_method("#{file_name}_skip?") do
22
+ !self.public_send(file_name).try(:present?)
23
+ end
24
+ end
25
+
26
+ # Safe to override
27
+ #
28
+ # Define default file method for defined file
29
+ # @param file_name [Symbol] the file: name
30
+ def define_file_method(file_name)
31
+ define_method(file_name) do
32
+ file = file_or_uploader = source_model.public_send(file_name)
33
+ if file_or_uploader.respond_to?(:file) # Carrierwave Uploader
34
+ file = file_or_uploader.file
35
+ end
36
+ file
37
+ end
38
+ end
39
+
40
+ # Safe to override
41
+ #
42
+ # Define default extension method for defined file
43
+ # @param file_name [Symbol] the file: name
44
+ def define_extension_method(file_name)
45
+ define_method("#{file_name}_extension") do
46
+ return unless self.public_send(file_name).respond_to?(:extension)
47
+ self.public_send(file_name).extension # Carrierwave Uploader
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def file(file_name, options={})
54
+ super
55
+ define_skip_method(file_name)
56
+ define_file_method(file_name)
57
+ define_extension_method(file_name)
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -2,6 +2,11 @@ require 'fastimage'
2
2
 
3
3
  module DirModel
4
4
  module Export
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ include Files
9
+ end
5
10
 
6
11
  attr_reader :source_model, :context
7
12
 
@@ -30,12 +35,14 @@ module DirModel
30
35
  self.class.file_names.each do |file_name|
31
36
  options = self.class.options(file_name)
32
37
 
38
+ next if self.send("#{file_name}_skip?")
39
+
33
40
  dir_path = get_value_of(options[:path])
34
41
  file_path = File.join(dir_path, get_value_of(options[:name]))
35
42
 
36
43
  mkdir { File.join(@root_path, dir_path) }
37
44
 
38
- file_path = ensure_extension(file_path, file_name)
45
+ file_path = ensure_extension(file_path, file_name)
39
46
 
40
47
  File.open(File.join(@root_path, file_path), 'wb') {|f| f.write(self.public_send(file_name).read) }
41
48
  end
@@ -44,18 +51,19 @@ module DirModel
44
51
  end
45
52
 
46
53
  def ensure_extension(file_path, file_method_name)
54
+ return file_path if File.extname(file_path).present? # Return if extension was provided in name: attribute, i.e name: 'image.png'
55
+
47
56
  file_path_with_extension = file_path
48
57
 
49
- if self.respond_to?("#{file_method_name}_extension")
50
- file_path_with_extension = file_path + '.' + self.public_send("#{file_method_name}_extension")
51
- elsif File.extname(file_path).blank?
52
- if ext = FastImage.type(self.public_send(file_method_name))
53
- file_path_with_extension = file_path + '.' + ext.to_s
54
- else
55
- raise StandardError.new("options :name should provid an extension")
56
- end
58
+ # Looking into <file_name>_extension method
59
+ ext = self.public_send("#{file_method_name}_extension")
60
+ ext ||= FastImage.type(self.public_send(file_method_name))
61
+ unless ext
62
+ # You have to provide an extension i.e name: 'file.json
63
+ raise StandardError.new("options :name should provide an extension")
57
64
  end
58
- file_path_with_extension
65
+
66
+ file_path_with_extension = file_path + '.' + ext.to_s
59
67
  end
60
68
 
61
69
  def get_value_of(string_or_proc)
@@ -16,11 +16,6 @@ module DirModel
16
16
  file_infos[:options][:match]
17
17
  end
18
18
 
19
- def document
20
- File.open(source_path)
21
- end
22
- alias_method :image, :document
23
-
24
19
  def skip?
25
20
  load
26
21
  !@_match
@@ -1,3 +1,3 @@
1
1
  module DirModel
2
- VERSION = '0.3.4'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/dir_model.rb CHANGED
@@ -9,6 +9,7 @@ require 'dir_model/utils'
9
9
  require 'dir_model/model'
10
10
  require 'dir_model/model/files'
11
11
 
12
+ require 'dir_model/export/files'
12
13
  require 'dir_model/export'
13
14
  require 'dir_model/export/aggregate_dir'
14
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dir_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.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-06 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -74,6 +74,7 @@ files:
74
74
  - lib/dir_model.rb
75
75
  - lib/dir_model/export.rb
76
76
  - lib/dir_model/export/aggregate_dir.rb
77
+ - lib/dir_model/export/files.rb
77
78
  - lib/dir_model/import.rb
78
79
  - lib/dir_model/import/dir.rb
79
80
  - lib/dir_model/import/path.rb