Class: Importu::Record

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/importu/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(importer, data, raw_data) ⇒ Record

Returns a new instance of Record.



12
13
14
# File 'lib/importu/record.rb', line 12

def initialize(importer, data, raw_data)
  @importer, @data, @raw_data = importer, data, raw_data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



116
117
118
119
120
121
122
# File 'lib/importu/record.rb', line 116

def method_missing(meth, *args, &block)
  if converters[meth]
    convert(args[0], meth, args[1]||{}) # convert(name, type, options)
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/importu/record.rb', line 4

def data
  @data
end

#importerObject (readonly)

Returns the value of attribute importer.



4
5
6
# File 'lib/importu/record.rb', line 4

def importer
  @importer
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



4
5
6
# File 'lib/importu/record.rb', line 4

def raw_data
  @raw_data
end

Instance Method Details

#assign_to(object, action, &block) ⇒ Object



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
# File 'lib/importu/record.rb', line 54

def assign_to(object, action, &block)
  @object, @action = object, action

  instance_eval(&preprocessor) if preprocessor
  instance_exec(object, record_hash, &block) if block

  # filter out any fields we're not allowed to copy for this action
  allowed_fields = definitions.select {|n,d| d[action] }.keys
  concrete_fields = definitions.reject {|n,d| d[:abstract] }.keys
  field_names = record_hash.keys & allowed_fields & concrete_fields

  unsupported = field_names.reject {|n| object.respond_to?("#{n}=") }
  if unsupported.any?
    raise "model does not support assigning fields: #{unsupported.to_sentence}"
  end

  (record_hash.keys & allowed_fields & concrete_fields).each do |name|
    if object.respond_to?("#{name}=")
      object.send("#{name}=", record_hash[name])
    else
    end
  end

  instance_eval(&postprocessor) if postprocessor

  object
end

#convert(name, type, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/importu/record.rb', line 24

def convert(name, type, options = {})
  type, options = type[:to], type if type.kind_of?(Hash)
  converter = type ? converters[type] : options[:converter] \
    or raise "converter not found: #{type}"

  # TODO: defining options in field definition is deprecated
  definition = definitions[name] || {}
  options = definition.merge(options)

  begin
    value = instance_exec(name, options, &converter)
    value.nil? ? options[:default] : value

  rescue Importu::MissingField => e
    raise e if options[:required]
    options[:default]

  rescue ArgumentError => e
    # conversion of field value most likely failed
    raise Importu::FieldParseError, "#{name}: #{e.message}"
  end
end

#field_value(name, options = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/importu/record.rb', line 47

def field_value(name, options = {})
  definition = definitions[name] \
    or raise "importer field not defined: #{name}"

  convert(name, nil, definition.merge(options))
end

#record_hashObject Also known as: record



16
17
18
# File 'lib/importu/record.rb', line 16

def record_hash
  @record_hash ||= generate_record_hash
end

#save!Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/importu/record.rb', line 82

def save!
  return :unchanged unless @object.changed?

  begin
    @object.save!
    case @action
      when :create then :created
      when :update then :updated
    end

  rescue ActiveRecord::RecordInvalid => e
    error_msgs = @object.errors.map do |name,message|
      name = definitions[name][:label] if definitions[name]
      name == 'base' ? message : "#{name} #{message}"
    end.join(', ')

    raise Importu::InvalidRecord, error_msgs, @object.errors.full_messages
  end
end

#to_hashObject



20
21
22
# File 'lib/importu/record.rb', line 20

def to_hash
  record_hash
end