garden 0.0.13.pre → 0.0.14.pre

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/garden.gemspec CHANGED
@@ -14,7 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.rubyforge_project = "garden"
15
15
 
16
16
  s.add_dependency 'spreadsheet'
17
- # s.add_dependency 'active_record'
18
17
 
19
18
  s.files = `git ls-files`.split("\n")
20
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/lib/garden.rb CHANGED
@@ -22,12 +22,14 @@ module Garden
22
22
  # end
23
23
 
24
24
  def self.excel file_name_or_path, options=nil
25
- filepath = resolve_file_path(file_name_or_path)
25
+ filepath = File.exists?(file_name_or_path) ? file_name_or_path : File.join(Rails.root, "db/seeds/#{file_name_or_path}.xls")
26
26
  Spreadsheets::Excel.new filepath, options
27
27
  end
28
28
 
29
- def self.resolve_file_path name_or_path
30
- File.exists?(name_or_path) ? name_or_path : File.join(Rails.root, "db/seeds/#{name_or_path}.xls")
29
+ def self.csv file_name_or_path, options=nil
30
+ filepath = File.exists?(file_name_or_path) ? file_name_or_path : File.join(Rails.root, "db/seeds/#{file_name_or_path}.csv")
31
+ Spreadsheets::CSV.new filepath, options
31
32
  end
32
33
 
34
+
33
35
  end
@@ -47,7 +47,7 @@ module Garden
47
47
  # puts " ** Could not derive ActiveRecord model from the provided name: #{namish}. Exception: #{e.message}"
48
48
  # end
49
49
 
50
- @instance = @clazz.new
50
+ # @instance = @clazz.new
51
51
 
52
52
  end
53
53
 
@@ -55,10 +55,10 @@ module Garden
55
55
  @clazz != nil
56
56
  end
57
57
 
58
- def parse_headers array
59
- # @headers = array.map { |header| header.to_s.parameterize.underscore }
60
- @headers = array.map { |header| header.to_s.strip.gsub(/ /, '-').underscore }
61
- end
58
+ # def parse_headers array
59
+ # # @headers = array.map { |header| header.to_s.parameterize.underscore }
60
+ # @headers = array.map { |header| header.to_s.strip.gsub(/ /, '-').underscore }
61
+ # end
62
62
 
63
63
  def reference_by col_name
64
64
  @instance_options[:reference] = col_name
@@ -68,11 +68,10 @@ module Garden
68
68
  # @relationships
69
69
  # end
70
70
  #
71
- def create_instance attributes
71
+ def row attributes
72
72
  Instance.new @clazz, attributes, @instance_options.dup
73
73
  end
74
74
 
75
-
76
75
  def class_exists?(name)
77
76
  begin
78
77
  true if Kernel.const_get(name)
@@ -1,58 +1,60 @@
1
1
  require 'spreadsheet'
2
+ require 'csv'
2
3
 
3
4
  module Garden
4
5
  module Spreadsheets
5
- class Excel
6
6
 
7
+ class AbstractSpreadsheet
7
8
  def initialize filepath, options={}
8
9
  options ||= {}
9
-
10
10
  @options = options.reverse_merge! :some_option => "nevermind"
11
-
12
11
  open filepath
13
12
  end
14
13
 
14
+ def open(filepath)
15
+ end
16
+
17
+ def build_mediator name
18
+ @mediator = Mediators::Table.new name
19
+ raise "Invalid mediator for table #{name}" unless @mediator.valid?
20
+ @mediator.reference_by(@options[:reference]) if @options.has_key?(:reference)
21
+ @mediator
22
+ end
23
+
24
+ end
25
+
26
+ class Excel < AbstractSpreadsheet
27
+
15
28
  def open filepath
16
- message = "Planting spreadsheet: #{filepath}"
29
+ message = "Planting excel spreadsheet: #{filepath}"
17
30
  message += " (only these worksheets: #{@options[:only].to_s})" if @options[:only]
18
31
  message += " (worksheet #{@options[:worksheet].to_s})" if @options[:worksheet]
19
32
  puts message
33
+
34
+ excel_spreadsheet = ::Spreadsheet.open filepath
20
35
 
21
- @ss = Spreadsheet.open filepath
22
- worksheet_names = @options[:only] || @options[:worksheet] || @ss.worksheets.collect { |table| table.name }
36
+ worksheet_names = @options[:only] || @options[:worksheet] || excel_spreadsheet.worksheets.collect { |table| table.name }
23
37
  worksheet_names = [worksheet_names] unless worksheet_names.is_a?(Enumerable)
24
38
 
25
39
  # Import the worksheets
26
40
  worksheet_names.each do |name|
27
41
  puts "Parsing table #{name}"
28
- parse_table @ss.worksheets.find { |table| table.name == name.to_s }
42
+ table = excel_spreadsheet.worksheets.find { |table| table.name == name.to_s }
43
+ parse_worksheet table.name, table.to_a
29
44
  end
30
45
 
31
46
  end
32
-
33
- def parse_table table
34
- # The table object passed in is a Spreadsheet::Worksheet instance.
35
-
36
- table_mediator = Mediators::Table.new table.name
37
- if !table_mediator.valid?
38
- return
39
- end
40
47
 
41
- # Get the headers. These values will be the attribute names
42
- headers = table_mediator.parse_headers table.first.to_a
43
-
44
- # Set reference column if defined.
45
- if @options.has_key?(:reference)
46
- table_mediator.reference_by(@options[:reference])
47
- end
48
-
49
- # Now loop the table rows, inserting records.
50
- table.each do |row|
51
- next if row.idx == 0
52
- table_mediator.create_instance parse_worksheet_row(headers, row)
48
+ def parse_worksheet name, rows
49
+ build_mediator name
50
+ # Expects the first row to be the headers.
51
+ headers = rows.shift.map { |header| header.to_s.strip.gsub(/ /, '-').underscore }
52
+ rows.each do |row|
53
+ attributes = parse_worksheet_row(headers, row)
54
+ @mediator.row attributes
53
55
  end
54
56
  end
55
-
57
+
56
58
  def parse_worksheet_row keys, values
57
59
  h = {}
58
60
  keys.each_index do |index|
@@ -61,6 +63,41 @@ module Garden
61
63
  end
62
64
  h
63
65
  end
66
+
64
67
  end
68
+
69
+ class CSV < AbstractSpreadsheet
70
+ def open filepath
71
+ file = File.open filepath
72
+ begin
73
+ rows = ::CSV.parse(file, :headers => true)
74
+ rescue Exception => e
75
+ rows = []
76
+ end
77
+
78
+ # Build the mediator. Assume the table name is the same as the file's name passed in or the :table option
79
+ table_name = @options.delete(:table) || File.basename(filepath, File.extname(filepath))
80
+ message = "Planting csv spreadsheet: #{filepath}"
81
+ message << " (into table #{table_name})"
82
+ puts message
83
+ build_mediator table_name
84
+
85
+ rows.each do |row|
86
+ attributes = row_to_attributes(row)
87
+ @mediator.row attributes
88
+ end
89
+ end
90
+
91
+ def row_to_attributes row
92
+ h = {}
93
+ row.each do |ary|
94
+ key = ary.first.to_sym
95
+ h[key] = ary.last
96
+ end
97
+ h
98
+ end
99
+
100
+ end
101
+
65
102
  end
66
103
  end
@@ -1,3 +1,3 @@
1
1
  module Garden
2
- VERSION = "0.0.13.pre"
2
+ VERSION = "0.0.14.pre"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13.pre
4
+ version: 0.0.14.pre
5
5
  prerelease: 7
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-20 00:00:00.000000000Z
12
+ date: 2011-11-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spreadsheet
16
- requirement: &70304106801080 !ruby/object:Gem::Requirement
16
+ requirement: &70302264679260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70304106801080
24
+ version_requirements: *70302264679260
25
25
  description: ! 'Allows you to organize your seeds in different formats. Typical seeds.rb,
26
26
  yaml fixtures, and a variety of spreadsheet formats. '
27
27
  email: