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.0.6.24 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.23 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.22 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.21 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.20 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.19 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.18 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.17 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.16 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.15 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.14 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.13 tracks/ruby/lib/generator/files.rb
trackler-2.0.6.12 tracks/ruby/lib/generator/files.rb