csv_pirate 4.0.1 → 4.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.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ Version 4.0.2 2010-01-18
2
+ - Improving to_memory method on CsvPirate
3
+ - Improving raise_anchor method on PirateShip
4
+
1
5
  Version 4.0.1 2010-01-15
2
6
  - Improving import / export
3
7
  - Updating Readme to reflect 4.0.0 API
data/README.rdoc CHANGED
@@ -45,7 +45,14 @@ Avast! Here be pirates! To brush up on pirate coding naming conventions:
45
45
 
46
46
  http://www.privateerdragons.com/pirate_dictionary.html
47
47
 
48
- Perhaps the next version will have CSV importing abilities...
48
+ == New for version 4.x... Importing to DB or Ruby objects in memory from CSV
49
+
50
+ Importing abilities are now here! You can dump data to CSV, copy the CSV to wherever, and then import the data in the CSV. Works very well with ActiveRecord.
51
+
52
+ See the weigh_anchor method, added to models with has_csv_pirate_ship defined, for dumping.
53
+ See the raise_anchor method, added to models with has_csv_pirate_ship defined, for importing.
54
+ See the to_memory method to convert the data in a csv or CsvPirate instance object back into Ruby class instances
55
+ with as many attributes as possible set equal to the data from the CSV.
49
56
 
50
57
  == On The Web
51
58
 
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 4
4
4
  :minor: 0
5
5
  :build:
data/csv_pirate.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{csv_pirate}
8
- s.version = "4.0.1"
8
+ s.version = "4.0.2"
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"]
12
- s.date = %q{2010-01-15}
12
+ s.date = %q{2010-01-18}
13
13
  s.description = %q{CsvPirate is the easy way to create a CSV of essentially anything in Ruby, in full pirate regalia.
14
14
  It works better if you are wearing a tricorne!}
15
15
  s.email = %q{peter.boling@gmail.com}
data/lib/csv_pirate.rb CHANGED
@@ -306,7 +306,21 @@ class CsvPirate
306
306
  end
307
307
  end
308
308
 
309
- def to_memory(exclude_id = true, exclude_timestamps = true)
309
+ #permanence can be any of:
310
+ # {:new => :new} - only calls the initializer with data hash for each row to instantiate objects (useful with any vanilla Ruby Class)
311
+ # {:new => :save} - calls the initializer with the data hash for each row and then calls save on each (useful with ActiveRecord)
312
+ # {:new => :create} - calls a create method with the data hash for each row (useful with ActiveRecord)
313
+ # {:find_or_new => [column names for find_by]} - see below (returns only the new objects
314
+ # {:find_or_save => [column names for find_by]} - see below (returns all found or saved objects)
315
+ # {:find_or_create => [column names for find_by]} - looks for existing objects using find_by_#{columns.join('_and_')}, (returns all found or created objects)
316
+ # and if not found creates a new object.
317
+ # The difference between the new, save and create versions are the same as the various :new hashes above.
318
+ # {:update_or_new => [column names for find_by]} - see below (returns only the new objects)
319
+ # {:update_or_save => [column names for find_by]} - see below (returns all updated or saved objects)
320
+ # {:update_or_create => [column names for find_by]} - looks for existing objects using find_by_#{columns.join('_and_')} , (returns all updated or created objects)
321
+ # and updates them with the data hash form the csv row, otherwise creates a new object.
322
+ #TODO: This is a nasty method. Just a quick hack to GTD. Needs to be rethought and refactored. --pboling
323
+ def to_memory(permanence = {:new => :new}, exclude_id = true, exclude_timestamps = true)
310
324
  return nil unless self.grub
311
325
  begin
312
326
  example = self.grub.new
@@ -316,8 +330,65 @@ class CsvPirate
316
330
  end
317
331
  buccaneers = []
318
332
  self.scuttle do |row|
319
- #puts "#{self.pinnacle.first.inspect} #{row[self.pinnacle.first].inspect}"
320
- buccaneers << self.grub.new(self.data_hash_from_row(row, exclude_id, exclude_timestamps, example))
333
+ data_hash = self.data_hash_from_row(row, exclude_id, exclude_timestamps, example)
334
+ case permanence
335
+ when {:new => :new} then
336
+ buccaneers << self.grub.new(data_hash)
337
+ when {:new => :save} then
338
+ obj = self.grub.new(data_hash)
339
+ buccaneers << obj.save(false)
340
+ when {:new => :create} then
341
+ buccaneers << self.grub.create(data_hash)
342
+ else
343
+ if permanence[:find_or_new]
344
+ obj = self.grub.send("find_by_#{permanence[:find_or_new].join('_and_')}".to_sym)
345
+ buccaneers << self.grub.new(data_hash) if obj.nil?
346
+ elsif permanence[:find_or_save]
347
+ obj = self.grub.send("find_by_#{permanence[:find_or_save].join('_and_')}".to_sym)
348
+ if obj.nil?
349
+ obj = self.grub.new(data_hash)
350
+ obj.save(false) if obj.respond_to?(:save)
351
+ end
352
+ buccaneers << obj
353
+ elsif permanence[:find_or_create]
354
+ obj = self.grub.send("find_by_#{permanence[:find_or_create].join('_and_')}".to_sym)
355
+ buccaneers << obj || self.grub.create(data_hash)
356
+ elsif permanence[:update_or_new]
357
+ obj = self.grub.send("find_by_#{permanence[:find_or_new].join('_and_')}".to_sym)
358
+ if obj.nil?
359
+ obj = self.grub.new(data_hash)
360
+ else
361
+ data_hash.each do |k,v|
362
+ obj.send("#{k}=".to_sym, v)
363
+ obj.save(false)
364
+ end
365
+ end
366
+ buccaneers << obj
367
+ elsif permanence[:update_or_save]
368
+ obj = self.grub.send("find_by_#{permanence[:find_or_new].join('_and_')}".to_sym)
369
+ if obj.nil?
370
+ obj = self.grub.new(data_hash)
371
+ obj.save(false)
372
+ else
373
+ data_hash.each do |k,v|
374
+ obj.send("#{k}=".to_sym, v)
375
+ obj.save(false)
376
+ end
377
+ end
378
+ buccaneers << obj
379
+ elsif permanence[:update_or_create]
380
+ obj = self.grub.send("find_by_#{permanence[:find_or_new].join('_and_')}".to_sym)
381
+ if obj.nil?
382
+ obj = self.grub.create(data_hash)
383
+ else
384
+ data_hash.each do |k,v|
385
+ obj.send("#{k}=".to_sym, v)
386
+ obj.save(false)
387
+ end
388
+ end
389
+ buccaneers << obj
390
+ end
391
+ end
321
392
  end
322
393
  buccaneers
323
394
  end
@@ -329,10 +400,8 @@ class CsvPirate
329
400
  my_booty = exclude_timestamps ? my_booty.reject {|x| a = x.to_sym; [:created_at, :updated_at, :created_on, :updated_on].include?(a)} : self.booty
330
401
  my_booty = my_booty.reject {|x| !example.respond_to?("#{x}=".to_sym)} unless example.nil?
331
402
  my_booty.each do |method|
332
- #puts "#{self.pinnacle[index]}"
333
403
  data_hash = data_hash.merge({method => row[self.pinnacle[self.booty.index(method)]]})
334
404
  end
335
- #puts "#{data_hash.inspect}"
336
405
  data_hash
337
406
  end
338
407
 
@@ -113,7 +113,7 @@ module NinthBit
113
113
  CsvPirate.create(pargs)
114
114
  end
115
115
 
116
- def raise_anchor(args = {})
116
+ def raise_anchor(permanence = {:new => :new}, args = {})
117
117
  pargs = self.piratey_args(args)
118
118
  pargs.merge!({
119
119
  :chart => pargs[:chart] + ["dumps"],
@@ -121,10 +121,7 @@ module NinthBit
121
121
  })
122
122
  csv_pirate = CsvPirate.new(pargs)
123
123
 
124
- csv_pirate.to_memory.map do |obj|
125
- obj.save(false) if obj.respond_to?(:save)
126
- obj
127
- end
124
+ csv_pirate.to_memory(permanence)
128
125
  end
129
126
 
130
127
  protected
@@ -54,16 +54,17 @@ describe "PirateShip" do
54
54
 
55
55
  describe "#raise_anchor" do
56
56
  before(:each) do
57
- @csv_pirate = Star.weigh_anchor({:chronometer => Date.parse("3/29/2002")})
58
- @csv_pirate = Star.weigh_anchor({:chronometer => Date.parse("6/14/2004")})
59
- @csv_pirate = Star.weigh_anchor({:chronometer => Date.parse("12/25/1962")})
60
- @csv_pirate = Star.raise_anchor()
57
+ Star.weigh_anchor({:chronometer => Date.parse("3/29/2002")})
58
+ Star.weigh_anchor({:chronometer => Date.parse("6/14/2004")})
59
+ Star.weigh_anchor({:chronometer => Date.parse("12/25/1962")})
61
60
  end
62
61
 
63
- it "should return an array of 10 grubs built from data in CSV" do
62
+ it "should return an array of 10 Stars built from data in CSV" do
63
+ @csv_pirate = Star.raise_anchor({:new => :new})
64
64
  @csv_pirate.class.should == Array
65
65
  @csv_pirate.length.should == 10
66
66
  end
67
+
67
68
  end
68
69
 
69
70
  end
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: 4.0.1
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-15 00:00:00 -05:00
12
+ date: 2010-01-18 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency