shipping_materials 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -7
- data/lib/shipping_materials.rb +0 -1
- data/lib/shipping_materials/config.rb +0 -8
- data/lib/shipping_materials/csv_dsl.rb +41 -29
- data/lib/shipping_materials/packager.rb +23 -15
- data/lib/shipping_materials/packing_slips.rb +2 -9
- data/lib/shipping_materials/sorter.rb +11 -11
- data/lib/shipping_materials/storage.rb +2 -1
- data/lib/shipping_materials/version.rb +1 -1
- data/shipping_materials.gemspec +0 -1
- data/test/files/template.erb +14 -0
- data/test/models.rb +4 -0
- data/test/unit/csv_dsl_test.rb +18 -8
- data/test/unit/packager_test.rb +1 -1
- data/test/unit/packing_slips_test.rb +0 -1
- metadata +5 -19
- data/test/files/template.mustache +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd786b238dd207df0bd5c80582afab3496b2f410
|
4
|
+
data.tar.gz: a70915402894014d982e03e36311f296e4a36f74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a215b7f887748fe776e89e3ae3f9d39c0bcbd4f00b3c52eb95574d009a3c302ae10b3ab841b2e76450b0153684212b35f31d85aad2979d65fe5b94cfc3436142
|
7
|
+
data.tar.gz: 6d557cba658f4f8c32256072649a4f48e37a5596f7a29bbf1feded9e9a631635f6e27dabb76bf39b2c28690f02285e65b37c6079ad4005e99c52ad35526c360d
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ includes packing slips and CSVs for label makers.
|
|
11
11
|
## Dependencies
|
12
12
|
|
13
13
|
`wkhtmltopdf` if used for PDF generation. The call to it is made as a linux
|
14
|
-
command therefore this
|
14
|
+
command therefore this functionality will not work on Windows.
|
15
15
|
|
16
16
|
`gzip` is used for the zip functionality.
|
17
17
|
|
@@ -40,7 +40,7 @@ If you would like to use S3, add the following:
|
|
40
40
|
|
41
41
|
### The Packager
|
42
42
|
|
43
|
-
The DSL is provided via the ShippingMaterials::Packager class.
|
43
|
+
The DSL is provided via the `ShippingMaterials::Packager` class.
|
44
44
|
|
45
45
|
```ruby
|
46
46
|
packager = ShippingMaterials::Packager.new
|
@@ -59,7 +59,7 @@ type.
|
|
59
59
|
|
60
60
|
Because we are creating shipping materials here, at the very least, it is
|
61
61
|
assumed you are going to want packing slips. You may specify a global template
|
62
|
-
with the `#pdf`
|
62
|
+
with the `#html` or `#pdf` methods:
|
63
63
|
|
64
64
|
```ruby
|
65
65
|
packager.package orders do
|
@@ -111,7 +111,7 @@ context of the order array. A sample ERB template would look like this:
|
|
111
111
|
<p><%= order.number %>
|
112
112
|
<div>
|
113
113
|
<% order.line_items.each do |li| %>
|
114
|
-
<p><%=
|
114
|
+
<p><%= li.desc %>: $<%= li.price %> x <%= li.qty %> = <%= li.total %></p>
|
115
115
|
<% end %>
|
116
116
|
</div>
|
117
117
|
<% end %>
|
@@ -135,11 +135,11 @@ Here is an example with hashes:
|
|
135
135
|
```ruby
|
136
136
|
group 'Canadian Standard Post' do
|
137
137
|
csv(headers: true) {
|
138
|
-
row 'Code'
|
138
|
+
row 'Code' => 'Q',
|
139
139
|
'Order Number' => :number,
|
140
|
-
'Name'
|
140
|
+
'Name' => [ :shipping_address, :name ]
|
141
141
|
# ...
|
142
|
-
'Country'
|
142
|
+
'Country' => [ :shipping_address, :country, :iso ]
|
143
143
|
|
144
144
|
row line_items: [ 'H', :id, :name, :quantity, :price ]
|
145
145
|
}
|
@@ -159,6 +159,24 @@ As demonstrated in the second call to row, you can evalute your row in the
|
|
159
159
|
context of your line items (or other one-to-many relationship) using its method
|
160
160
|
name as a key.
|
161
161
|
|
162
|
+
### CSV Callbacks
|
163
|
+
|
164
|
+
Right now an `if` callback can be provided to the `row` method. This is useful
|
165
|
+
when calling row multiple times and you don't always want all of them to render.
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
group 'Canadian Standard Post' do
|
169
|
+
csv(headers: true) {
|
170
|
+
row 'Code' => 'Q'
|
171
|
+
# ...
|
172
|
+
|
173
|
+
row({ line_items: [ 'H', :id, :name, :quantity, :price ] },
|
174
|
+
if: proc {|o| o.lines_items.size > 1 })
|
175
|
+
}
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
|
162
180
|
### Sorting
|
163
181
|
|
164
182
|
While most sorting should probably be done at the query level, Shipping
|
data/lib/shipping_materials.rb
CHANGED
@@ -6,14 +6,6 @@ module ShippingMaterials
|
|
6
6
|
:s3_secret,
|
7
7
|
:gzip_file_name
|
8
8
|
|
9
|
-
def base_context
|
10
|
-
@base_context || :objects
|
11
|
-
end
|
12
|
-
|
13
|
-
def base_context=(bc)
|
14
|
-
@base_context = bc.to_sym
|
15
|
-
end
|
16
|
-
|
17
9
|
def save_path=(save_path)
|
18
10
|
@save_path = save_path.sub(/(\/)+$/, '')
|
19
11
|
end
|
@@ -3,29 +3,31 @@ module ShippingMaterials
|
|
3
3
|
require 'csv'
|
4
4
|
|
5
5
|
attr_accessor :objects, :row_maps
|
6
|
+
attr_reader :headers
|
6
7
|
|
7
8
|
def initialize(objects, options={})
|
8
|
-
@objects
|
9
|
-
@row_maps
|
10
|
-
@options
|
9
|
+
@objects = objects
|
10
|
+
@row_maps = {}
|
11
|
+
@options = options
|
11
12
|
end
|
12
13
|
|
13
14
|
# This method is on the complex side. It is a DSL method that
|
14
15
|
# performs type-checking and also sets the headers.
|
15
16
|
# Be sure to see headers=() defined below
|
16
|
-
def row(collection)
|
17
|
+
def row(collection, callbacks={})
|
18
|
+
@callbacks = callbacks
|
17
19
|
if collection.is_a? Array
|
18
|
-
|
20
|
+
update_row_maps(:object, collection, callbacks)
|
19
21
|
elsif collection.is_a? Hash
|
20
22
|
f = collection.first
|
21
23
|
if f[1].is_a? Array
|
22
|
-
|
24
|
+
update_row_maps(f[0], f[1], callbacks)
|
23
25
|
elsif f[1].is_a? Hash
|
24
|
-
self.headers =
|
25
|
-
|
26
|
+
self.headers = f[1]
|
27
|
+
update_row_maps(f[0], f[1].values, callbacks)
|
26
28
|
else
|
27
29
|
self.headers = collection
|
28
|
-
|
30
|
+
update_row_maps(:object, collection.values, callbacks)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
@@ -34,29 +36,25 @@ module ShippingMaterials
|
|
34
36
|
CSV.generate do |csv|
|
35
37
|
csv << headers if headers?
|
36
38
|
@objects.each do |object|
|
37
|
-
@row_maps.each do |context,
|
39
|
+
@row_maps.each do |context, stuff|
|
40
|
+
next unless apply_callbacks(stuff[:callbacks], object)
|
38
41
|
if context == :object
|
39
|
-
csv << get_row(object,
|
42
|
+
csv << get_row(object, stuff[:values])
|
40
43
|
else
|
41
|
-
object.send(context).each do |
|
42
|
-
csv << get_row(
|
44
|
+
object.send(context).each do |obj|
|
45
|
+
csv << get_row(obj, stuff[:values])
|
43
46
|
end
|
44
47
|
end
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
49
|
-
|
50
52
|
alias_method :to_s, :to_csv
|
51
53
|
|
52
54
|
def extension
|
53
55
|
@options[:extension] || 'csv'
|
54
56
|
end
|
55
57
|
|
56
|
-
def headers
|
57
|
-
@headers
|
58
|
-
end
|
59
|
-
|
60
58
|
def headers=(object)
|
61
59
|
@headers ||= object.keys.map {|h| h.to_s } if self.headers?
|
62
60
|
end
|
@@ -66,19 +64,33 @@ module ShippingMaterials
|
|
66
64
|
end
|
67
65
|
|
68
66
|
private
|
67
|
+
def apply_callbacks(callbacks, object)
|
68
|
+
return true unless callbacks.any?
|
69
|
+
if callbacks[:if]
|
70
|
+
callbacks[:if].call(object)
|
71
|
+
else
|
72
|
+
true
|
73
|
+
end
|
74
|
+
end
|
69
75
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
def get_row(object, methods)
|
77
|
+
methods.map do |meth|
|
78
|
+
if meth.is_a? Symbol
|
79
|
+
object.send(meth)
|
80
|
+
elsif meth.is_a? Array
|
81
|
+
meth.reduce(object) {|o,m| o.send(m) }
|
82
|
+
elsif meth.is_a? Proc
|
83
|
+
object.instance_eval(&meth)
|
84
|
+
elsif meth.is_a? String
|
85
|
+
meth
|
86
|
+
end
|
80
87
|
end
|
81
88
|
end
|
82
|
-
|
89
|
+
|
90
|
+
def update_row_maps(key, values, callbacks)
|
91
|
+
@row_maps[key] ||= {}
|
92
|
+
@row_maps[key][:values] = values
|
93
|
+
@row_maps[key][:callbacks] = callbacks
|
94
|
+
end
|
83
95
|
end
|
84
96
|
end
|
@@ -17,11 +17,20 @@ module ShippingMaterials
|
|
17
17
|
create_packing_slips(group)
|
18
18
|
create_csvs(group)
|
19
19
|
end
|
20
|
-
|
20
|
+
if Config.use_gzip?
|
21
|
+
Storage.write_gzip rescue nil
|
22
|
+
end
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def html(template)
|
27
|
+
@packing_slip_template = template
|
28
|
+
@packing_slip_ext = :html
|
21
29
|
end
|
22
30
|
|
23
31
|
def pdf(template)
|
24
32
|
@packing_slip_template = template
|
33
|
+
@packing_slip_ext = :pdf
|
25
34
|
end
|
26
35
|
|
27
36
|
def group(basename, &block)
|
@@ -31,22 +40,21 @@ module ShippingMaterials
|
|
31
40
|
end
|
32
41
|
|
33
42
|
private
|
43
|
+
def sort_group(group)
|
44
|
+
group.sorters ||= self.sorters
|
45
|
+
group.sort!
|
46
|
+
end
|
34
47
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
def create_packing_slips(group)
|
42
|
-
packing_slip = PackingSlips.new(group.objects, @packing_slip_template)
|
43
|
-
Storage.write_pdf(group.basename, packing_slip.to_s)
|
44
|
-
end
|
48
|
+
def create_packing_slips(group)
|
49
|
+
packing_slip = PackingSlips.new(group.objects, @packing_slip_template)
|
50
|
+
filename = "#{group.basename}.#{@packing_slip_ext}"
|
51
|
+
Storage.send(:"write_#{@packing_slip_ext}", filename , packing_slip.to_s)
|
52
|
+
end
|
45
53
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
54
|
+
def create_csvs(group)
|
55
|
+
group.csvs.each do |csv|
|
56
|
+
filename = Storage.filenameize(group.basename) + '.' + csv.extension
|
57
|
+
Storage.write_file(filename, csv.to_s)
|
50
58
|
end
|
51
59
|
end
|
52
60
|
end
|
@@ -6,15 +6,8 @@ module ShippingMaterials
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def to_s
|
9
|
-
|
10
|
-
|
11
|
-
t.template_file = @template_file
|
12
|
-
t[Config.base_context] = @objects
|
13
|
-
t.render
|
14
|
-
else
|
15
|
-
t = Tilt.new(@template_file)
|
16
|
-
t.render(@objects)
|
17
|
-
end
|
9
|
+
t = Tilt.new(@template_file)
|
10
|
+
t.render(@objects)
|
18
11
|
end
|
19
12
|
|
20
13
|
alias_method :to_html, :to_s
|
@@ -2,7 +2,7 @@ module ShippingMaterials
|
|
2
2
|
class Sorter
|
3
3
|
def initialize
|
4
4
|
@rules = []
|
5
|
-
|
5
|
+
@attr_callbacks = []
|
6
6
|
end
|
7
7
|
|
8
8
|
def rule(&block)
|
@@ -28,22 +28,22 @@ module ShippingMaterials
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
apply_callbacks(a)
|
32
|
+
apply_callbacks(b)
|
33
33
|
|
34
34
|
i += 1
|
35
35
|
( sort(a, i) + sort(b, i) ).compact
|
36
36
|
end
|
37
37
|
|
38
38
|
|
39
|
-
|
39
|
+
private
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
41
|
+
def apply_callbacks(items)
|
42
|
+
@attr_callbacks.each do |attr|
|
43
|
+
items.sort! do |a,b|
|
44
|
+
b.send(attr) <=> a.send(attr)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
48
|
end
|
49
49
|
end
|
@@ -12,6 +12,7 @@ module ShippingMaterials
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
15
|
+
alias_method :write_html, :write_file
|
15
16
|
|
16
17
|
def write_pdf(filename, contents)
|
17
18
|
basename = filenameize(File.basename(filename, '.*'))
|
@@ -29,7 +30,7 @@ module ShippingMaterials
|
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
32
|
-
def
|
33
|
+
def write_gzip
|
33
34
|
filename = "#{Config.save_path}/#{Config.gzip_file_name}"
|
34
35
|
`cd #{Config.save_path} && tar -cvzf #{filename} * && cd -`
|
35
36
|
if Config.use_s3?
|
data/shipping_materials.gemspec
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
<html>
|
2
|
+
<body>
|
3
|
+
<% self.each do |order| %>
|
4
|
+
<div>
|
5
|
+
<h1><%= order.id %></h1>
|
6
|
+
<ul>
|
7
|
+
<% order.line_items.each do |li| %>
|
8
|
+
<li><%= li.name %> - <%= li.quantity %> x $<%= li.price %> = $<%= li.total %></li>
|
9
|
+
<% end %>
|
10
|
+
</ul>
|
11
|
+
</div>
|
12
|
+
<% end %>
|
13
|
+
</body>
|
14
|
+
</html>
|
data/test/models.rb
CHANGED
data/test/unit/csv_dsl_test.rb
CHANGED
@@ -8,21 +8,21 @@ module ShippingMaterials
|
|
8
8
|
|
9
9
|
@csv.row(array)
|
10
10
|
|
11
|
-
assert_equal array, @csv.row_maps[:object],
|
11
|
+
assert_equal array, @csv.row_maps[:object][:values],
|
12
12
|
"Passing array to CSVDSL#row does not work"
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_row_hash
|
16
16
|
@csv = ShippingMaterials::CSVDSL.new(orders, headers: true)
|
17
17
|
hash = { :order_id => :id,
|
18
|
-
|
19
|
-
|
18
|
+
:name => :name,
|
19
|
+
:static_field => 'A string' }
|
20
20
|
|
21
21
|
@csv.row(hash)
|
22
22
|
|
23
23
|
assert_equal %w(order_id name static_field), @csv.headers,
|
24
24
|
"CSVDSL headers are not being set properly"
|
25
|
-
assert_equal hash.values, @csv.row_maps[:object],
|
25
|
+
assert_equal hash.values, @csv.row_maps[:object][:values],
|
26
26
|
"CSVDSL row map values not being properly set"
|
27
27
|
end
|
28
28
|
|
@@ -32,7 +32,7 @@ module ShippingMaterials
|
|
32
32
|
|
33
33
|
@csv.row(hash)
|
34
34
|
|
35
|
-
assert_equal hash[:line_items], @csv.row_maps[:line_items],
|
35
|
+
assert_equal hash[:line_items], @csv.row_maps[:line_items][:values],
|
36
36
|
"CSVDSL row map not being set properly when given " \
|
37
37
|
"hash with array"
|
38
38
|
end
|
@@ -51,7 +51,7 @@ module ShippingMaterials
|
|
51
51
|
|
52
52
|
assert_equal %w(order_id name static_field), @csv.headers,
|
53
53
|
"CSVDSL headers should be strings"
|
54
|
-
assert_equal hash[:line_items].values, @csv.row_maps[:line_items],
|
54
|
+
assert_equal hash[:line_items].values, @csv.row_maps[:line_items][:values],
|
55
55
|
"CSVDSL values should match line_item values"
|
56
56
|
|
57
57
|
end
|
@@ -104,11 +104,21 @@ module ShippingMaterials
|
|
104
104
|
assert_equal "1+Andrew\n", @csv.to_csv, "CSV with proc doesn't work"
|
105
105
|
end
|
106
106
|
|
107
|
+
def test_callbacks
|
108
|
+
@csv = CSVDSL.new(orders.select {|o| o.id == 1 })
|
109
|
+
@csv.row :order_id => :id,
|
110
|
+
:name => :name,
|
111
|
+
:static_fields => 'A string'
|
112
|
+
@csv.row({ :line_items => [:id, :name, :quantity] }, :if => proc {|o| o.line_items.size < 1 })
|
113
|
+
|
114
|
+
assert_equal "1,Andrew,A string\n", @csv.to_csv
|
115
|
+
end
|
116
|
+
|
107
117
|
def test_to_csv
|
108
118
|
@csv = CSVDSL.new(orders.select {|o| o.id == 1 }, headers: true)
|
109
119
|
@csv.row :order_id => :id,
|
110
|
-
|
111
|
-
|
120
|
+
:name => :name,
|
121
|
+
:static_fields => 'A string'
|
112
122
|
|
113
123
|
@csv.row :line_items => [:id, :name, :quantity]
|
114
124
|
|
data/test/unit/packager_test.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shipping_materials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Haust
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: mustache
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '>='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: aws-sdk
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,7 +108,7 @@ files:
|
|
122
108
|
- lib/shipping_materials/version.rb
|
123
109
|
- shipping_materials.gemspec
|
124
110
|
- test/data.rb
|
125
|
-
- test/files/template.
|
111
|
+
- test/files/template.erb
|
126
112
|
- test/files/test.html
|
127
113
|
- test/models.rb
|
128
114
|
- test/suite.rb
|
@@ -155,13 +141,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
141
|
version: '0'
|
156
142
|
requirements: []
|
157
143
|
rubyforge_project:
|
158
|
-
rubygems_version: 2.0.
|
144
|
+
rubygems_version: 2.0.6
|
159
145
|
signing_key:
|
160
146
|
specification_version: 4
|
161
147
|
summary: Shipping Materials
|
162
148
|
test_files:
|
163
149
|
- test/data.rb
|
164
|
-
- test/files/template.
|
150
|
+
- test/files/template.erb
|
165
151
|
- test/files/test.html
|
166
152
|
- test/models.rb
|
167
153
|
- test/suite.rb
|