spreader 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/spreader.rb +51 -0
- metadata +45 -0
data/lib/spreader.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Thanks to the example set at https://github.com/xml4r/libxml-ruby/wiki/Using-the-Reader-API
|
2
|
+
# http://snippets.dzone.com/posts/show/5051
|
3
|
+
# http://www.informit.com/articles/article.aspx?p=683059&seqNum=18
|
4
|
+
# http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html
|
5
|
+
# http://xml4r.github.com/libxml-ruby/rdoc/
|
6
|
+
# http://blog.codahale.com/2005/11/24/a-ruby-howto-writing-a-method-that-uses-code-blocks/
|
7
|
+
# http://libxml.rubyforge.org/svn/tags/REL_0_9_2/README
|
8
|
+
# http://guides.rubygems.org/make-your-own-gem/
|
9
|
+
# http://visual.merriam-webster.com/plants-gardening/gardening/seeding-planting-tools_2.php
|
10
|
+
|
11
|
+
class Spreader
|
12
|
+
#require 'rubygems'
|
13
|
+
require 'xml'
|
14
|
+
|
15
|
+
def extract(nodes)
|
16
|
+
return yield nodes
|
17
|
+
end
|
18
|
+
|
19
|
+
def transform(nodes, model_name, latitude_field_name, longitude_field_name)
|
20
|
+
prev_name = ''
|
21
|
+
seeds = ''
|
22
|
+
# i = 0
|
23
|
+
|
24
|
+
while nodes.read
|
25
|
+
unless nodes.node_type == XML::Reader::TYPE_END_ELEMENT
|
26
|
+
seeds << model_name + '.create(:' + longitude_field_name + ' => ' + nodes.value.to_s.split(',').first + ', ' + ':' + latitude_field_name + ' => ' + nodes.value.to_s.split(',')[1] + ")\n" if (prev_name == 'coordinates')
|
27
|
+
# seeds << 'thing_' + (i+=1).to_s + ":\n lat: " + nodes.value.to_s.split(',')[1] + "\n lng: " + nodes.value.to_s.split(',').first + "\n\n" if (prev_name == 'coordinates')
|
28
|
+
prev_name = nodes.name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
nodes.close
|
33
|
+
# puts seeds.chop
|
34
|
+
return seeds
|
35
|
+
end
|
36
|
+
|
37
|
+
def load(seeds)
|
38
|
+
File.open('db/seeds.rb', 'w') {|f| f.write(seeds.chop)}
|
39
|
+
# File.open('../test/fixtures/things.yml', 'w') {|f| f.write(seeds.chop.chop)}
|
40
|
+
system('rake db:seed')
|
41
|
+
end
|
42
|
+
|
43
|
+
def transformLoad(nodes, model_name, latitude_field_name, longitude_field_name)
|
44
|
+
load(transform(nodes, model_name, latitude_field_name, longitude_field_name))
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.seed(filename, model_name, latitude_field_name, longitude_field_name)
|
48
|
+
spreader = Spreader.new
|
49
|
+
spreader.transformLoad(spreader.extract(filename){|nodes| XML::Reader.file("#{nodes}", :options => XML::Parser::Options::NOBLANKS | XML::Parser::Options::NOENT)}, model_name, latitude_field_name, longitude_field_name)
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spreader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ''
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/spreader.rb
|
21
|
+
homepage:
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: ''
|
45
|
+
test_files: []
|