Class: CheapSkate::CSVLoader
- Inherits:
-
Object
- Object
- CheapSkate::CSVLoader
- Defined in:
- lib/cheap_skate/models.rb
Instance Attribute Summary collapse
-
#field_meta ⇒ Object
readonly
Returns the value of attribute field_meta.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
-
#initialize(params, index) ⇒ CSVLoader
constructor
A new instance of CSVLoader.
- #parse ⇒ Object
- #parse_file ⇒ Object
Constructor Details
#initialize(params, index) ⇒ CSVLoader
Returns a new instance of CSVLoader.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/cheap_skate/models.rb', line 260 def initialize(params, index) @filename = params['stream.file'] if @filename @file = open(@filename) end @field_meta = {} params.each_pair do |key, val| next unless key =~ /^f\./ (f,field,arg) = key.split(".") @field_meta[field] ||={} @field_meta[field][arg] = val end @index = index end |
Instance Attribute Details
#field_meta ⇒ Object (readonly)
Returns the value of attribute field_meta.
259 260 261 |
# File 'lib/cheap_skate/models.rb', line 259 def @field_meta end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
259 260 261 |
# File 'lib/cheap_skate/models.rb', line 259 def fields @fields end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
259 260 261 |
# File 'lib/cheap_skate/models.rb', line 259 def file @file end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
259 260 261 |
# File 'lib/cheap_skate/models.rb', line 259 def filename @filename end |
Instance Method Details
#parse ⇒ Object
275 276 277 278 279 |
# File 'lib/cheap_skate/models.rb', line 275 def parse if @file parse_file end end |
#parse_file ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/cheap_skate/models.rb', line 281 def parse_file @fields = @file.gets.chomp.split(",") documents = [] while line = @file.gets line.chomp! doc = @index.create_document # keep track of where we are on the row i = 0 FasterCSV.parse_line(line).each do |field| unless field && @fields[i] i+= 1 next end if @fields[i] == "id" doc[@index.schema.id_field] = field.strip else if @field_meta[@fields[i]] && @field_meta[@fields[i]]["split"] == "true" field.split(@field_meta[@fields[i]]["separator"]).each do |f| doc.add_field(@fields[i], f.strip) end else doc.add_field(@fields[i], field.strip) end end i+=1 end @index << doc end end |