active_model_archive 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,4 @@
1
1
  *.gem
2
2
  .bundle
3
3
  Gemfile.lock
4
- pkg/*
4
+ tmp/
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "active_model_archive"
5
- s.version = '0.9.3'
5
+ s.version = '0.9.4'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Grant Rodgers"]
8
8
  s.email = ["grantr@gmail.com"]
@@ -5,23 +5,22 @@ module ActiveModelArchive
5
5
  module ClassMethods
6
6
  def dump!(filename, options = {})
7
7
  query = options[:query]
8
- per_file = options[:per_file]
8
+ per_file = options[:per_file] ? options[:per_file].to_i : nil
9
9
 
10
- file_manager = FileManager.new(filename, per_file)
11
-
12
- if query.blank?
13
- find_each do |object|
14
- yield(object) if block_given?
15
- file_manager.add(object)
16
- end
17
- else
18
- paginated_each(query) do |object|
19
- yield(object) if block_given?
20
- file_manager.add(object)
10
+ file_manager = FileManager.new(filename, per_file) do |file_manager|
11
+ if query.blank?
12
+ find_each do |object|
13
+ yield(object) if block_given?
14
+ file_manager.add(object)
15
+ end
16
+ else
17
+ paginated_each(query) do |object|
18
+ yield(object) if block_given?
19
+ file_manager.add(object)
20
+ end
21
21
  end
22
22
  end
23
23
 
24
- file_manager.flush
25
24
  file_manager.item_count
26
25
  end
27
26
  end
@@ -2,11 +2,14 @@ module ActiveModelArchive
2
2
  class FileManager
3
3
  UNLIMITED_PER_FILE = 999999999
4
4
  attr_accessor :item_count, :file_number, :base_filename, :items_per_file
5
- def initialize(base_filename, items_per_file)
5
+ def initialize(base_filename, items_per_file = nil)
6
6
  @item_count = 0
7
7
  @file_number = 0
8
8
  @base_filename = base_filename
9
- @items_per_file = (items_per_file.presence || UNLIMITED_PER_FILE).to_i
9
+ @items_per_file = items_per_file || UNLIMITED_PER_FILE
10
+
11
+ yield(self)
12
+ flush
10
13
  end
11
14
 
12
15
  def add(object)
@@ -25,7 +28,7 @@ module ActiveModelArchive
25
28
 
26
29
  def create_file
27
30
  self.file_number += 1
28
-
31
+
29
32
  filename = items_per_file == UNLIMITED_PER_FILE ? base_filename : "#{base_filename}.#{file_number}"
30
33
  file = File.open(filename, "w")
31
34
 
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveModelArchive::FileManagerTest < MiniTest::Spec
4
+ def setup
5
+ FileUtils.mkpath('tmp')
6
+ end
7
+
8
+ def teardown
9
+ FileUtils.rm_rf('tmp')
10
+ end
11
+
12
+ def test_add
13
+ object = TestModel.new(id: 'foo', color: 'red')
14
+
15
+ file_manager = ActiveModelArchive::FileManager.new('tmp/wtf') do |file_manager|
16
+ file_manager.add(object)
17
+ end
18
+
19
+ assert_equal file_manager.encoder.encode(object.as_archive), File.read('tmp/wtf')
20
+ end
21
+
22
+ def test_items_per_file
23
+ file_manager = ActiveModelArchive::FileManager.new('tmp/wtf', 2) do |file_manager|
24
+ file_manager.add(TestModel.new)
25
+ file_manager.add(TestModel.new)
26
+ file_manager.add(TestModel.new)
27
+ end
28
+
29
+ assert_equal ["tmp/wtf.1", "tmp/wtf.2"], Dir.glob('tmp/wtf*')
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-09 00:00:00.000000000 Z
12
+ date: 2011-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70119318295040 !ruby/object:Gem::Requirement
16
+ requirement: &70165958779720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70119318295040
24
+ version_requirements: *70165958779720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &70119318293880 !ruby/object:Gem::Requirement
27
+ requirement: &70165958779240 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70119318293880
35
+ version_requirements: *70165958779240
36
36
  description: Adds dump and restore functionality to activemodel classes
37
37
  email:
38
38
  - grantr@gmail.com
@@ -50,6 +50,7 @@ files:
50
50
  - lib/active_model_archive/file_manager.rb
51
51
  - lib/active_model_archive/restore.rb
52
52
  - test/dump_test.rb
53
+ - test/file_manager_test.rb
53
54
  - test/restore_test.rb
54
55
  - test/test_helper.rb
55
56
  homepage: ''
@@ -78,5 +79,6 @@ specification_version: 3
78
79
  summary: Dump and restore activemodel objects
79
80
  test_files:
80
81
  - test/dump_test.rb
82
+ - test/file_manager_test.rb
81
83
  - test/restore_test.rb
82
84
  - test/test_helper.rb