csv_patch 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDkxYjBmMGNhMjY1ZTY5ZjNiZTEwNWI2YjFjZjUwNGRiZjg3OTVhMQ==
4
+ MmUwOTQxZTdjZDZlOGE3YjVlNjFiOGJmZjg4OWI3ZTU0ODk4ZjQzZA==
5
5
  data.tar.gz: !binary |-
6
- YTJhNGEwZTdjNzE5MjRiNzg2MjY3Njg1ZGE4MWY2ZDQ2MDU0MDhkMg==
6
+ YTkyZmNjMTYzNmFjOTJjMDVlMWZkYmY0ZTJkZGQxMWU2MTJiOTFmMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDYyYTliMjU1MDYwMWE5NmM4MjdkNjhhNzNjYzI2ZGM1ZWI0YzhhOGUzOGU5
10
- NDFlN2ZmMmJhMTdiMDJmMGNlNTViZTI3OTMzNmY2ZTE5M2FkYWI0Mzc5ZmFi
11
- ZjEyYmQyNzhlNWQ3N2MyMDYyMjdjMWIxMDhhOTkwMzBlMGYzZDY=
9
+ OTk4MzUzOGQ5OWFjY2IyNjY4MDQ0ZmRlODYxYjlhYzYwZmM0MWIyM2E0ODYy
10
+ MTg2M2Q4YzAzMGVjOTM2OGFhNjlhODQ3YzdmYjQ1MWJlNDc2NDA5MDU2NmYy
11
+ YmEzMmY5NDJkNzRjMGQ3MzJlMmQ3MTI0ZTQyODRkMzQyNmRkMjI=
12
12
  data.tar.gz: !binary |-
13
- MDNlYjkwYjg0YTkwNGU2YjYzZTBlYmUzZTk2ZWEzOGE0OGE2MjA5NmViZTVh
14
- MjM1NjYyMDEwOTlkNTZmNTUzZTIwYWFiNzg0NTJkMmIxZTZlYTdhM2ZlNzc3
15
- MjgxZDVlNjc1ZjhkZDc5NWE4N2RkNzk0NTQ0NjBiMjJkMjhiZGI=
13
+ MjcxY2E5OTg2ZTY4ZDI0ZWQyYTg0MjkzN2UzOTcxYTMxNGM2NGZjZWVlMmZi
14
+ YTQ1MTk3MzU4ZTZhZjRlNjZiZjk4MWZkZDAyMWJkMjllYTZlYzMwNzJlYmFm
15
+ NWQ1ODhjYTNkYWExYTVmM2U2MGNiN2FlZjdkZDBiZGI3ZjVjOWI=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
data/bin/csv_patch CHANGED
@@ -20,7 +20,7 @@ OptionParser.new do |opts|
20
20
  options[:changes] = File.new(changes_file, 'r')
21
21
  end
22
22
 
23
- opts.on('-bBATCH_SIZE', '--batch_size=BATCH_SIZE', 'Number of changes to read before applying a patch') do |batch_size|
23
+ opts.on('-bBATCH_SIZE', '--batch_size=BATCH_SIZE', 'Number of changes to read before applying a patch. Defaults to 500.') do |batch_size|
24
24
  options[:batch_size] = batch_size.to_i
25
25
  end
26
26
 
data/csv_patch.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: csv_patch 1.0.2 ruby lib
5
+ # stub: csv_patch 1.1.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "csv_patch"
9
- s.version = "1.0.2"
9
+ s.version = "1.1.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Lyudmil"]
14
- s.date = "2015-10-28"
14
+ s.date = "2015-11-03"
15
15
  s.description = "Applies a list of changes in a given format to a CSV file"
16
16
  s.email = "lyudmilangelov@gmail.com"
17
17
  s.executables = ["csv_patch"]
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "bin/csv_patch",
24
24
  "csv_patch.gemspec",
25
25
  "lib/csv_patch.rb",
26
+ "lib/csv_patch/batches.rb",
26
27
  "lib/csv_patch/compression.rb",
27
28
  "lib/csv_patch/operation.rb",
28
29
  "lib/csv_patch/patch.rb",
@@ -0,0 +1,62 @@
1
+ require 'csv_patch/stream_batch'
2
+ require 'csv_patch/patch'
3
+
4
+ module CsvPatch
5
+
6
+ class Batches
7
+
8
+ DEFAULT_BATCH_SIZE = 500
9
+
10
+ def initialize options
11
+ @intermediate_files = []
12
+ @input, @output = options[:input], options[:output]
13
+ @batches = StreamBatch.new(options[:changes], options[:batch_size] || DEFAULT_BATCH_SIZE)
14
+ end
15
+
16
+ def execute
17
+ apply_changes_in_batches
18
+ close_intermediate_files
19
+ end
20
+
21
+ private
22
+
23
+ def apply_changes_in_batches
24
+ @batches.each { |batch| patch_for(batch).apply }
25
+ end
26
+
27
+ def patch_for batch
28
+ Patch.new(patch_options(batch))
29
+ end
30
+
31
+ def patch_options batch
32
+ { input: input_for_next_patch, output: output_for_next_patch(batch), changes: batch.changes }
33
+ end
34
+
35
+ def close_intermediate_files
36
+ @intermediate_files.each(&:close)
37
+ end
38
+
39
+ def input_for_next_patch
40
+ last_intermediate_file || @input
41
+ end
42
+
43
+ def last_intermediate_file
44
+ return if @intermediate_files.empty?
45
+
46
+ @intermediate_files.last.rewind
47
+ @intermediate_files.last
48
+ end
49
+
50
+ def output_for_next_patch batch
51
+ return @output if batch.last?
52
+ new_intermediate_file
53
+ end
54
+
55
+ def new_intermediate_file
56
+ @intermediate_files << Tempfile.new('csv_patch')
57
+ @intermediate_files.last
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -9,7 +9,7 @@ module CsvPatch
9
9
  TEMPFILE_NAME = 'csv_patch'
10
10
 
11
11
  def initialize options
12
- @input, @output = options[:input], options[:output]
12
+ @input, @output = CSV.new(options[:input]), options[:output]
13
13
 
14
14
  @revision_result = Tempfile.new(TEMPFILE_NAME)
15
15
  @revision = Revision.new(options[:changes], @revision_result)
@@ -4,24 +4,22 @@ module CsvPatch
4
4
 
5
5
  class Revision < Operation
6
6
 
7
- EMPTY_LINE = "\n"
8
-
9
7
  def initialize changes, output_stream
10
8
  @output_stream = output_stream
11
9
  @changes = changes
12
10
 
13
- header_line(EMPTY_LINE)
11
+ header_line([])
14
12
  end
15
13
 
16
14
  def header_line line
17
15
  return unless line
18
16
 
19
- @columns = csv_values(line)
17
+ @columns = line
20
18
  mark_all_columns_empty
21
19
  end
22
20
 
23
21
  def replace_line line
24
- emit replacement_line_for csv_values(line)
22
+ emit replacement_line_for(line)
25
23
  end
26
24
 
27
25
  def add_new_lines
@@ -1,36 +1,44 @@
1
1
  require 'json'
2
+ require 'ostruct'
2
3
 
3
- class StreamBatch
4
+ module CsvPatch
4
5
 
5
- def initialize stream, batch_size
6
- @stream = stream
7
- @batch_size = batch_size
8
- end
6
+ class StreamBatch
9
7
 
10
- def each
11
- yield next_batch until stream_end?
12
- end
8
+ def initialize stream, batch_size
9
+ @stream = stream
10
+ @batch_size = batch_size
11
+ end
13
12
 
14
- private
13
+ def each
14
+ yield next_batch until stream_end?
15
+ end
15
16
 
16
- def next_batch
17
- batch = {}
17
+ private
18
18
 
19
- batch.merge!(next_change) until batch_full?(batch)
19
+ def next_batch
20
+ OpenStruct.new(changes: next_batch_of_changes, last?: stream_end?)
21
+ end
20
22
 
21
- batch
22
- end
23
+ def next_batch_of_changes
24
+ batch = {}
23
25
 
24
- def next_change
25
- JSON.parse(@stream.gets)
26
- end
26
+ batch.merge!(next_change) until batch_full?(batch)
27
27
 
28
- def batch_full? batch
29
- batch.size >= @batch_size || stream_end?
30
- end
28
+ batch
29
+ end
31
30
 
32
- def stream_end?
33
- @stream.eof?
34
- end
31
+ def next_change
32
+ JSON.parse(@stream.gets)
33
+ end
34
+
35
+ def batch_full? batch
36
+ batch.size >= @batch_size || stream_end?
37
+ end
35
38
 
39
+ def stream_end?
40
+ @stream.eof?
41
+ end
42
+
43
+ end
36
44
  end
data/lib/csv_patch.rb CHANGED
@@ -1,22 +1,9 @@
1
- require 'csv_patch/stream_batch'
2
- require 'csv_patch/patch'
1
+ require 'csv_patch/batches'
3
2
 
4
3
  module CsvPatch
5
4
 
6
- DEFAULT_BATCH_SIZE = 500
7
-
8
5
  def self.patch options
9
- batches(options).each { |changes| apply_patch(options, changes) }
10
- end
11
-
12
- private
13
-
14
- def self.batches options
15
- StreamBatch.new(options[:changes], options[:batch_size] || DEFAULT_BATCH_SIZE)
16
- end
17
-
18
- def self.apply_patch options, changes
19
- Patch.new(input: options[:input], output: options[:output], changes: changes).apply
6
+ Batches.new(options).execute
20
7
  end
21
8
 
22
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_patch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lyudmil
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-28 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jeweler
@@ -52,6 +52,7 @@ files:
52
52
  - bin/csv_patch
53
53
  - csv_patch.gemspec
54
54
  - lib/csv_patch.rb
55
+ - lib/csv_patch/batches.rb
55
56
  - lib/csv_patch/compression.rb
56
57
  - lib/csv_patch/operation.rb
57
58
  - lib/csv_patch/patch.rb