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.



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

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)


62
63
64
65
# File 'lib/datashift/doc_context.rb', line 62

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

#create_node_context(method_binding, row_idx, data) ⇒ Object



56
57
58
59
# File 'lib/datashift/doc_context.rb', line 56

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



76
77
78
# File 'lib/datashift/doc_context.rb', line 76

def current_errors
  load_object.errors.full_messages
end

#errors?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/datashift/doc_context.rb', line 80

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

#failed_countObject



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

def failed_count
  progress_monitor.failed_count
end

#loaded_countObject

TOFIX - use delegation to doc_context.progress_monitor



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

def loaded_count
  progress_monitor.loaded_count
end

#new_load_objectObject



51
52
53
54
# File 'lib/datashift/doc_context.rb', line 51

def new_load_object
  @load_object = klass.new
  @load_object
end

#reset(object = nil) ⇒ Object

Reset the database object to be populated



45
46
47
48
49
# File 'lib/datashift/doc_context.rb', line 45

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

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

#reset_klass(klass) ⇒ Object



38
39
40
41
# File 'lib/datashift/doc_context.rb', line 38

def reset_klass( klass )
  @klass = klass
  reset
end

#saveObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/datashift/doc_context.rb', line 123

def save
  return false unless  load_object

  logger.debug("SAVING #{load_object.class} : #{load_object.inspect}")
  begin
    load_object.save!
  rescue => 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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/datashift/doc_context.rb', line 101

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)


91
92
93
94
95
96
97
# File 'lib/datashift/doc_context.rb', line 91

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