Class: Offroad::MirrorData

Inherits:
Object
  • Object
show all
Defined in:
lib/mirror_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, options = {}) ⇒ MirrorData

Returns a new instance of MirrorData.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mirror_data.rb', line 7

def initialize(group, options = {})
  @group = group
  @initial_mode = options.delete(:initial_mode) || false
  @skip_validation = options.delete(:skip_validation) || false
  
  raise PluginError.new("Invalid option keys") unless options.size == 0
  
  unless Offroad::app_offline? && @initial_mode
    raise PluginError.new("Need group") unless @group.is_a?(Offroad::group_base_model) && !@group.new_record?
    raise DataError.new("Group must be in offline mode") unless @group.group_offline?
  end

  @imported_models_to_validate = []
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/mirror_data.rb', line 5

def group
  @group
end

#modeObject (readonly)

Returns the value of attribute mode.



5
6
7
# File 'lib/mirror_data.rb', line 5

def mode
  @mode
end

Instance Method Details

#load_downwards_data(src) ⇒ Object

Raises:



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
# File 'lib/mirror_data.rb', line 53

def load_downwards_data(src)
  raise PluginError.new("Can only load downwards data in offline mode") unless Offroad.app_offline?
  
  read_data_from("online", src) do |cs, mirror_info, cargo_group_state|
    raise DataError.new("Unexpected initial file value") unless mirror_info.initial_file == @initial_mode
    
    group_cargo_name = MirrorData::data_cargo_name_for_model(Offroad::group_base_model)
    if mirror_info.initial_file
      raise DataError.new("No group data in initial down mirror file") unless cs.has_cargo_named?(group_cargo_name)
      # This is an initial mirror file, so we want it to determine the entirety of the database's new state
      # However, existing data is safe if there's a mid-import error; read_data_from places us in a transaction
      delete_all_existing_database_records!
      
      import_global_cargo(cs) # Global cargo must be done first because group data might belong_to global data
      import_group_specific_cargo(cs)
    else
      # Regular, non-initial down mirror file
      unless cargo_group_state.confirmed_global_data_version > @group.group_state.confirmed_global_data_version
        raise OldDataError.new("File contains old down-mirror data")
      end
      import_global_cargo(cs)
    end
    
    # Load information into our group state that the online app is in a better position to know about
    @group = Offroad::offline_group if @initial_mode
  end
end

#load_upwards_data(src) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mirror_data.rb', line 40

def load_upwards_data(src)
  raise PluginError.new("Can only load upwards data in online mode") unless Offroad.app_online?
  raise PluginError.new("No such thing as initial upwards data") if @initial_mode

  read_data_from("offline", src) do |cs, mirror_info, cargo_group_state|
    unless cargo_group_state.confirmed_group_data_version > @group.group_state.confirmed_group_data_version
      raise OldDataError.new("File contains old up-mirror data")
    end
    import_group_specific_cargo(cs)
    @group.group_offline = false if cargo_group_state.group_locked?
  end
end

#write_downwards_data(tgt = nil) ⇒ Object

Raises:



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

def write_downwards_data(tgt = nil)
  raise PluginError.new("Can only write downwards data in online mode") unless Offroad.app_online?
  write_data(tgt) do |cs|
    add_global_cargo(cs)
    if @initial_mode
      add_group_specific_cargo(cs)
    end
  end
end

#write_upwards_data(tgt = nil) ⇒ Object

Raises:



22
23
24
25
26
27
28
# File 'lib/mirror_data.rb', line 22

def write_upwards_data(tgt = nil)
  raise PluginError.new("Can only write upwards data in offline mode") unless Offroad.app_offline?
  raise PluginError.new("No such thing as initial upwards data") if @initial_mode
  write_data(tgt) do |cs|
    add_group_specific_cargo(cs)
  end
end