garden 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in garden.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,5 @@
1
+ A utility for using different seed formats in your ActiveRecord database.
2
+
3
+ In process. Currently supports Excel spreadsheet format.
4
+
5
+ Garden.excel 'spreadsheetname'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/garden.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "garden/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "garden"
7
+ s.version = Garden::VERSION
8
+ s.authors = ["amoslanka"]
9
+ s.email = ["amoslanka@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A utility for using different seed formats in your ActiveRecord database. }
12
+ s.description = %q{Allows you to organize your seeds in different formats. Typical seeds.rb, yaml fixtures, and a variety of spreadsheet formats. }
13
+
14
+ s.rubyforge_project = "garden"
15
+
16
+ s.add_dependency 'spreadsheet'
17
+ # s.add_dependency 'active_record'
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
data/lib/garden.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "garden/version"
2
+
3
+ module Garden
4
+
5
+ autoload :Excel, 'garden/spreadsheets'
6
+ autoload :Mediators, 'garden/mediators'
7
+
8
+ # def self.plant
9
+ # # todo
10
+ # end
11
+ #
12
+ # def self.all
13
+ # # todo
14
+ # end
15
+ #
16
+ #
17
+ # def self.spreadsheets
18
+ # # todo
19
+ # end
20
+
21
+ def self.excel file_name_or_path
22
+ filepath = resolve_file_path(file_name_or_path)
23
+ Excel.new filepath
24
+ end
25
+
26
+ def self.resolve_file_path name_or_path
27
+ File.exists?(name_or_path) ? name_or_path : File.join(Rails.root, "db/seeds/#{name_or_path}.xls")
28
+ end
29
+
30
+ end
@@ -0,0 +1,107 @@
1
+ module Garden
2
+ module Mediators
3
+ class Table
4
+
5
+ def self.parse_table_name namish
6
+ namish.parameterize.underscore
7
+ end
8
+ def self.get_instance table_name, id
9
+
10
+ # puts "Find #{id} from #{table_name}"
11
+ # puts id.class.to_s
12
+
13
+ begin
14
+ clazz = table_name.to_s.classify.constantize
15
+ raise "Class #{clazz.to_s} is not an ActiveRecord subclass." unless clazz.new.is_a?(ActiveRecord::Base)
16
+ instance = clazz.find id.to_s
17
+ return instance
18
+ rescue Exception => e
19
+ puts "Could not find #{id} from table #{table_name}: #{e.message}"
20
+ return nil
21
+ end
22
+ end
23
+
24
+
25
+
26
+ def initialize namish
27
+
28
+ @name = Table.parse_table_name namish
29
+
30
+ # Get the ActiveRecord model from the tablename.
31
+ begin
32
+ @clazz = @name.classify.constantize
33
+ raise "Class #{@clazz.to_s} is not an ActiveRecord subclass." unless @clazz.new.is_a?(ActiveRecord::Base)
34
+ rescue Exception => e
35
+ puts " ** Could not derive ActiveRecord model from the provided name: #{namish}. Exception: #{e.message}"
36
+ end
37
+
38
+ @instance = @clazz.new
39
+
40
+ end
41
+
42
+ def valid?
43
+ @clazz != nil
44
+ end
45
+
46
+ def parse_headers array
47
+ @headers = array.map { |header| header.to_s.parameterize.underscore }
48
+ @relationships = []
49
+
50
+ @headers.each do |header|
51
+ @relationships.push header if @clazz.reflections.keys.include?(header.to_sym)
52
+ end
53
+ end
54
+
55
+
56
+ def relationships
57
+ @relationships
58
+ end
59
+
60
+ def create_instance attributes
61
+ relationships = @relationships
62
+
63
+
64
+ attributes = parse_row_relationships attributes, relationships
65
+
66
+ instance = @clazz.new
67
+
68
+ rejected = {}
69
+ attributes.each_key do |key|
70
+ rejected[key] = attributes.delete(key) if !instance.attributes.include?(key.to_s) && !relationships.include?(key.to_s)
71
+ end
72
+
73
+ instance.attributes = attributes
74
+
75
+ # instance.id = rejected[:id] if rejected.key?(:id)
76
+
77
+ # puts "Valid? #{instance.valid?}"
78
+ # puts instance.errors.to_s
79
+
80
+ valid = instance.valid?
81
+
82
+ if instance.valid?
83
+ instance.save!
84
+ puts ".Saved instance: #{@clazz} #{instance.to_param}"
85
+ else
86
+ puts "Invalid instance."
87
+ puts "#{@name}:"
88
+ puts " - Valid attributes: #{attributes.keys}"
89
+ puts " - Invalid attributes: #{rejected.keys}"
90
+ puts " - Association attributes: #{relationships}"
91
+ puts " - #{instance.errors}"
92
+ end
93
+ end
94
+
95
+ def parse_row_relationships hash, relationships
96
+ relationships.each do |r|
97
+ # puts "Parsing row relationship: #{hash[r.to_sym]}"
98
+ instance = get_instance r, hash[r.to_sym]
99
+ hash[r.to_sym] = instance
100
+ end
101
+ hash
102
+ end
103
+
104
+
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,50 @@
1
+ module Garden
2
+ class Excel
3
+
4
+ def initialize filepath
5
+ open filepath
6
+ end
7
+
8
+ def open filepath
9
+ puts "Planting spreadsheet: #{filepath}"
10
+
11
+ @ss = Spreadsheet.open filepath
12
+ @ss.worksheets.each do |table|
13
+ puts "Parsing table #{table.name}"
14
+ parse_table table
15
+ end
16
+
17
+ end
18
+
19
+ def parse_table table
20
+ # The table object passed in is a Spreadsheet::Worksheet instance.
21
+
22
+ table_mediator = Mediators::Table.new table.name
23
+ if !table_mediator.valid?
24
+ return
25
+ end
26
+
27
+ # Get the headers. These values will be the attribute names
28
+ headers = table_mediator.parse_headers table.first.to_a
29
+
30
+ # Now loop the table rows, inserting records.
31
+ table.each do |row|
32
+ next if row.idx == 0
33
+ # puts '...............'
34
+
35
+ table_mediator.create_instance parse_worksheet_row(headers, row)
36
+ end
37
+ end
38
+
39
+ def parse_worksheet_row keys, values
40
+ h = {}
41
+ keys.each_index do |index|
42
+ key = keys[index].to_sym
43
+ h[key] = values[index]
44
+ end
45
+ h
46
+ end
47
+
48
+
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Garden
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garden
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - amoslanka
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spreadsheet
16
+ requirement: &70240349441300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70240349441300
25
+ description: ! 'Allows you to organize your seeds in different formats. Typical seeds.rb,
26
+ yaml fixtures, and a variety of spreadsheet formats. '
27
+ email:
28
+ - amoslanka@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.markdown
36
+ - Rakefile
37
+ - garden.gemspec
38
+ - lib/garden.rb
39
+ - lib/garden/mediators.rb
40
+ - lib/garden/spreadsheets.rb
41
+ - lib/garden/version.rb
42
+ homepage: ''
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project: garden
62
+ rubygems_version: 1.8.6
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: A utility for using different seed formats in your ActiveRecord database.
66
+ test_files: []