Module: MapFields::InstanceMethods

Defined in:
lib/map_fields.rb

Instance Method Summary collapse

Instance Method Details

#fields_mapped?Boolean

Returns:

  • (Boolean)

Raises:

  • (@map_fields_error)


91
92
93
94
# File 'lib/map_fields.rb', line 91

def fields_mapped?
  raise @map_fields_error if @map_fields_error
  @mapped_fields
end

#map_field_parameters(&block) ⇒ Object



96
97
98
# File 'lib/map_fields.rb', line 96

def map_field_parameters(&block)

end

#map_fieldsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/map_fields.rb', line 28

def map_fields
  default_options = {
    :file_field => 'file',
    :params => []
  }
  options = default_options.merge(
    self.class.read_inheritable_attribute("map_fields_options_#{params[:action]}")
  )
  if session[:map_fields].nil? || params[options[:file_field]]
    session[:map_fields] = {}
    if params[options[:file_field]].blank?
      @map_fields_error = MissingFileContentsError
      return
    end

    file_field = params[options[:file_field]]

    temp_path = File.join(Dir::tmpdir, "map_fields_#{Time.now.to_i}_#{$$}")
    File.open(temp_path, 'wb') do |f|
      f.write file_field.read
    end

    session[:map_fields][:file] = temp_path
  end

  if params[:fields].nil?
    @rows = []
    parser_options = session[:parser_options] = params[:parser_options].symbolize_keys
    CsvParser.foreach(temp_path, parser_options) do |row|
      @rows << row
      break if @rows.size == 5
    end
    expected_fields = self.class.read_inheritable_attribute("map_fields_fields_#{params[:action]}")
    @fields = ([nil] + expected_fields).inject([]){ |o, e| o << [e, o.size]}
    @parameters = []
    options[:params].each do |param|
      @parameters += ParamsParser.parse(params, param)
    end
  else
    if @set
      @mapped_fields = MappedFields.new(session[:map_fields][:file],
                                        @set.fields,
                                        @set.ignore_first_row,
                                        @set.parser_options)

    else
      if session[:map_fields][:file].nil? || params[:fields].nil?
        session[:map_fields] = nil
        @map_fields_error =  InconsistentStateError
      else
        @mapped_fields = MappedFields.new(session[:map_fields][:file],
                                          params[:fields],
                                          params[:ignore_first_row],
                                          session[:parser_options])
      end
    end
  end
end

#map_fields_cleanupObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/map_fields.rb', line 100

def map_fields_cleanup
  if @mapped_fields
    if session[:map_fields][:file]
      File.delete(session[:map_fields][:file])
    end
    session[:map_fields] = nil
    @mapped_fields = nil
    @map_fields_error = nil
  end
end

#mapped_fieldsObject



87
88
89
# File 'lib/map_fields.rb', line 87

def mapped_fields
  @mapped_fields
end