Class: DataShift::DocContext

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/datashift/doc_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logdir, #logdir=, #logger, #verbose

Constructor Details

#initialize(klass) ⇒ DocContext

Returns a new instance of DocContext.



30
31
32
33
34
35
36
37
38
# File 'lib/datashift/doc_context.rb', line 30

def initialize( klass )
  reset_klass(klass)

  @headers = DataShift::Headers.new(:na)

  @progress_monitor = ProgressMonitor.new

  @reporters = [DataShift::Reporters::BasicStdoutReporter.new(@progress_monitor)]
end

Instance Attribute Details

#headersObject

The inbound document headers



24
25
26
# File 'lib/datashift/doc_context.rb', line 24

def headers
  @headers
end

#klassObject (readonly)

Returns the value of attribute klass.



16
17
18
# File 'lib/datashift/doc_context.rb', line 16

def klass
  @klass
end

#load_objectObject

Returns the value of attribute load_object.



18
19
20
# File 'lib/datashift/doc_context.rb', line 18

def load_object
  @load_object
end

#node_contextObject

The current Node Context - method_binding (includes inbound_column), row, data



21
22
23
# File 'lib/datashift/doc_context.rb', line 21

def node_context
  @node_context
end

#progress_monitorObject

Returns the value of attribute progress_monitor.



26
27
28
# File 'lib/datashift/doc_context.rb', line 26

def progress_monitor
  @progress_monitor
end

#reportersObject

Returns the value of attribute reporters.



26
27
28
# File 'lib/datashift/doc_context.rb', line 26

def reporters
  @reporters
end

Instance Method Details

#all_or_nothing?Boolean

Only save object if all columns ok, or allow errors in individual columns

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/datashift/doc_context.rb', line 64

def all_or_nothing?
  true
  # TODO: - read in from configration
end

#create_node_context(method_binding, row_idx, data) ⇒ Object



58
59
60
61
# File 'lib/datashift/doc_context.rb', line 58

def create_node_context(method_binding, row_idx, data)
  @node_context = DataShift::NodeContext.new(self, method_binding, row_idx, data)
  @node_context
end

#current_errorsObject



69
70
71
# File 'lib/datashift/doc_context.rb', line 69

def current_errors
  load_object.errors.full_messages
end

#errors?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/datashift/doc_context.rb', line 73

def errors?
  !load_object.errors.empty? || progress_monitor.current_status == :failure
end

#new_load_objectObject



53
54
55
56
# File 'lib/datashift/doc_context.rb', line 53

def new_load_object
  @load_object = klass.new
  @load_object
end

#reset(object = nil) ⇒ Object

Reset the database object to be populated



47
48
49
50
51
# File 'lib/datashift/doc_context.rb', line 47

def reset(object = nil)
  @node_context = DataShift::EmptyContext.new

  @load_object = LoadObject.new(object || new_load_object)
end

#reset_klass(klass) ⇒ Object



40
41
42
43
# File 'lib/datashift/doc_context.rb', line 40

def reset_klass( klass )
  @klass = klass
  reset
end

#saveObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/datashift/doc_context.rb', line 116

def save
  return false unless  load_object

  logger.debug("SAVING #{load_object.class} : #{load_object.inspect}")
  begin
    load_object.save!
  rescue StandardError => e
    logger.error( "Save Error : #{e.inspect} on #{load_object.class}")
    logger.error(e.backtrace)
    false
  end
end

#save_and_monitor_progressObject

Save the object and then report the outcome to ProgressMonitor, as either success or failure



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/datashift/doc_context.rb', line 94

def save_and_monitor_progress
  if(errors? && all_or_nothing?)
    # Error already logged with doc_context.failure
    logger.warn "SAVE skipped due to Errors for Row #{node_context.row_index} - #{node_context.method_binding.spp}"
  else
    if save
      @progress_monitor.success(load_object)

      logger.info("Successfully Processed [#{node_context.method_binding.spp}]")
      logger.info("Successfully SAVED Object #{@progress_monitor.success_inbound_count} - [#{load_object.id}]")
    else

      failed = FailureData.new(load_object, node_context, current_errors)

      @progress_monitor.failure(failed)

      logger.info("Failed to Process [#{node_context.method_binding.spp}]")
      logger.info("Failed to SAVE Object #{@progress_monitor.success_inbound_count} - [#{load_object.inspect}]")
    end
  end
end

#save_if_newObject

This method usually called during processing to avoid errors with associations like

<ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved>

If the object is still invalid at this point probably indicates compulsory columns on model have not been processed before associations on that model.

You can provide a custom sort function to the Collection of model methods (which are comparable) to fix this.

Raises:

  • (DataShift::SaveError)


84
85
86
87
88
89
90
# File 'lib/datashift/doc_context.rb', line 84

def save_if_new
  return unless load_object.new_record?

  return save if load_object.valid?

  raise DataShift::SaveError, "Cannot Save Invalid #{load_object.class} Record : #{current_errors}"
end