Sha256: cf81b44ea81d473244b683e138ebfad94813240c00048771d938d6188ec003c6

Contents?: true

Size: 1011 Bytes

Versions: 1

Compression:

Stored size: 1011 Bytes

Contents

require_relative 'client'
require_relative 'project'
require_relative 'registration'
require_relative 'repos'
require_relative 'uuid'

module Kamerling
  class Registrar
    def self.register(addr:, message:, repos: Repos)
      new(addr: addr, message: message, repos: repos).register
    end

    def initialize(addr:, message:, repos:)
      @addr, @message, @repos = addr, message, repos
    end

    def register
      client.addr = addr
      repos << client
      repos << registration
    end

    attr_reader :addr, :message, :repos
    private     :addr, :message, :repos

    private

    def client
      @client ||= find_or_create_client
    end

    def find_or_create_client
      repos[Client][message.client_uuid]
    rescue Repo::NotFound
      Client.new(addr: addr, uuid: message.client_uuid)
    end

    def project
      @project ||= repos[Project][message.project_uuid]
    end

    def registration
      Registration.new(addr: addr, client: client, project: project)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.3 lib/kamerling/registrar.rb