Class: Mumukit::Sync::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/sync/syncer.rb

Instance Method Summary collapse

Constructor Details

#initialize(store, inflators = [], resource_classifier = nil) ⇒ Syncer

Returns a new instance of Syncer.



12
13
14
15
16
# File 'lib/mumukit/sync/syncer.rb', line 12

def initialize(store, inflators = [], resource_classifier = nil)
  @store = store
  @inflators = inflators
  @resource_classifier ||= proc { |kind| kind.as_module }
end

Instance Method Details

#export!(sync_key = nil, resource) ⇒ Object



51
52
53
54
55
# File 'lib/mumukit/sync/syncer.rb', line 51

def export!(sync_key = nil, resource)
  sync_key ||= resource.sync_key
  resource_h = resource.to_resource_h
  @store.write_resource!(sync_key, resource_h)
end

#import!(sync_key = nil, resource) ⇒ Object



39
40
41
42
43
44
# File 'lib/mumukit/sync/syncer.rb', line 39

def import!(sync_key = nil, resource)
  sync_key ||= resource.sync_key
  resource_h = @store.read_resource(sync_key)
  Mumukit::Sync::Inflator.inflate_with! sync_key, resource_h, @inflators
  resource.import_from_resource_h!(resource_h)
end

#import_all!(id_regex = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/mumukit/sync/syncer.rb', line 23

def import_all!(id_regex = nil)
  sync_keys_matching(id_regex).each do |key|
    puts "Importing #{key.kind} #{key.id}"
    begin
      locate_and_import! key
    rescue => e
      puts "Ignoring #{key.id} because of import error #{e}"
    end
  end
end

#locate_and_export!(*args) ⇒ Object



46
47
48
49
# File 'lib/mumukit/sync/syncer.rb', line 46

def locate_and_export!(*args)
  sync_key = key_for(*args)
  locate(sync_key).tap { |it| export! sync_key, it }
end

#locate_and_import!(*args) ⇒ Object



34
35
36
37
# File 'lib/mumukit/sync/syncer.rb', line 34

def locate_and_import!(*args)
  sync_key = key_for(*args)
  locate(sync_key).tap { |it| import! sync_key, it }
end

#sync_keys_matching(id_regex = nil) ⇒ Object



18
19
20
21
# File 'lib/mumukit/sync/syncer.rb', line 18

def sync_keys_matching(id_regex = nil)
  id_regex ||= /.*/
  @store.sync_keys.select { |key| id_regex.matches? key.id }
end