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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6dec67506b0acc185f46cbfd415f5b26c032b1b0
4
- data.tar.gz: 28c181e09c09451fa77ec82f9f6cd8e75e5b733b
3
+ metadata.gz: dd786b238dd207df0bd5c80582afab3496b2f410
4
+ data.tar.gz: a70915402894014d982e03e36311f296e4a36f74
5
5
  SHA512:
6
- metadata.gz: e21ecd0979f492d4dec669d1ece80931035b451687b0e6cef458c9404b15d737a1287e9617e319771a8f5d2c4f9ca9c217492aa589b69012f5b2b8329f8fa398
7
- data.tar.gz: 1799f53d73c0ede957138f1c7fb4c6386a8e13b13548dc8e6600dd2bcd6a6db5c577ed38ed1a8793cffd1c2f804e5b56d66d2c0905765fe08b530367fd1b40a7
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 plugin will not work on Windows yet.
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` method:
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><%= line_item.desc %>: $<%= li.price %> x <%= li.qty %> = <%= li.total %></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' => 'Q',
138
+ row 'Code' => 'Q',
139
139
  'Order Number' => :number,
140
- 'Name' => [ :shipping_address, :name ]
140
+ 'Name' => [ :shipping_address, :name ]
141
141
  # ...
142
- 'Country' => [ :shipping_address, :country, :iso ]
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
@@ -1,5 +1,4 @@
1
1
  require 'tilt'
2
- require 'mustache'
3
2
 
4
3
  require 'shipping_materials/version'
5
4
  require 'shipping_materials/mixins/sortable'
@@ -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 = objects
9
- @row_maps = {}
10
- @options = 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
- @row_maps[:object] = collection
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
- @row_maps[f[0]] = f[1]
24
+ update_row_maps(f[0], f[1], callbacks)
23
25
  elsif f[1].is_a? Hash
24
- self.headers = collection.first[1]
25
- @row_maps[f[0]] = f[1].values
26
+ self.headers = f[1]
27
+ update_row_maps(f[0], f[1].values, callbacks)
26
28
  else
27
29
  self.headers = collection
28
- @row_maps[:object] = collection.values
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, methods|
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, methods)
42
+ csv << get_row(object, stuff[:values])
40
43
  else
41
- object.send(context).each do |c|
42
- csv << get_row(c, methods)
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
- def get_row(object, methods)
71
- methods.map do |meth|
72
- if meth.is_a? Symbol
73
- object.send(meth)
74
- elsif meth.is_a? Array
75
- meth.reduce(object) {|obj, m| obj.send(m) }
76
- elsif meth.is_a? Proc
77
- object.instance_eval(&meth)
78
- elsif meth.is_a? String
79
- meth
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
- end
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
- Storage.gzip if Config.use_gzip?
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
- def sort_group(group)
36
- return if self.sorters.empty?
37
- group.sorters ||= self.sorters
38
- group.sort!
39
- end
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
- def create_csvs(group)
47
- group.csvs.each do |csv|
48
- filename = Storage.filenameize(group.basename) + '.' + csv.extension
49
- Storage.write_file(filename, csv.to_s)
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
- if File.extname(@template_file) == '.mustache'
10
- t = Mustache.new
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
- @attr_callbacks = []
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
- apply_callbacks(a)
32
- apply_callbacks(b)
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
- private
39
+ private
40
40
 
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
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 gzip
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?
@@ -1,3 +1,3 @@
1
1
  module ShippingMaterials
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency 'mustache'
23
22
  spec.add_dependency 'aws-sdk'
24
23
  spec.add_dependency 'tilt'
25
24
 
@@ -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
@@ -10,6 +10,10 @@ module TestModels
10
10
 
11
11
  class LineItem
12
12
  attr_accessor :id, :name, :quantity, :price, :type, :variant
13
+
14
+ def total
15
+ quantity * price
16
+ end
13
17
  end
14
18
 
15
19
  class Variant
@@ -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
- :name => :name,
19
- :static_field => 'A string' }
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
- :name => :name,
111
- :static_fields => 'A string'
120
+ :name => :name,
121
+ :static_fields => 'A string'
112
122
 
113
123
  @csv.row :line_items => [:id, :name, :quantity]
114
124
 
@@ -16,7 +16,7 @@ module ShippingMaterials
16
16
 
17
17
  def test_dsl
18
18
  @packager.package orders do
19
- pdf './test/files/template.mustache'
19
+ html './test/files/template.erb'
20
20
 
21
21
  sort(:line_items) {
22
22
  rule { type == 'Vinyl' }
@@ -3,7 +3,6 @@ require 'test_helper'
3
3
  module ShippingMaterials
4
4
  class PackingSlipsTest < TestCase
5
5
  def test_basic_stuff
6
- ps = ShippingMaterials::PackingSlips.new(orders, './test/files/template.mustache')
7
6
  end
8
7
  end
9
8
  end
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.2
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-08-05 00:00:00.000000000 Z
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.mustache
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.3
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.mustache
150
+ - test/files/template.erb
165
151
  - test/files/test.html
166
152
  - test/models.rb
167
153
  - test/suite.rb
@@ -1,16 +0,0 @@
1
- <html>
2
- <head>
3
- <style>
4
- </style>
5
- </head>
6
- <body>
7
- {{# objects }}
8
- <div>
9
- {{ name }}, {{ country }}
10
- {{# line_items }}
11
- {{ name }}
12
- {{/ line_items }}
13
- </div>
14
- {{/ objects }}
15
- </body>
16
- </html>