active_model_archive 0.9.6 → 1.0.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.
@@ -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.6'
5
+ s.version = '1.0.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Grant Rodgers"]
8
8
  s.email = ["grantr@gmail.com"]
@@ -0,0 +1,28 @@
1
+ require 'csv'
2
+
3
+ module ActiveModelArchive
4
+ module Archiver
5
+ class Csv
6
+ def header(object)
7
+ object.archive_attributes.to_csv
8
+ end
9
+
10
+ def encode(object)
11
+ object.as_archive.values_at(*object.archive_attributes).to_csv
12
+ end
13
+
14
+ def parse(io)
15
+ first_line = true
16
+ header = []
17
+ io.each do |line|
18
+ if first_line
19
+ first_line = false
20
+ header = line.parse_csv
21
+ else
22
+ yield Hash[header.zip(line.parse_csv)]
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveModelArchive
2
+ module Archiver
3
+ class Json
4
+ def header(object)
5
+ end
6
+
7
+ def encode(object)
8
+ (@encoder ||= Yajl::Encoder.new).encode(object.as_archive) + "\n"
9
+ end
10
+
11
+ def parse(io, &block)
12
+ (@parser ||= Yajl::Parser.new).parse(io, &block)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveModelArchive
2
+ module Attributes
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def archive_attributes
7
+ @archive_attributes ||= (new.as_archive.keys)
8
+ end
9
+ end
10
+
11
+ def archive_attributes
12
+ self.class.archive_attributes
13
+ end
14
+
15
+ def as_archive
16
+ attributes.dup.update('id' => id)
17
+ end
18
+
19
+ def to_archive
20
+ archiver.encode(self).tap do |encoded|
21
+ encoded.force_encoding('UTF-8')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -24,9 +24,5 @@ module ActiveModelArchive
24
24
  file_manager.item_count
25
25
  end
26
26
  end
27
-
28
- def as_archive
29
- attributes.dup.update('id' => id)
30
- end
31
27
  end
32
28
  end
@@ -20,10 +20,7 @@ module ActiveModelArchive
20
20
 
21
21
  self.item_count += 1
22
22
 
23
- encoder.encode(object.as_archive, nil) do |json|
24
- json.force_encoding('UTF-8')
25
- @current_file.puts(json)
26
- end
23
+ @current_file.write(object.to_archive)
27
24
  end
28
25
 
29
26
  def create_file
@@ -40,9 +37,5 @@ module ActiveModelArchive
40
37
  @current_file.close
41
38
  end
42
39
  end
43
-
44
- def encoder
45
- @encoder ||= Yajl::Encoder.new
46
- end
47
40
  end
48
41
  end
@@ -13,7 +13,7 @@ module ActiveModelArchive
13
13
  def each_instance(io)
14
14
  each_archive(io) do |hash|
15
15
  attributes = {}
16
- valid_archive_keys.each { |key| attributes[key] = hash[key] }
16
+ archive_attributes.each { |key| attributes[key] = hash[key] }
17
17
  yield new(attributes)
18
18
  end
19
19
  end
@@ -26,19 +26,14 @@ module ActiveModelArchive
26
26
 
27
27
  def each_archive(io)
28
28
  count = 0
29
- parser = Yajl::Parser.new
30
29
 
31
- parser.parse(io) do |hash|
30
+ archiver.parse(io) do |hash|
32
31
  count += 1
33
32
  yield hash
34
33
  end
35
34
 
36
35
  count
37
36
  end
38
-
39
- def valid_archive_keys
40
- @valid_archive_keys ||= (new.attributes.keys + ['id'])
41
- end
42
37
  end
43
38
  end
44
39
  end
@@ -1,13 +1,24 @@
1
1
  require 'active_support/dependencies/autoload'
2
2
  require 'active_support/concern'
3
+ require 'active_support/core_ext/class/attribute'
4
+ require 'active_support/inflector'
5
+
3
6
  require 'yajl'
4
7
 
5
8
  module ActiveModelArchive
6
9
  extend ActiveSupport::Autoload
7
10
 
11
+ autoload :Attributes
8
12
  autoload :FileManager
9
13
  autoload :Dump
10
14
  autoload :Restore
15
+
16
+ module Archiver
17
+ extend ActiveSupport::Autoload
18
+
19
+ autoload :Csv
20
+ autoload :Json
21
+ end
11
22
  end
12
23
 
13
24
  module ActiveModel
@@ -15,8 +26,20 @@ module ActiveModel
15
26
  extend ActiveSupport::Concern
16
27
 
17
28
  included do
29
+ class_attribute :archiver
30
+ self.archiver_type = :json
31
+
32
+ include ActiveModelArchive::Attributes
18
33
  include ActiveModelArchive::Dump
19
34
  include ActiveModelArchive::Restore
20
35
  end
36
+
37
+ module ClassMethods
38
+ # Widget.archiver_type = :json
39
+ # Widget.archiver_type = :csv
40
+ def archiver_type=(type)
41
+ self.archiver = "ActiveModelArchive::Archiver::#{type.to_s.camelize}".constantize.new
42
+ end
43
+ end
21
44
  end
22
45
  end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveModelArchive::Archiver::CsvTest < MiniTest::Spec
4
+ def test_header
5
+ model = TestModel.new
6
+ assert_equal "color,id\n", archiver.header(model)
7
+ end
8
+
9
+ def test_encode
10
+ model = TestModel.new(id: 5, color: 'blue')
11
+ assert_equal "blue,5\n", archiver.encode(model)
12
+ end
13
+
14
+ def test_parse
15
+ io = StringIO.new(
16
+ "id,color\n" +
17
+ "3,red\n" +
18
+ "4,green\n"
19
+ )
20
+
21
+ results = []
22
+
23
+ archiver.parse(io) do |attributes|
24
+ results << attributes
25
+ end
26
+
27
+ expected = [
28
+ {'id' => '3', 'color' => 'red'},
29
+ {'id' => '4', 'color' => 'green'}
30
+ ]
31
+ assert_equal expected, results
32
+ end
33
+
34
+ private
35
+ def archiver
36
+ @archiver ||= ActiveModelArchive::Archiver::Csv.new
37
+ end
38
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveModelArchive::AttributesTest < MiniTest::Spec
4
+ def test_as_archive
5
+ model = TestModel.new(id: 5, color: 'blue')
6
+ assert_equal(
7
+ {'id' => 5, 'color' => 'blue'},
8
+ model.as_archive
9
+ )
10
+ end
11
+
12
+ def test_to_archive
13
+ model = TestModel.new(id: 5, color: 'blue')
14
+ assert_equal(
15
+ model.class.archiver.encode(model),
16
+ model.to_archive
17
+ )
18
+ end
19
+ end
data/test/dump_test.rb CHANGED
@@ -1,11 +1,5 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ActiveModelArchive::DumpTest < MiniTest::Spec
4
- def test_as_archive
5
- model = TestModel.new(id: 5, color: 'blue')
6
- assert_equal(
7
- {'id' => 5, 'color' => 'blue'},
8
- model.as_archive
9
- )
10
- end
4
+
11
5
  end
@@ -16,7 +16,7 @@ class ActiveModelArchive::FileManagerTest < MiniTest::Spec
16
16
  file_manager.add(object)
17
17
  end
18
18
 
19
- assert_equal file_manager.encoder.encode(object.as_archive), File.read('tmp/wtf')
19
+ assert_equal object.to_archive, File.read('tmp/wtf')
20
20
  end
21
21
 
22
22
  def test_items_per_file
data/test/restore_test.rb CHANGED
@@ -68,13 +68,6 @@ class ActiveModelArchive::RestoreTest < MiniTest::Spec
68
68
  assert_equal 'green', results.first.color
69
69
  end
70
70
 
71
- def test_valid_archive_keys
72
- assert_equal(
73
- ['color', 'id'],
74
- TestModel.valid_archive_keys
75
- )
76
- end
77
-
78
71
  def test_restore!
79
72
 
80
73
  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.6
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000 Z
12
+ date: 2012-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -56,9 +56,14 @@ files:
56
56
  - Rakefile
57
57
  - active_model_archive.gemspec
58
58
  - lib/active_model_archive.rb
59
+ - lib/active_model_archive/archiver/csv.rb
60
+ - lib/active_model_archive/archiver/json.rb
61
+ - lib/active_model_archive/attributes.rb
59
62
  - lib/active_model_archive/dump.rb
60
63
  - lib/active_model_archive/file_manager.rb
61
64
  - lib/active_model_archive/restore.rb
65
+ - test/archiver/csv_test.rb
66
+ - test/attributes_test.rb
62
67
  - test/dump_test.rb
63
68
  - test/file_manager_test.rb
64
69
  - test/restore_test.rb
@@ -88,6 +93,8 @@ signing_key:
88
93
  specification_version: 3
89
94
  summary: Dump and restore activemodel objects
90
95
  test_files:
96
+ - test/archiver/csv_test.rb
97
+ - test/attributes_test.rb
91
98
  - test/dump_test.rb
92
99
  - test/file_manager_test.rb
93
100
  - test/restore_test.rb