procreate-swatches 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +7 -0
  3. data/.gitignore +16 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +8 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +19 -0
  8. data/.yardopts +5 -0
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/Gemfile +6 -0
  11. data/Gemfile.lock +102 -0
  12. data/LICENSE.txt +21 -0
  13. data/README.md +230 -0
  14. data/Rakefile +8 -0
  15. data/bin/console +14 -0
  16. data/bin/setup +8 -0
  17. data/doc/Procreate/Swatches/ColorsHelper.html +693 -0
  18. data/doc/Procreate/Swatches/Errors/InvalidFormat.html +220 -0
  19. data/doc/Procreate/Swatches/Errors/InvalidPath.html +220 -0
  20. data/doc/Procreate/Swatches/Errors.html +128 -0
  21. data/doc/Procreate/Swatches/Exporter.html +754 -0
  22. data/doc/Procreate/Swatches/Parser.html +622 -0
  23. data/doc/Procreate/Swatches/Wrapper.html +1168 -0
  24. data/doc/Procreate/Swatches.html +497 -0
  25. data/doc/Procreate.html +128 -0
  26. data/doc/_index.html +209 -0
  27. data/doc/class_list.html +51 -0
  28. data/doc/css/common.css +1 -0
  29. data/doc/css/full_list.css +58 -0
  30. data/doc/css/style.css +496 -0
  31. data/doc/file.CODE_OF_CONDUCT.html +146 -0
  32. data/doc/file.LICENSE.html +70 -0
  33. data/doc/file.README.html +296 -0
  34. data/doc/file_list.html +66 -0
  35. data/doc/frames.html +17 -0
  36. data/doc/index.html +296 -0
  37. data/doc/js/app.js +292 -0
  38. data/doc/js/full_list.js +216 -0
  39. data/doc/js/jquery.js +4 -0
  40. data/doc/method_list.html +275 -0
  41. data/doc/top-level-namespace.html +110 -0
  42. data/lib/procreate/swatches/colors_helper.rb +83 -0
  43. data/lib/procreate/swatches/errors/invalid_format.rb +19 -0
  44. data/lib/procreate/swatches/errors/invalid_path.rb +16 -0
  45. data/lib/procreate/swatches/exporter.rb +131 -0
  46. data/lib/procreate/swatches/parser.rb +82 -0
  47. data/lib/procreate/swatches/version.rb +8 -0
  48. data/lib/procreate/swatches/wrapper.rb +155 -0
  49. data/lib/procreate/swatches.rb +60 -0
  50. data/procreate-swatches.gemspec +41 -0
  51. metadata +275 -0
@@ -0,0 +1,275 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
5
+ <meta charset="utf-8" />
6
+
7
+ <link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" charset="utf-8" />
8
+
9
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
10
+
11
+
12
+
13
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
16
+
17
+
18
+ <title>Method List</title>
19
+ <base id="base_target" target="_parent" />
20
+ </head>
21
+ <body>
22
+ <div id="content">
23
+ <div class="fixed_header">
24
+ <h1 id="full_list_header">Method List</h1>
25
+ <div id="full_list_nav">
26
+
27
+ <span><a target="_self" href="class_list.html">
28
+ Classes
29
+ </a></span>
30
+
31
+ <span><a target="_self" href="method_list.html">
32
+ Methods
33
+ </a></span>
34
+
35
+ <span><a target="_self" href="file_list.html">
36
+ Files
37
+ </a></span>
38
+
39
+ </div>
40
+
41
+ <div id="search">Search: <input type="text" /></div>
42
+ </div>
43
+
44
+ <ul id="full_list" class="method">
45
+
46
+
47
+ <li class="odd ">
48
+ <div class="item">
49
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#<<-instance_method" title="Procreate::Swatches::Wrapper#&lt;&lt; (method)">#&lt;&lt;</a></span>
50
+ <small>Procreate::Swatches::Wrapper</small>
51
+ </div>
52
+ </li>
53
+
54
+
55
+ <li class="even ">
56
+ <div class="item">
57
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#==-instance_method" title="Procreate::Swatches::Wrapper#== (method)">#==</a></span>
58
+ <small>Procreate::Swatches::Wrapper</small>
59
+ </div>
60
+ </li>
61
+
62
+
63
+ <li class="odd ">
64
+ <div class="item">
65
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#available_color_formats-instance_method" title="Procreate::Swatches::Wrapper#available_color_formats (method)">#available_color_formats</a></span>
66
+ <small>Procreate::Swatches::Wrapper</small>
67
+ </div>
68
+ </li>
69
+
70
+
71
+ <li class="even ">
72
+ <div class="item">
73
+ <span class='object_link'><a href="Procreate/Swatches/Exporter.html#call-instance_method" title="Procreate::Swatches::Exporter#call (method)">#call</a></span>
74
+ <small>Procreate::Swatches::Exporter</small>
75
+ </div>
76
+ </li>
77
+
78
+
79
+ <li class="odd ">
80
+ <div class="item">
81
+ <span class='object_link'><a href="Procreate/Swatches/Exporter.html#call-class_method" title="Procreate::Swatches::Exporter.call (method)">call</a></span>
82
+ <small>Procreate::Swatches::Exporter</small>
83
+ </div>
84
+ </li>
85
+
86
+
87
+ <li class="even ">
88
+ <div class="item">
89
+ <span class='object_link'><a href="Procreate/Swatches/Parser.html#call-instance_method" title="Procreate::Swatches::Parser#call (method)">#call</a></span>
90
+ <small>Procreate::Swatches::Parser</small>
91
+ </div>
92
+ </li>
93
+
94
+
95
+ <li class="odd ">
96
+ <div class="item">
97
+ <span class='object_link'><a href="Procreate/Swatches/Parser.html#call-class_method" title="Procreate::Swatches::Parser.call (method)">call</a></span>
98
+ <small>Procreate::Swatches::Parser</small>
99
+ </div>
100
+ </li>
101
+
102
+
103
+ <li class="even ">
104
+ <div class="item">
105
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#colors-instance_method" title="Procreate::Swatches::Wrapper#colors (method)">#colors</a></span>
106
+ <small>Procreate::Swatches::Wrapper</small>
107
+ </div>
108
+ </li>
109
+
110
+
111
+ <li class="odd ">
112
+ <div class="item">
113
+ <span class='object_link'><a href="Procreate/Swatches.html#export-class_method" title="Procreate::Swatches.export (method)">export</a></span>
114
+ <small>Procreate::Swatches</small>
115
+ </div>
116
+ </li>
117
+
118
+
119
+ <li class="even ">
120
+ <div class="item">
121
+ <span class='object_link'><a href="Procreate/Swatches/Parser.html#file_path-instance_method" title="Procreate::Swatches::Parser#file_path (method)">#file_path</a></span>
122
+ <small>Procreate::Swatches::Parser</small>
123
+ </div>
124
+ </li>
125
+
126
+
127
+ <li class="odd ">
128
+ <div class="item">
129
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#initialize-instance_method" title="Procreate::Swatches::Wrapper#initialize (method)">#initialize</a></span>
130
+ <small>Procreate::Swatches::Wrapper</small>
131
+ </div>
132
+ </li>
133
+
134
+
135
+ <li class="even ">
136
+ <div class="item">
137
+ <span class='object_link'><a href="Procreate/Swatches/Parser.html#initialize-instance_method" title="Procreate::Swatches::Parser#initialize (method)">#initialize</a></span>
138
+ <small>Procreate::Swatches::Parser</small>
139
+ </div>
140
+ </li>
141
+
142
+
143
+ <li class="odd ">
144
+ <div class="item">
145
+ <span class='object_link'><a href="Procreate/Swatches/Exporter.html#initialize-instance_method" title="Procreate::Swatches::Exporter#initialize (method)">#initialize</a></span>
146
+ <small>Procreate::Swatches::Exporter</small>
147
+ </div>
148
+ </li>
149
+
150
+
151
+ <li class="even ">
152
+ <div class="item">
153
+ <span class='object_link'><a href="Procreate/Swatches/Errors/InvalidPath.html#message-instance_method" title="Procreate::Swatches::Errors::InvalidPath#message (method)">#message</a></span>
154
+ <small>Procreate::Swatches::Errors::InvalidPath</small>
155
+ </div>
156
+ </li>
157
+
158
+
159
+ <li class="odd ">
160
+ <div class="item">
161
+ <span class='object_link'><a href="Procreate/Swatches/Errors/InvalidFormat.html#message-instance_method" title="Procreate::Swatches::Errors::InvalidFormat#message (method)">#message</a></span>
162
+ <small>Procreate::Swatches::Errors::InvalidFormat</small>
163
+ </div>
164
+ </li>
165
+
166
+
167
+ <li class="even ">
168
+ <div class="item">
169
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#name-instance_method" title="Procreate::Swatches::Wrapper#name (method)">#name</a></span>
170
+ <small>Procreate::Swatches::Wrapper</small>
171
+ </div>
172
+ </li>
173
+
174
+
175
+ <li class="odd ">
176
+ <div class="item">
177
+ <span class='object_link'><a href="Procreate/Swatches/Exporter.html#options-instance_method" title="Procreate::Swatches::Exporter#options (method)">#options</a></span>
178
+ <small>Procreate::Swatches::Exporter</small>
179
+ </div>
180
+ </li>
181
+
182
+
183
+ <li class="even ">
184
+ <div class="item">
185
+ <span class='object_link'><a href="Procreate/Swatches.html#parse-class_method" title="Procreate::Swatches.parse (method)">parse</a></span>
186
+ <small>Procreate::Swatches</small>
187
+ </div>
188
+ </li>
189
+
190
+
191
+ <li class="odd ">
192
+ <div class="item">
193
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#pop-instance_method" title="Procreate::Swatches::Wrapper#pop (method)">#pop</a></span>
194
+ <small>Procreate::Swatches::Wrapper</small>
195
+ </div>
196
+ </li>
197
+
198
+
199
+ <li class="even ">
200
+ <div class="item">
201
+ <span class='object_link'><a href="Procreate/Swatches/ColorsHelper.html#prepare_color_for_push-instance_method" title="Procreate::Swatches::ColorsHelper#prepare_color_for_push (method)">#prepare_color_for_push</a></span>
202
+ <small>Procreate::Swatches::ColorsHelper</small>
203
+ </div>
204
+ </li>
205
+
206
+
207
+ <li class="odd ">
208
+ <div class="item">
209
+ <span class='object_link'><a href="Procreate/Swatches/Exporter.html#swatches_path-instance_method" title="Procreate::Swatches::Exporter#swatches_path (method)">#swatches_path</a></span>
210
+ <small>Procreate::Swatches::Exporter</small>
211
+ </div>
212
+ </li>
213
+
214
+
215
+ <li class="even ">
216
+ <div class="item">
217
+ <span class='object_link'><a href="Procreate/Swatches/ColorsHelper.html#to_chroma_hsv-instance_method" title="Procreate::Swatches::ColorsHelper#to_chroma_hsv (method)">#to_chroma_hsv</a></span>
218
+ <small>Procreate::Swatches::ColorsHelper</small>
219
+ </div>
220
+ </li>
221
+
222
+
223
+ <li class="odd ">
224
+ <div class="item">
225
+ <span class='object_link'><a href="Procreate/Swatches/ColorsHelper.html#to_color_json-instance_method" title="Procreate::Swatches::ColorsHelper#to_color_json (method)">#to_color_json</a></span>
226
+ <small>Procreate::Swatches::ColorsHelper</small>
227
+ </div>
228
+ </li>
229
+
230
+
231
+ <li class="even ">
232
+ <div class="item">
233
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#to_file-instance_method" title="Procreate::Swatches::Wrapper#to_file (method)">#to_file</a></span>
234
+ <small>Procreate::Swatches::Wrapper</small>
235
+ </div>
236
+ </li>
237
+
238
+
239
+ <li class="odd ">
240
+ <div class="item">
241
+ <span class='object_link'><a href="Procreate/Swatches/Wrapper.html#to_json-instance_method" title="Procreate::Swatches::Wrapper#to_json (method)">#to_json</a></span>
242
+ <small>Procreate::Swatches::Wrapper</small>
243
+ </div>
244
+ </li>
245
+
246
+
247
+ <li class="even ">
248
+ <div class="item">
249
+ <span class='object_link'><a href="Procreate/Swatches/ColorsHelper.html#to_swatches_json-instance_method" title="Procreate::Swatches::ColorsHelper#to_swatches_json (method)">#to_swatches_json</a></span>
250
+ <small>Procreate::Swatches::ColorsHelper</small>
251
+ </div>
252
+ </li>
253
+
254
+
255
+ <li class="odd ">
256
+ <div class="item">
257
+ <span class='object_link'><a href="Procreate/Swatches/Exporter.html#wrapper-instance_method" title="Procreate::Swatches::Exporter#wrapper (method)">#wrapper</a></span>
258
+ <small>Procreate::Swatches::Exporter</small>
259
+ </div>
260
+ </li>
261
+
262
+
263
+ <li class="even ">
264
+ <div class="item">
265
+ <span class='object_link'><a href="Procreate/Swatches/Parser.html#wrapper-instance_method" title="Procreate::Swatches::Parser#wrapper (method)">#wrapper</a></span>
266
+ <small>Procreate::Swatches::Parser</small>
267
+ </div>
268
+ </li>
269
+
270
+
271
+
272
+ </ul>
273
+ </div>
274
+ </body>
275
+ </html>
@@ -0,0 +1,110 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.16
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Procreate.html" title="Procreate (module)">Procreate</a></span>
86
+
87
+
88
+
89
+
90
+ </p>
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+ </div>
101
+
102
+ <div id="footer">
103
+ Generated on Sat Apr 27 09:32:32 2019 by
104
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
+ 0.9.16 (ruby-2.4.3).
106
+ </div>
107
+
108
+ </div>
109
+ </body>
110
+ </html>
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Procreate
4
+ module Swatches
5
+ # Helper module to interact with colors
6
+ module ColorsHelper
7
+ # +.swatches+ files can hold a maximum of 30 colors.
8
+ SWATCHES_MAX_SIZE = 30
9
+ # Procreate +.swatches+ don't consider the opacity, therefore a default value of 1 is used.
10
+ SWATCHES_ALPHA = 1
11
+ SWATCHES_COLOR_SPACE = 0
12
+ SELECTED_KEYS = %w[hue saturation brightness].freeze
13
+
14
+ # Transforms the {https://github.com/jfairbank/chroma Chroma::Color} color be exported to the +.swatches+ file.
15
+ #
16
+ # @param [Chroma::Color] color Color
17
+ #
18
+ # @return [Hash{hue => Float, saturation => Float, brightness => Float, alpha => Integer, colorSpace => Integer}]
19
+ #
20
+ def to_swatches_json(color)
21
+ hsv_color = color.hsv
22
+
23
+ hue = hsv_color.h / 360
24
+ saturation = hsv_color.s
25
+ brightness = hsv_color.v
26
+
27
+ to_color_json(hue, saturation, brightness)
28
+ end
29
+
30
+ #
31
+ # Transforms the HSV (HSB) parameters into
32
+ # a hash, to be exported to the +.swatches+ file.
33
+ #
34
+ # @param [Float, Integer] hue Hue
35
+ # @param [Float, Integer] saturation Saturation
36
+ # @param [Float, Integer] brightness Brightness/Value
37
+ #
38
+ # @return [Hash{hue => Float, saturation => Float, brightness => Float, alpha => Integer, colorSpace => Integer}]
39
+ #
40
+ def to_color_json(hue, saturation, brightness)
41
+ {
42
+ hue: hue,
43
+ saturation: saturation,
44
+ brightness: brightness,
45
+ alpha: SWATCHES_ALPHA,
46
+ colorSpace: SWATCHES_COLOR_SPACE
47
+ }
48
+ end
49
+
50
+ #
51
+ # Generates a string formatted to initialize a {https://github.com/jfairbank/chroma Chroma::Color} from HSV values
52
+ #
53
+ # @param [Hash{hue => Float, saturation => Float, brightness => Float}] color Color as hash (from parsed +.swatches+ file)
54
+ #
55
+ # @return [String] string String formatted to initialize a {https://github.com/jfairbank/chroma Chroma::Color} from HSV values
56
+ #
57
+ def to_chroma_hsv(color)
58
+ hue, saturation, brightness = *color.values_at(*SELECTED_KEYS)
59
+ hue, saturation, brightness = *[hue * 360, saturation * 100, brightness * 100].map(&:round)
60
+
61
+ +"hsv(#{hue}, #{saturation}%, #{brightness}%)"
62
+ end
63
+
64
+ #
65
+ # Checks a "color" before adding it to a {Procreate::Swatches::Wrapper} instance's colors array.
66
+ #
67
+ # @param [Chroma::Color, String] color A string or a {https://github.com/jfairbank/chroma Chroma::Color}
68
+ #
69
+ # @return [Chroma::Color] Return a {https://github.com/jfairbank/chroma Chroma::Color} if the color is already an instance of {https://github.com/jfairbank/chroma Chroma::Color}, or the string can be converted to one.
70
+ # @return [nil] Returns nil when {https://github.com/jfairbank/chroma Chroma::Color} can't build a color object from the provided string
71
+ #
72
+ def prepare_color_for_push(color)
73
+ return color if color.is_a?(Chroma::Color)
74
+
75
+ begin
76
+ Chroma::Color.new(color.dup)
77
+ rescue Chroma::Errors::UnrecognizedColor
78
+ nil
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Procreate
4
+ module Swatches
5
+ #
6
+ # Errors used for {Procreate::Swatches}
7
+ #
8
+ module Errors
9
+ # Exception raised when the provided file format is invalid.
10
+ class InvalidFormat < StandardError
11
+ MESSAGE = 'Invalid file format specified. Accepted file format: `.swatches`.'
12
+
13
+ def message
14
+ MESSAGE
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Procreate
4
+ module Swatches
5
+ module Errors
6
+ # Exception raised when the specified path is invalid.
7
+ class InvalidPath < StandardError
8
+ MESSAGE = 'Invalid file path specified.'
9
+
10
+ def message
11
+ MESSAGE
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+ require 'zip'
5
+
6
+ module Procreate
7
+ module Swatches
8
+ #
9
+ # The class that handles the export of a {Procreate::Swatches::Wrapper} to a
10
+ # +.swatches+ file.
11
+ #
12
+ class Exporter
13
+ include CallableClass
14
+
15
+ # @!method self.call
16
+ # Creates a new instance of {Procreate::Swatches::Exporter} and calls {#call}
17
+ # Dynamically included by using {https://github.com/laurentzziu/callable_class CallableClass} gem.
18
+
19
+ # An instance of {Procreate::Swatches::Wrapper}
20
+ attr_accessor :wrapper
21
+ # The computed path of the +.swatches+ file, after the file was exported.
22
+ attr_reader :swatches_path
23
+
24
+ #
25
+ # Initialize a new {Exporter}
26
+ #
27
+ # @param [Procreate::Swatches::Wrapper] wrapper Wrapper instance
28
+ # @param [Hash] options Options for exporting the wrapper
29
+ # @option options [String] :export_directory ('Dir.pwd') The export directory for the +.swatches+ file
30
+ # @option options [String] :file_name ('Wrapper#name') Custom file name for the exported +.swatches+ file. If none is provided, the +name+ of the +wrapper+ instance is used
31
+ #
32
+ def initialize(wrapper, options = {})
33
+ @wrapper = wrapper
34
+ parse_options(options)
35
+ end
36
+
37
+ # Exports the +.swatches+ file.
38
+ #
39
+ # @return [String] swatches_path Path of the exported +.swatches+ file
40
+ def call
41
+ zip_content!
42
+
43
+ swatches_path
44
+ end
45
+
46
+ alias export call
47
+
48
+ # @!attribute [r] options
49
+ # Export options
50
+ # @return [Hash] Export options
51
+ def options
52
+ parse_options(@options)
53
+ end
54
+
55
+ # @!attribute [w] options
56
+ # Export options
57
+ # @return [Hash] Export options
58
+ def options=(hash)
59
+ parse_options(hash)
60
+ end
61
+
62
+ # Permitted options. Any other key will be discarded.
63
+ PERMITTED_OPTIONS = %i[export_directory file_name].freeze
64
+
65
+ private
66
+
67
+ DEFAULT_OPTIONS = {
68
+ export_directory: Dir.pwd
69
+ }.freeze
70
+
71
+ def parse_options(options)
72
+ @options = DEFAULT_OPTIONS.merge(
73
+ options.symbolize_keys.slice(*PERMITTED_OPTIONS)
74
+ )
75
+ end
76
+
77
+ def create_temp_swatches_file!
78
+ json_content = wrapper.to_json
79
+ temp_file = Tempfile.new(wrapper.name)
80
+ temp_file << json_content
81
+ temp_file.rewind
82
+ temp_file.path
83
+ end
84
+
85
+ def zip_content!
86
+ temp_file = create_temp_swatches_file!
87
+
88
+ Zip::File.open(generate_unique_filename, Zip::File::CREATE) do |zipfile|
89
+ zipfile.add(SWATCHES_FILE_NAME, temp_file)
90
+ end
91
+ end
92
+
93
+ # http://myprogrammingblog.com/2015/01/05/how-to-ensure-that-filename-is-always-unique-ruby/
94
+ #
95
+ # Function generates uniq file name from the String passed to it
96
+ # based on extension and basename
97
+ #
98
+ # @return [String] generated file name
99
+ def generate_unique_filename
100
+ name = options[:file_name] || wrapper.name
101
+ # TODO: Sanitize the file name
102
+
103
+ filename = "#{name}.swatches"
104
+
105
+ related_files = related_file_indexes(filename)
106
+
107
+ filename = if related_files.present?
108
+ "#{name}-#{related_files.max + 1}#{SWATCHES_EXTENSION}"
109
+ else
110
+ "#{name}#{SWATCHES_EXTENSION}"
111
+ end
112
+
113
+ @swatches_path = File.join(options[:export_directory], filename)
114
+ end
115
+
116
+ SWATCHES_EXTENSION = '.swatches'
117
+
118
+ def related_file_indexes(file_name)
119
+ name = File.basename(file_name, SWATCHES_EXTENSION)
120
+
121
+ existing_swatches_files(file_name).map do |file|
122
+ file.split('-').last.to_i if File.basename(file).include?(name) && File.extname(file) == SWATCHES_EXTENSION
123
+ end.compact
124
+ end
125
+
126
+ def existing_swatches_files(file_name)
127
+ Dir[Pathname.new(File.dirname(file_name)).join('*')]
128
+ end
129
+ end
130
+ end
131
+ end