Class: Spreader

Inherits:
Object
  • Object
show all
Defined in:
lib/spreader.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (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