Sha256: 99d36ced0527a3a93845af1e0981bf212f25a8a5f87d8a5ebac566670db2f7dd

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

require 'pathname'

module GitCompound
  class Component
    # Component destination
    #
    class Destination
      attr_reader :path

      def initialize(component_path, component)
        raise CompoundSyntaxError, 'Destination cannot be empty' if
          component_path.nil? || component_path.empty?

        raise CompoundSyntaxError,
              'Destination should contain at least one directory' unless
          Pathname.new(component_path).each_filename.count > 0

        @component = component
        @path      = expand_path(component_path)
      end

      def exists?
        FileTest.exist?(@path)
      end

      def repository
        destination_repository =
          Repository::RepositoryLocal.new(@path)
        yield destination_repository if block_given?
        destination_repository
      end

      private

      def expand_path(component_path)
        pathname = Pathname.new(component_path)

        unless pathname.absolute?
          ancestor_paths = @component.ancestors.map(&:path)
          pathname = Pathname.new('.').join(*ancestor_paths) + pathname
        end

        Pathname.new("./#{pathname}").cleanpath.to_s + '/'
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
git_compound-0.2.2 lib/git_compound/component/destination.rb
git_compound-0.2.1 lib/git_compound/component/destination.rb
git_compound-0.2.0 lib/git_compound/component/destination.rb
git_compound-0.1.2 lib/git_compound/component/destination.rb
git_compound-0.1.1 lib/git_compound/component/destination.rb
git_compound-0.1.0 lib/git_compound/component/destination.rb
git_compound-0.0.10 lib/git_compound/component/destination.rb