Sha256: 4218b32c4bcdeb2362790bae69143a99de2043d299bce349c016c72c3b3a7313

Contents?: true

Size: 1014 Bytes

Versions: 187

Compression:

Stored size: 1014 Bytes

Contents

require 'pathname'

module Generator
  module Files
    def self.read(filename)
      File.read(filename) if File.exist?(filename)
    end

    class Readable
      attr_reader :filename, :repository_root
      def initialize(filename:, repository_root: nil)
        @filename = filename
        @repository_root = repository_root
      end

      def to_s
        Files.read(filename)
      end

      def abbreviated_commit_hash
        GitCommand.abbreviated_commit_hash(git_path, relative_filename)
      end

      private

      def relative_filename
        Pathname.new(filename).relative_path_from(Pathname.new(repository_root)).to_s
      end

      def git_path
        File.join(repository_root, '.git')
      end
    end

    class Writable < Readable
      def save(content)
        write(content) unless content == to_s
        content
      end

      private

      def write(content)
        File.open(filename, 'w') do |file|
          file.write content
        end
      end
    end
  end
end

Version data entries

187 entries across 187 versions & 1 rubygems

Version Path
trackler-2.2.1.137 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.136 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.135 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.134 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.133 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.132 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.131 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.130 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.129 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.128 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.127 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.126 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.125 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.124 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.123 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.122 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.121 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.120 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.119 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.118 tracks/ruby/lib/generator/files.rb