csv_pirate 2.0.1 → 2.1.1
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.
- data/CHANGELOG +3 -0
- data/README.rdoc +57 -0
- data/Rakefile +0 -1
- data/VERSION.yml +2 -2
- data/csv_pirate.gemspec +1 -2
- data/lib/csv_pirate.rb +26 -1
- metadata +1 -2
- data/about.yml +0 -7
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -183,6 +183,63 @@ OR
|
|
183
183
|
|
184
184
|
csv_pirate = CsvPirate.new({:swag => users,:waggoner => 'inactive_users_not_logged_in',:booty => ["id","number","login","created_at"],:chart => 'log/csv/'})
|
185
185
|
|
186
|
+
== Advanced Example with Nested Methods
|
187
|
+
|
188
|
+
You have a VehicleModel class and the same Make class as up above:
|
189
|
+
|
190
|
+
# == Schema Information
|
191
|
+
#
|
192
|
+
# Table name: vehicle_models
|
193
|
+
#
|
194
|
+
# id :integer(4) not null, primary key
|
195
|
+
# name :string(255)
|
196
|
+
# year :integer(4)
|
197
|
+
# horsepower :integer(4)
|
198
|
+
# price :integer(4)
|
199
|
+
# electric :boolean(1)
|
200
|
+
# make_id :integer(4)
|
201
|
+
#
|
202
|
+
|
203
|
+
class VehicleModel < ActiveRecord::Base
|
204
|
+
belongs_to :make
|
205
|
+
has_csv_pirate_ship :booty => [:id, :name, :year,
|
206
|
+
{:make => :name},
|
207
|
+
{:tires => {:size => {:width => :inches}}}]
|
208
|
+
def tires; TireSize.new; end
|
209
|
+
end
|
210
|
+
|
211
|
+
class TireSize
|
212
|
+
# To call an instance method you need to return an instance
|
213
|
+
def size; TireWidth.new; end
|
214
|
+
end
|
215
|
+
|
216
|
+
class TireWidth
|
217
|
+
# To call a class method you need to return the class object
|
218
|
+
def width; Measurement; end
|
219
|
+
end
|
220
|
+
|
221
|
+
class Measurement
|
222
|
+
def self.inches; 13; end
|
223
|
+
end
|
224
|
+
|
225
|
+
Then to create the CSV:
|
226
|
+
|
227
|
+
a = VehicleModel.walk_the_plank
|
228
|
+
|
229
|
+
Then check the output from the console:
|
230
|
+
|
231
|
+
a.weigh_anchor
|
232
|
+
|
233
|
+
id,name,year,makename,tiressizewidthinches
|
234
|
+
1,Cavalier,1999,Chevrolet,13
|
235
|
+
2,Trailblazer,2006,Chevrolet,13
|
236
|
+
3,Corvette,2010,Chevrolet,13
|
237
|
+
4,Mustang,1976,Ford,13
|
238
|
+
5,Lebaron,1987,Chrysler,13
|
239
|
+
6,Avalon,1996,Toyota,13
|
240
|
+
=> #<File:/Users/pboling/RubymineProjects/empty_csv_pirate_app/log/VehicleModel.20091001.export.2.csv (closed)>
|
241
|
+
|
242
|
+
Joy to recursive code everywhere!
|
186
243
|
|
187
244
|
----------------------------------------------------------------------------------
|
188
245
|
Author: Peter Boling, peter.boling at gmail dot com
|
data/Rakefile
CHANGED
data/VERSION.yml
CHANGED
data/csv_pirate.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{csv_pirate}
|
8
|
-
s.version = "2.
|
8
|
+
s.version = "2.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Peter Boling"]
|
@@ -23,7 +23,6 @@ It works better if you are wearing a tricorne!}
|
|
23
23
|
"README.rdoc",
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION.yml",
|
26
|
-
"about.yml",
|
27
26
|
"csv_pirate.gemspec",
|
28
27
|
"init.rb",
|
29
28
|
"install.rb",
|
data/lib/csv_pirate.rb
CHANGED
@@ -158,7 +158,12 @@ class CsvPirate
|
|
158
158
|
def prize(spoils)
|
159
159
|
gold_doubloons = []
|
160
160
|
self.booty.each do |plunder|
|
161
|
-
|
161
|
+
# Check for nestedness
|
162
|
+
if plunder.is_a?(Hash)
|
163
|
+
gold_doubloons << CsvPirate.marlinespike(spoils, plunder)
|
164
|
+
else
|
165
|
+
gold_doubloons << spoils.send(plunder.to_sym)
|
166
|
+
end
|
162
167
|
end
|
163
168
|
gold_doubloons
|
164
169
|
end
|
@@ -286,6 +291,26 @@ class CsvPirate
|
|
286
291
|
############ CLASS METHODS #############
|
287
292
|
########################################
|
288
293
|
|
294
|
+
# if this is your booty:
|
295
|
+
# {:booty => [
|
296
|
+
# :id,
|
297
|
+
# {:region => {:country => :name }, :state => :name },
|
298
|
+
# :name
|
299
|
+
# ]}
|
300
|
+
# so nested_hash = {:region => {:country => :name }, :state => :name }
|
301
|
+
def self.marlinespike(spoils, navigation)
|
302
|
+
navigation.map do |east,west|
|
303
|
+
spoils = spoils.send(east.to_sym)
|
304
|
+
if west.is_a?(Hash)
|
305
|
+
# Recursive nadness is here!
|
306
|
+
spoils = CsvPirate.marlinespike(spoils, west)
|
307
|
+
else
|
308
|
+
spoils = spoils.send(west.to_sym)
|
309
|
+
end
|
310
|
+
spoils
|
311
|
+
end.join(' - ')
|
312
|
+
end
|
313
|
+
|
289
314
|
# Used to read any loot found by any pirate
|
290
315
|
def self.rinse(quarterdeck)
|
291
316
|
File.open(File.expand_path(quarterdeck), "r") do |bucket_line|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_pirate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Boling
|
@@ -30,7 +30,6 @@ files:
|
|
30
30
|
- README.rdoc
|
31
31
|
- Rakefile
|
32
32
|
- VERSION.yml
|
33
|
-
- about.yml
|
34
33
|
- csv_pirate.gemspec
|
35
34
|
- init.rb
|
36
35
|
- install.rb
|