Class: Spreader
- Inherits:
-
Object
- Object
- Spreader
- Defined in:
- lib/spreader.rb
Overview
Thanks to the example set at github.com/xml4r/libxml-ruby/wiki/Using-the-Reader-API snippets.dzone.com/posts/show/5051 www.informit.com/articles/article.aspx?p=683059&seqNum=18 www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html xml4r.github.com/libxml-ruby/rdoc/ blog.codahale.com/2005/11/24/a-ruby-howto-writing-a-method-that-uses-code-blocks/ libxml.rubyforge.org/svn/tags/REL_0_9_2/README guides.rubygems.org/make-your-own-gem/ visual.merriam-webster.com/plants-gardening/gardening/seeding-planting-tools_2.php stackoverflow.com/questions/2741260/can-ruby-access-output-from-shell-commands-as-it-appears www.wallpaperama.com/forums/how-to-count-the-number-of-lines-in-a-file-in-linux-shell-command-t1084.html www.techotopia.com/index.php/Working_with_Files_in_Ruby
Class Method Summary collapse
Instance Method Summary collapse
- #extract(data) ⇒ Object
- #load(seeds) ⇒ Object
- #transform(data, model_name, latitude_field_name, longitude_field_name) ⇒ Object
- #transformLoad(data, model_name, latitude_field_name, longitude_field_name) ⇒ Object
- #xml?(relative_path) ⇒ Boolean
Class Method Details
.sow(relative_path, model_name, latitude_field_name, longitude_field_name) ⇒ Object
65 66 67 68 69 |
# File 'lib/spreader.rb', line 65 def self.sow(relative_path, model_name, latitude_field_name, longitude_field_name) spreader = Spreader.new spreader.transformLoad(spreader.extract(relative_path){|data| XML::Reader.file("#{data}", :options => XML::Parser::Options::NOBLANKS | XML::Parser::Options::NOENT)}, model_name, latitude_field_name, longitude_field_name) if spreader.xml?(relative_path) spreader.transformLoad(spreader.extract(relative_path){|data| File.open("#{data}")}, model_name, latitude_field_name, longitude_field_name) if !spreader.xml?(relative_path) end |
Instance Method Details
#extract(data) ⇒ Object
19 20 21 |
# File 'lib/spreader.rb', line 19 def extract(data) return yield data end |
#load(seeds) ⇒ Object
56 57 58 59 |
# File 'lib/spreader.rb', line 56 def load(seeds) File.file?('db/seeds.rb') && File.stat('db/seeds.rb').size > 0 ? File.open('db/seeds.rb', 'a') {|f| f.write("\n" + seeds.chop)} : File.open('db/seeds.rb', 'a') {|f| f.write(seeds.chop)} system('rake db:seed') end |
#transform(data, model_name, latitude_field_name, longitude_field_name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/spreader.rb', line 32 def transform(data, model_name, latitude_field_name, longitude_field_name) seeds = '' if @xml prev_name = '' # i = 0 while data.read unless data.node_type == XML::Reader::TYPE_END_ELEMENT seeds << model_name + '.create(:' + longitude_field_name + ' => ' + data.value.to_s.split(',').first + ', ' + ':' + latitude_field_name + ' => ' + data.value.to_s.split(',')[1] + ")\n" if (prev_name == 'coordinates') # 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') prev_name = data.name end end # puts seeds.chop else data.each do |datum| seeds << model_name + '.create(:' + longitude_field_name + ' => ' + datum.split(',').first + ', ' + ':' + latitude_field_name + ' => ' + datum.split(',').last.chop + ")\n" end end data.close return seeds end |
#transformLoad(data, model_name, latitude_field_name, longitude_field_name) ⇒ Object
61 62 63 |
# File 'lib/spreader.rb', line 61 def transformLoad(data, model_name, latitude_field_name, longitude_field_name) load(transform(data, model_name, latitude_field_name, longitude_field_name)) end |
#xml?(relative_path) ⇒ Boolean
23 24 25 26 27 28 29 30 |
# File 'lib/spreader.rb', line 23 def xml?(relative_path) head = IO.popen('head -' + ((IO.popen("wc -l #{relative_path}").gets).to_i + 1).to_s + " #{relative_path}") while line = head.gets @xml = true if (line.include? '<') && !@xml end return @xml end |