diff --git a/lib/dry/files.rb b/lib/dry/files.rb index 631e2bf..5cdb37e 100644 --- a/lib/dry/files.rb +++ b/lib/dry/files.rb @@ -27,9 +27,9 @@ class Files # # @since 0.1.0 # @api public - def initialize(memory: false, adapter: Adapter.call(memory: memory, - newline: $INPUT_RECORD_SEPARATOR)) + def initialize(memory: false, adapter: Adapter.call(memory: memory), newline: $INPUT_RECORD_SEPARATOR) @adapter = adapter + @newline = newline end # Read file content @@ -74,7 +74,7 @@ def touch(path) # @since 0.1.0 # @api public def write(path, lines) - joined_lines = Array(lines).flatten.map { |line| line.chomp.concat($/) }.join + joined_lines = Array(lines).flatten.map { |line| line.chomp.concat(newline) }.join adapter.write(path, joined_lines) end @@ -777,6 +777,10 @@ def remove_block(path, target) # @api private attr_reader :adapter + # @since x.x.x + # @api private + attr_reader :newline + # @since 0.1.0 # @api private def match?(content, target) diff --git a/lib/dry/files/adapter.rb b/lib/dry/files/adapter.rb index ef36e21..9fc87a3 100644 --- a/lib/dry/files/adapter.rb +++ b/lib/dry/files/adapter.rb @@ -7,13 +7,13 @@ class Files class Adapter # @since 0.1.0 # @api private - def self.call(newline:, memory:) + def self.call(memory:) if memory require_relative "./memory_file_system" MemoryFileSystem.new else require_relative "./file_system" - FileSystem.new(newline: newline) + FileSystem.new end end end diff --git a/lib/dry/files/file_system.rb b/lib/dry/files/file_system.rb index f005e0c..3bd7745 100644 --- a/lib/dry/files/file_system.rb +++ b/lib/dry/files/file_system.rb @@ -17,10 +17,6 @@ class FileSystem # @api private attr_reader :file_utils - # @since x.x.x - # @api private - attr_reader :newline - # Creates a new instance # # @param file [Class] @@ -29,10 +25,9 @@ class FileSystem # @return [Dry::Files::FileSystem] # # @since 0.1.0 - def initialize(newline:, file: File, file_utils: FileUtils) + def initialize(file: File, file_utils: FileUtils) @file = file @file_utils = file_utils - @newline = newline end # Opens (or creates) a new file for both read/write operations. diff --git a/spec/unit/dry/files/adapter_spec.rb b/spec/unit/dry/files/adapter_spec.rb index c1edd26..07059ff 100644 --- a/spec/unit/dry/files/adapter_spec.rb +++ b/spec/unit/dry/files/adapter_spec.rb @@ -2,53 +2,21 @@ RSpec.describe Dry::Files::Adapter do describe ".call" do - let(:subject) { described_class.call(memory: memory, newline: newline) } + let(:subject) { described_class.call(memory: memory) } - describe "default newline character" do - let(:newline) { $INPUT_RECORD_SEPARATOR } + context "memory: true" do + let(:memory) { true } - context "memory: true" do - let(:memory) { true } - - it "returns memory file system adapter" do - expect(subject).to be_kind_of(Dry::Files::MemoryFileSystem) - end - end - - context "memory: false" do - let(:memory) { false } - - it "returns real file system adapter" do - expect(subject).to be_kind_of(Dry::Files::FileSystem) - end - - it "returns newline" do - expect(subject.newline).to eq("\n") - end + it "returns memory file system adapter" do + expect(subject).to be_kind_of(Dry::Files::MemoryFileSystem) end end - describe "override newline character" do - let(:newline) { "\r\n" } - - context "memory: true" do - let(:memory) { true } - - it "returns memory file system adapter" do - expect(subject).to be_kind_of(Dry::Files::MemoryFileSystem) - end - end - - context "memory: false" do - let(:memory) { false } - - it "returns real file system adapter" do - expect(subject).to be_kind_of(Dry::Files::FileSystem) - end + context "memory: false" do + let(:memory) { false } - it "returns newline" do - expect(subject.newline).to eq("\r\n") - end + it "returns real file system adapter" do + expect(subject).to be_kind_of(Dry::Files::FileSystem) end end end diff --git a/spec/unit/dry/files/file_system_spec.rb b/spec/unit/dry/files/file_system_spec.rb index 457b996..664d1d9 100644 --- a/spec/unit/dry/files/file_system_spec.rb +++ b/spec/unit/dry/files/file_system_spec.rb @@ -7,8 +7,6 @@ let(:root) { Pathname.new(Dir.pwd).join("tmp", SecureRandom.uuid).tap(&:mkpath) } let(:newline) { $INPUT_RECORD_SEPARATOR } - subject { described_class.new(newline: newline) } - after do FileUtils.remove_entry_secure(root) end