shipping_materials 0.0.1 → 0.0.2

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: 09d4424dc7c0604733b0a036401f36ce73e2c18c
4
- data.tar.gz: 8775e800e7580de9203ad819bb4c8337d6444f7c
3
+ metadata.gz: 6dec67506b0acc185f46cbfd415f5b26c032b1b0
4
+ data.tar.gz: 28c181e09c09451fa77ec82f9f6cd8e75e5b733b
5
5
  SHA512:
6
- metadata.gz: 001907c0d0a1ebda644b76221a44de39d5813a3fc542eaac733fc221dbd963d1d707ab6f812eda8f9ee8a2268327309ad4606146307c49d303c5b5ce06a176cd
7
- data.tar.gz: f03e05bfec70bc67692286dcf4bfe0b681a23fa3090a4d0c3eb4e068c94a5093097777b22343eb2d57ab1cb5f913a1db5e452bdeeca4925df690791d42b87ac8
6
+ metadata.gz: e21ecd0979f492d4dec669d1ece80931035b451687b0e6cef458c9404b15d737a1287e9617e319771a8f5d2c4f9ca9c217492aa589b69012f5b2b8329f8fa398
7
+ data.tar.gz: 1799f53d73c0ede957138f1c7fb4c6386a8e13b13548dc8e6600dd2bcd6a6db5c577ed38ed1a8793cffd1c2f804e5b56d66d2c0905765fe08b530367fd1b40a7
data/README.md CHANGED
@@ -52,7 +52,7 @@ type.
52
52
  ```ruby
53
53
  orders = Order.where(state: 'placed')
54
54
 
55
- packager.package :orders do
55
+ packager.package orders do
56
56
  # ...
57
57
  end
58
58
  ```
@@ -63,7 +63,7 @@ with the `#pdf` method:
63
63
 
64
64
  ```ruby
65
65
  packager.package orders do
66
- pdf 'path/to/template.mustache'
66
+ pdf 'path/to/template.erb'
67
67
  end
68
68
  ```
69
69
 
@@ -72,7 +72,7 @@ groups.
72
72
 
73
73
  ```ruby
74
74
  packager.package orders do
75
- pdf 'path/to/template.mustache'
75
+ pdf 'path/to/template.erb'
76
76
 
77
77
  group 'Canadian Standard Post' do
78
78
  filter {
@@ -100,32 +100,25 @@ and 'InternationalWorldShip.pdf'.
100
100
 
101
101
  ### Templating
102
102
 
103
- Right now, templating is done with Mustache. I plan on adding more in the
104
- future (or feel free to send me a pull request). Here is an example template:
103
+ Shipping Materials uses [Tilt](http://www.github.com/rtomayko/tilt) therefore
104
+ there are a number of options available. Templates are evaluated within the
105
+ context of the order array. A sample ERB template would look like this:
105
106
 
106
- ```html
107
+ ```erb
107
108
  <html>
108
- {{# objects }}
109
- <div>
110
- <p>{{ number }}</p>
111
- {{# line_items }}
112
- <p>{{ name }}: ${{ price }} x {{ quantity }} = {{ total }}</p>
113
- {{/ line_items }}
114
- </div>
115
- {{/ objects }}
109
+ <body>
110
+ <% self.each do |order| %>
111
+ <p><%= order.number %>
112
+ <div>
113
+ <% order.line_items.each do |li| %>
114
+ <p><%= line_item.desc %>: $<%= li.price %> x <%= li.qty %> = <%= li.total %></p>
115
+ <% end %>
116
+ </div>
117
+ <% end %>
118
+ </body>
116
119
  </html>
117
120
  ```
118
121
 
119
-
120
- I chose the 'objects' as the default variable name, though you may change this
121
- in config:
122
-
123
- ```ruby
124
- ShippingMaterials.config do |config|
125
- config.base_context = 'orders'
126
- end
127
- ```
128
-
129
122
  Each group will produce one PDF.
130
123
 
131
124
  ### CSV (for shipping labels)
@@ -173,12 +166,12 @@ Materials provides a sorting DSL for more complex sorts. For example:
173
166
 
174
167
  ```ruby
175
168
  packager.package orders do
176
- pdf 'path/to/template.mustache'
169
+ pdf 'path/to/template.erb'
177
170
 
178
171
  sort do
179
172
  # put orders containing only Vinyl at the top
180
173
  rule {
181
- return true unless line_items.detect {|li| type != 'Vinyl' }
174
+ line_items.select {|li| type != 'Vinyl' }.any?
182
175
  }
183
176
 
184
177
  # next come orders that have both Vinyl and CDs and nothing else
@@ -73,6 +73,8 @@ module ShippingMaterials
73
73
  object.send(meth)
74
74
  elsif meth.is_a? Array
75
75
  meth.reduce(object) {|obj, m| obj.send(m) }
76
+ elsif meth.is_a? Proc
77
+ object.instance_eval(&meth)
76
78
  elsif meth.is_a? String
77
79
  meth
78
80
  end
@@ -6,6 +6,7 @@ module ShippingMaterials
6
6
 
7
7
  def initialize
8
8
  @groups = []
9
+ @sorters = {}
9
10
  end
10
11
 
11
12
  def package(objects, &block)
@@ -14,7 +15,7 @@ module ShippingMaterials
14
15
  @groups.each do |group|
15
16
  sort_group(group)
16
17
  create_packing_slips(group)
17
- csvs(group)
18
+ create_csvs(group)
18
19
  end
19
20
  Storage.gzip if Config.use_gzip?
20
21
  end
@@ -32,8 +33,8 @@ module ShippingMaterials
32
33
  private
33
34
 
34
35
  def sort_group(group)
35
- return unless self.sorters.nil?
36
- group.sorters ||= self.sorters unless group.sorters.nil?
36
+ return if self.sorters.empty?
37
+ group.sorters ||= self.sorters
37
38
  group.sort!
38
39
  end
39
40
 
@@ -42,10 +43,10 @@ module ShippingMaterials
42
43
  Storage.write_pdf(group.basename, packing_slip.to_s)
43
44
  end
44
45
 
45
- def csvs(group)
46
+ def create_csvs(group)
46
47
  group.csvs.each do |csv|
47
- extension = group.basename + '.' + csv.extension
48
- Storage.write_file(extension, csv.to_s)
48
+ filename = Storage.filenameize(group.basename) + '.' + csv.extension
49
+ Storage.write_file(filename, csv.to_s)
49
50
  end
50
51
  end
51
52
  end
@@ -1,15 +1,20 @@
1
1
  module ShippingMaterials
2
2
  class PackingSlips
3
3
  def initialize(objects, template_file)
4
- @objects = objects
4
+ @objects = objects
5
5
  @template_file = template_file
6
6
  end
7
7
 
8
8
  def to_s
9
- t = Mustache.new
10
- t.template_file = @template_file
11
- t[Config.base_context] = @objects
12
- t.render
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
13
18
  end
14
19
 
15
20
  alias_method :to_html, :to_s
@@ -35,14 +35,20 @@ module ShippingMaterials
35
35
  if Config.use_s3?
36
36
  @s3 ||= S3.new
37
37
  File.open(filename) do |fp|
38
+ puts "Saving gzip file to S3"
38
39
  @s3.write(Config.gzip_file_name, fp)
40
+ puts "Done"
39
41
  end
40
42
  FileUtils.rm_rf(self.save_path) unless self.save_path == '/'
43
+ puts "Removed working dir: #{Config.save_path}/"
41
44
  end
42
45
  end
43
46
 
44
47
  def save_path
45
- FileUtils.mkdir(Config.save_path) unless Dir.exists?(Config.save_path)
48
+ unless Dir.exists?(Config.save_path)
49
+ FileUtils.mkdir(Config.save_path)
50
+ puts "Created working dir: #{Config.save_path}/"
51
+ end
46
52
  Config.save_path
47
53
  end
48
54
 
@@ -1,3 +1,3 @@
1
1
  module ShippingMaterials
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'tilt'
1
2
  require 'mustache'
2
3
 
3
4
  require 'shipping_materials/version'
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency 'mustache'
23
23
  spec.add_dependency 'aws-sdk'
24
+ spec.add_dependency 'tilt'
24
25
 
25
26
  spec.add_development_dependency 'bundler', '~> 1.3'
26
27
  spec.add_development_dependency 'rake'
@@ -97,10 +97,17 @@ module ShippingMaterials
97
97
  "CSV method chaining is borked"
98
98
  end
99
99
 
100
+ def test_proc
101
+ @csv = CSVDSL.new(orders.select {|o| o.name == 'Andrew' })
102
+ @csv.row [ proc { "#{id}+#{name}" } ]
103
+
104
+ assert_equal "1+Andrew\n", @csv.to_csv, "CSV with proc doesn't work"
105
+ end
106
+
100
107
  def test_to_csv
101
108
  @csv = CSVDSL.new(orders.select {|o| o.id == 1 }, headers: true)
102
- @csv.row :order_id => :id,
103
- :name => :name,
109
+ @csv.row :order_id => :id,
110
+ :name => :name,
104
111
  :static_fields => 'A string'
105
112
 
106
113
  @csv.row :line_items => [:id, :name, :quantity]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shipping_materials
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-07-28 00:00:00.000000000 Z
11
+ date: 2013-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mustache
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tilt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement