Sha256: 221a6497833c2c4fc695e5c3a544db892359d123f0d39e993457af0f8a899dc0

Contents?: true

Size: 1.26 KB

Versions: 93

Compression:

Stored size: 1.26 KB

Contents

require 'pathname'

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

      def to_s
        File.read(filename) if File.exist?(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

    # An Exercise is used as part of a Repository
    # so expects :paths and :exercise_name to be defined.
    module Exercise
      def paths
        fail NotImplementedError, 'Should return a Generator::Paths object'
      end

      def exercise_name
        fail NotImplementedError, 'Should return a String object'
      end
    end
  end
end

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
trackler-2.1.0.4 tracks/ruby/lib/generator/files.rb
trackler-2.1.0.3 tracks/ruby/lib/generator/files.rb
trackler-2.1.0.2 tracks/ruby/lib/generator/files.rb
trackler-2.1.0.1 tracks/ruby/lib/generator/files.rb
trackler-2.1.0.0 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.55 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.54 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.53 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.52 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.51 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.50 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.49 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.48 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.47 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.46 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.45 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.44 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.43 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.42 tracks/ruby/lib/generator/files.rb
trackler-2.0.8.41 tracks/ruby/lib/generator/files.rb