Sha256: b7ae751191fd1bdcf0ab87fe9db724101620fd8fc5897d57f5e673905d04749e

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# coding: utf-8

module Abak::Flow
  class Repository
    extend Forwardable

    REMOTES = %w{origin upstream}.map(&:freeze)

    def initialize
      @_errors = Hash.new

      create_public_instance_methods
    end

    def valid?
      @_errors = Hash.new
      @_errors["origin"] = ['not_set'] if origin.nil?
      @_errors["upstream"] = ['not_set'] if upstream.nil?

      @_errors.empty?
    end

    def errors
      ErrorsPresenter.new(self, @_errors)
    end

    private

    def create_public_instance_methods
      remotes = Hash[fetch_remotes_from_git]

      REMOTES.each do |name|
        define_singleton_method(name) { remotes[name] }
      end
    end

    def fetch_remotes_from_git
      Manager.git.remotes.select { |remote| REMOTES.include?(remote.name) }
        .map { |remote| create_remote(remote) }.compact
    end

    def create_remote(remote)
      matches = /.+.github\.com[\:|\/](?<owner>.+)\/(?<project>.+).git/.match(remote.url)

      if !matches.nil? && matches.captures.length == 2
        [remote.name, Remote.new(matches[:owner], matches[:project], remote)]
      end
    end

    class Remote < Struct.new(:owner, :project, :repo)
      def to_s
        "#{owner}/#{project}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
abak-flow-1.1.1 lib/abak-flow/repository.rb
abak-flow-1.1.0 lib/abak-flow/repository.rb