Class: JunglePath::DBAccess::Import::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/jungle_path/db_access/import/select.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, logger = nil) ⇒ Select



11
12
13
14
15
# File 'lib/jungle_path/db_access/import/select.rb', line 11

def initialize(config, logger=nil)
  @logger = logger
  @path = config.import_file_path
  #@db = JunglePath::DBAccess::IO::DB.new(config, logger)
end

Instance Method Details

#_model(model) ⇒ Object



17
18
19
# File 'lib/jungle_path/db_access/import/select.rb', line 17

def _model(model)
  nil
end

#_model_by_any(model) ⇒ Object



21
22
23
# File 'lib/jungle_path/db_access/import/select.rb', line 21

def _model_by_any(model)
  nil
end

#_models(model) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/jungle_path/db_access/import/select.rb', line 25

def _models(model)
  #if @db
  #  @db.select._models(model)
  #else
    from_file(model)
  #end
end

#_models_from_file(model) ⇒ Object



33
34
35
# File 'lib/jungle_path/db_access/import/select.rb', line 33

def _models_from_file(model)
  from_file(model)
end

#from_file(model) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jungle_path/db_access/import/select.rb', line 37

def from_file(model)
  # select all of a given model.
  file_name = JunglePath::DBAccess::Import.data_file_name(@path, model.class.table_name)
  puts "file_name: #{file_name}."
  models = []
  lines = File.readlines(file_name)
  puts "lines.count: #{lines.count}."
  lines.each do |line|
    values = line.split("\t")
    hash = to_hash(model, values)
    params = JunglePath::Controller::Base.transform(hash, model.class.columns)
    models << model.class.new(params, false)
  end
  models
end

#to_hash(model, values) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jungle_path/db_access/import/select.rb', line 53

def to_hash(model, values)
  hash = {}
  i = 0
  model._columns.keys.each do |key|
    value = values[i]
    if value == '\N'
      value = nil
    end
    hash[key] = value
    i += 1
  end
  hash
end