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.117 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.116 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.115 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.114 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.113 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.111 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.110 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.109 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.108 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.107 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.106 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.105 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.104 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.103 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.102 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.101 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.100 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.99 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.98 tracks/ruby/lib/generator/files.rb
trackler-2.2.1.97 tracks/ruby/lib/generator/files.rb