crhym3-imexport 0.1.0

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 ADDED
File without changes
data/Manifest ADDED
@@ -0,0 +1,6 @@
1
+ Rakefile
2
+ imexport.gemspec
3
+ Manifest
4
+ CHANGELOG
5
+ lib/imexport.rb
6
+ README.rdoc
data/README.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ == Description
2
+
3
+ TODO
4
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('imexport', '0.1.0') do |p|
6
+ p.description = "Simple import from a text file generated by mysql -E ..."
7
+ p.url = "http://github.com/crhym3/imexport"
8
+ p.author = "alex"
9
+ p.email = "alex@digns.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
15
+
data/imexport.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{imexport}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["alex"]
9
+ s.date = %q{2009-04-21}
10
+ s.description = %q{Simple import from a text file generated by mysql -E ...}
11
+ s.email = %q{alex@digns.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/imexport.rb", "README.rdoc"]
13
+ s.files = ["Rakefile", "imexport.gemspec", "Manifest", "CHANGELOG", "lib/imexport.rb", "README.rdoc"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/crhym3/imexport}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Imexport", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{imexport}
19
+ s.rubygems_version = %q{1.3.2}
20
+ s.summary = %q{Simple import from a text file generated by mysql -E ...}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
data/lib/imexport.rb ADDED
@@ -0,0 +1,104 @@
1
+ module ImExport
2
+ def self.import(file_name, options = {})
3
+
4
+ ## e.g. :seminar => "seminar" => "Seminar" => Seminar
5
+ model = Kernel.const_get(options[:class_name].to_s.classify)
6
+
7
+ ## used to check for an existing record. so we do update_attributes()
8
+ # instead of save() in that case
9
+ find_method = "find_by_#{options[:find_by]}"
10
+
11
+ ## helps to distinguish between real columns and column content
12
+ # could be something like "COLUMN_"
13
+ columns_prefix = options[:db_columns_prefix].to_s
14
+
15
+ ## for each line, this is how we check for a new column or cont.
16
+ # from the previous line
17
+ scan_regexp = Regexp.new("^\\s*#{columns_prefix}(\\w+)\\: (.*)")
18
+
19
+ ## table columns --> model attributes mapping
20
+ # if an attribute is not specified and present as a column,
21
+ # it'll try figure it out:
22
+ # { 'title' => :title } - works w/o expicit mapping
23
+ attr_map = options[:map] || {}
24
+
25
+ ## Read the file
26
+ # we assume it's been created with vertial columns layout (mysql -E ...)
27
+ IO.foreach(file_name) do |line|
28
+ ## mysql -E ... does this:
29
+ # ********* 1. row **********
30
+ # column: value
31
+ # another_column: value
32
+ # ********* 2. row **********
33
+ unless (line =~ /^\*+ \d+\. row \*+$/).nil?
34
+ ## We've got a new row
35
+ unless block_given?
36
+ ## Save previously created object ...
37
+ if !@model.nil? and @model.valid?
38
+ if (s = model.send("#{find_method}", @model.title))
39
+ s.update_attributes(@model.attributes.reject{|k,v| v.nil?})
40
+ else
41
+ @model.save
42
+ end
43
+ else
44
+ $stderr.puts "\n>>> ERRORS while storing #{options[:class_name]}: #{@model.errors.full_messages.join('; ')}\n#{@model.inspect}" unless @model.nil?
45
+ end
46
+ else
47
+ ## or pass it to a block
48
+ # in this case we don't do any validations
49
+ yield(@model)
50
+ end
51
+
52
+ # ... then create a new one
53
+ #$stderr.puts "\n###################################"
54
+ @model = model.new
55
+ @last_column_name = nil
56
+ @last_column_content = nil
57
+ next
58
+ end
59
+
60
+ ## Cont. of the same row.
61
+ ## It's one table column at a time (or more lines).
62
+ ## Parse it and add set the corresponding attribute in the model
63
+ line.chomp!
64
+ column = line.scan(scan_regexp)
65
+ #$stderr.puts column.inspect
66
+
67
+ unless column.size > 0
68
+ ## this is a column continuation of the previous line
69
+ @last_column_content << '<br/>' << line
70
+ next
71
+ end
72
+
73
+ ## this is a new model attribute
74
+ # set last attribute in the model if defined
75
+ # we should have at least two items in the array
76
+ column.flatten!
77
+ @last_column_name = column.first
78
+ @last_column_content = column[1].gsub(/\t+/, " ").strip.gsub(/^\n+$/, "").gsub(/\n+/, "<br/>")
79
+
80
+ # in column-to-model map
81
+ if attr_map.include?(@last_column_name)
82
+ mattr = attr_map[@last_column_name]
83
+ case
84
+ when mattr.kind_of?(Symbol)
85
+ @model.send("#{mattr.to_s}=", @last_column_content)
86
+ when mattr.kind_of?(Proc)
87
+ mattr.call(@last_column_content, @model)
88
+ when mattr.kind_of?(Hash)
89
+ @model.send("#{mattr.keys.first}=", mattr.values.first.call(@last_column_content))
90
+ else
91
+ $stderr.puts "WARNING: don't know how to handle #{mattr.inspect}"
92
+ end
93
+
94
+ # simple auto-mapping
95
+ elsif @model.respond_to?(@last_column_name.to_sym)
96
+ @model.send("#{@last_column_name}=", @last_column_content)
97
+
98
+ # otherwise we don't know how to do it
99
+ else
100
+ $stderr.puts "WARNING: don't know how to set :#{@last_column_name}"
101
+ end
102
+ end # IO.foreach
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crhym3-imexport
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alex
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-21 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple import from a text file generated by mysql -E ...
17
+ email: alex@digns.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - lib/imexport.rb
25
+ - README.rdoc
26
+ files:
27
+ - Rakefile
28
+ - imexport.gemspec
29
+ - Manifest
30
+ - CHANGELOG
31
+ - lib/imexport.rb
32
+ - README.rdoc
33
+ has_rdoc: true
34
+ homepage: http://github.com/crhym3/imexport
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --line-numbers
38
+ - --inline-source
39
+ - --title
40
+ - Imexport
41
+ - --main
42
+ - README.rdoc
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "1.2"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project: imexport
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Simple import from a text file generated by mysql -E ...
64
+ test_files: []
65
+