class Mumukit::Sync::Syncer

Public Class Methods

new(store, inflators = [], resource_classifier = nil) click to toggle source
# 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

Public Instance Methods

export!(sync_key = nil, resource) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

key_for(*args) click to toggle source
# File lib/mumukit/sync/syncer.rb, line 63
def key_for(*args)
  args.size == 1 ? args.first : Mumukit::Sync.key(*args)
end
locate(key) click to toggle source
# File lib/mumukit/sync/syncer.rb, line 59
def locate(key)
  @resource_classifier.call(key.kind).locate_resource(key.id)
end