stuff_arc 0.0.4 → 0.0.6.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # stuff_arc - supports Rails portable data dump and restore
2
2
 
3
- ## Version 0.0.4
3
+ ## Version 0.0.6
4
4
 
5
5
  stuff_arc adds class level archiving and unarchiving of an ActiveRecord object
6
- a Rails app.
6
+ a Rails app by Auto-Extending ActiveRecord::Base with two methods: `archive` and
7
+ `unarchive`
7
8
 
8
9
  Archives are JSON data, one line per model instance with the 'id' value omitted so that
9
10
  the data can be restored w/o worrying about primary key clashes.
@@ -16,23 +17,16 @@ These methods are designed to be used in a Rails Console, not programatically.
16
17
 
17
18
  ## Install
18
19
 
19
- gem install stuff_arc
20
+ Add `gem stuff_arc` to your Gemfile & run `bundle`
20
21
 
22
+ All models (derived from ActiveRecord::Base) will now have `archive` and `unarchive`
23
+ class methods.
24
+
21
25
  ## Usage
22
26
 
23
- Include into models in which you want to create archives.
24
-
25
- app/models/foo.rb:
26
-
27
- require 'stuff_arc'
27
+ Assume:
28
28
 
29
29
  class Foo < ActiveRecord::Base
30
- include StuffArc
31
-
32
- . . .
33
- end
34
-
35
- This will add two class methods to Foo:
36
30
 
37
31
  * Foo.archive(options = {}) - which will create the file 'foos.json' containing JSON serializations
38
32
  of every record retrieved by Foo.all with the 'id' entry is excluded.
@@ -56,7 +50,7 @@ included.
56
50
 
57
51
  ## Creating an Archive
58
52
 
59
- Fire up the rails console [rails c] and . . .
53
+ Fire up the rails console [rails c &lt;development|production&gt;] and . . .
60
54
 
61
55
  Create an archive in the default directory with default name:
62
56
 
@@ -72,6 +66,6 @@ Create an archive in a specified directory with the default name
72
66
 
73
67
  ## Restoring an Archive
74
68
 
75
- Fire up the rails console [rails c] and . . .
69
+ Fire up the rails console [rails c &lt;development|production&gt;] and . . .
76
70
 
77
71
  repeat one of the commands above using **Foo.unarchive** instead of *Foo.archive*
data/Rakefile CHANGED
@@ -13,13 +13,9 @@ task :default => :test
13
13
 
14
14
  desc "Run unit tests"
15
15
  task :test do
16
- system 'ruby tests/*'
17
- # require "./test/#{gem_name}_base_test"
18
- end
19
-
20
- desc "run rdoc to create doc"
21
- task :doc do
22
- system 'rdoc'
16
+ system 'ruby test/*'
17
+ # puts "------requiring ./test/#{gem_name}_test"
18
+ # require "./test/#{gem_name}_test"
23
19
  end
24
20
 
25
21
  desc "build gem"
@@ -31,12 +27,7 @@ task :gem do
31
27
  end
32
28
  end
33
29
 
34
- desc "push to github"
35
- task :git_push do
36
- system 'git push'
37
- end
38
-
39
30
  desc "push to rubygems"
40
- task :gem_push => :gem do
31
+ task :push_gem => :gem do
41
32
  system "gem push #{gem_name}-#{gem_version}.gem"
42
33
  end
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+
3
+ module StuffArc
4
+ class Engine < Rails::Engine
5
+ if Rails.version =~ /^3.[01]/
6
+ initializer "active_record.add_stuff_arc" do
7
+ ActiveSupport.on_load(:active_record) do
8
+ extend StuffArc::Base
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,91 @@
1
+ # StuffArc - supports portable Rails databases dump and restore
2
+ #
3
+ # defines two class level methods for archiving and unarchiving ActiveRecord::Base
4
+ # objects.
5
+ #
6
+ # archive - creates an archive named <class.underscore>.json containing JSON representations
7
+ # of each instance of the model
8
+ #
9
+ # unarchive - which reads a file created by **archive** (named <class.underscore>.json) and
10
+ # saves each record in the datbase.
11
+ #
12
+ # Both methods are designed to be run from a Rails Console - not programatically.
13
+
14
+ require 'rails'
15
+ require 'active_model'
16
+ # require 'pry'
17
+
18
+ module StuffArc
19
+ VERSION = "0.0.6.pre1"
20
+ module Base
21
+ def archive options = {}
22
+ return self.class.archive options unless self.class == Class
23
+ mod_lowercase = self.to_s.underscore.pluralize
24
+
25
+ unless (lib_dir = options.delete(:lib_dir))
26
+ if Rails.public_path
27
+ lib_dir = File.join( File.dirname(::Rails.public_path), 'lib', 'stuff_arc' )
28
+ Dir.mkdir(lib_dir) unless File.exists? lib_dir
29
+ else
30
+ lib_dir = '.'
31
+ end
32
+ end
33
+ fname = options.delete(:fname) || "#{mod_lowercase}.json"
34
+ fname = File.join(lib_dir, fname) unless fname[0] == File::SEPARATOR
35
+
36
+ if File.exists? fname
37
+ back_name = fname + '~'
38
+ File.unlink back_name if File.exists? back_name
39
+ File.rename fname, back_name
40
+ end
41
+ f = File.open(fname, 'w')
42
+ list = self.all
43
+ list.each do |instance|
44
+ # as_json returns a hash, which we have to change to a JSON string
45
+ f.write instance.as_json.to_json + "\n"
46
+ end
47
+ f.close
48
+ list.length
49
+ end
50
+
51
+ def unarchive options = {}
52
+ return self.class.unarchive options unless self.class == Class
53
+ mod_lowercase = self.to_s.underscore.pluralize
54
+
55
+ unless (lib_dir = options.delete(:lib_dir))
56
+ if Rails.public_path
57
+ lib_dir = File.join( File.dirname(::Rails.public_path), 'lib', 'stuff_arc' )
58
+ Dir.mkdir(lib_dir) unless File.exists? lib_dir
59
+ else
60
+ lib_dir = '.'
61
+ end
62
+ end
63
+ fname = options.delete(:fname) || "#{mod_lowercase}.json"
64
+ fname = File.join(lib_dir, fname) unless fname[0] == File::SEPARATOR
65
+
66
+ return nil unless File.exists? fname
67
+
68
+ f = File.new fname
69
+
70
+ counter = 0
71
+ f.lines do |line|
72
+ tmp = self.new.from_json(line.chomp)
73
+ begin
74
+ tmp.save!
75
+ counter += 1
76
+ rescue Exception => e
77
+ puts "exception unarchiving #{self}: \#{e}\n"
78
+ end
79
+ end
80
+
81
+ f.close
82
+ counter
83
+ end
84
+
85
+ def self.included(mod)
86
+ # if I'm included, then I want to extend myself
87
+ puts "I'm being included in #{mod}!!!!"
88
+ mod.send :extend, self
89
+ end
90
+ end
91
+ end
data/lib/stuff_arc.rb CHANGED
@@ -11,77 +11,7 @@
11
11
  #
12
12
  # Both methods are designed to be run from a Rails Console - not programatically.
13
13
 
14
- require 'rails'
15
- require 'active_model'
16
- # require 'pry'
17
-
18
- module StuffArc
19
- VERSION = "0.0.4"
20
-
21
- def self.included(mod)
22
- mod_lowercase = mod.to_s.underscore.pluralize
23
- tmp =<<-EOF
24
- def self.archive options = {}
25
- unless (lib_dir = options.delete(:lib_dir))
26
- if Rails.public_path
27
- lib_dir = File.join( File.dirname(::Rails.public_path), 'lib', 'stuff_arc' )
28
- Dir.mkdir(lib_dir) unless File.exists? lib_dir
29
- else
30
- lib_dir = '.'
31
- end
32
- end
33
- fname = options.delete(:fname) || '#{mod_lowercase}.json'
34
- fname = File.join(lib_dir, fname) unless fname[0] == File::SEPARATOR
35
-
36
- if File.exists? fname
37
- back_name = fname + '~'
38
- File.unlink back_name if File.exists? back_name
39
- File.rename fname, back_name
40
- end
41
- f = File.open(fname, 'w')
42
- list = self.all
43
- list.each do |#{mod_lowercase}|
44
- # as_json returns a hash, which we have to change to a JSON string
45
- f.write #{mod_lowercase}.as_json.to_json + "\n"
46
- end
47
- f.close
48
- list.length
49
- end
50
- EOF
51
-
52
- mod.instance_eval tmp, __FILE__, __LINE__
53
-
54
- tmp =<<-EOF
55
- def self.unarchive options = {}
56
- unless (lib_dir = options.delete(:lib_dir))
57
- if Rails.public_path
58
- lib_dir = File.join( File.dirname(::Rails.public_path), 'lib', 'stuff_arc' )
59
- Dir.mkdir(lib_dir) unless File.exists? lib_dir
60
- else
61
- lib_dir = '.'
62
- end
63
- end
64
- fname = options.delete(:fname) || '#{mod_lowercase}.json'
65
- fname = File.join(lib_dir, fname) unless fname[0] == File::SEPARATOR
66
-
67
- return nil unless File.exists? fname
68
14
 
69
- f = File.new fname
70
-
71
- f.lines do |line|
72
- #{mod_lowercase} = #{mod}.new.from_json(line.chomp)
73
- begin
74
- #{mod_lowercase}.save!
75
- rescue Exception => e
76
- puts "exception unarchiving #{mod_lowercase}: \#{e}\n"
77
- end
78
- end
79
-
80
- f.close
81
- end
82
- EOF
83
-
84
- mod.instance_eval tmp, __FILE__, __LINE__
85
-
86
- end
87
- end
15
+ require 'rails'
16
+ require 'stuff_arc/engine'
17
+ require 'stuff_arc/stuff_arc'
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stuff_arc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.6.pre1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mike Howard
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-08 00:00:00.000000000Z
12
+ date: 2011-09-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2166421240 !ruby/object:Gem::Requirement
16
+ requirement: &2152971800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2166421240
24
+ version_requirements: *2152971800
25
25
  description: stuff_arc - adds class level archiving/unarchiving to ActiveRecord::Base
26
26
  children
27
27
  email: mike@clove.com
@@ -29,8 +29,9 @@ executables: []
29
29
  extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
+ - lib/stuff_arc/engine.rb
33
+ - lib/stuff_arc/stuff_arc.rb
32
34
  - lib/stuff_arc.rb
33
- - tests/stuff_arc_test.rb
34
35
  - LICENSE
35
36
  - Rakefile
36
37
  - README.md
@@ -50,9 +51,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
51
  required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  none: false
52
53
  requirements:
53
- - - ! '>='
54
+ - - ! '>'
54
55
  - !ruby/object:Gem::Version
55
- version: '0'
56
+ version: 1.3.1
56
57
  requirements: []
57
58
  rubyforge_project:
58
59
  rubygems_version: 1.8.6
@@ -1,99 +0,0 @@
1
- $LOAD_PATH << File.expand_path("../../lib", __FILE__)
2
- require 'test/unit'
3
- require 'active_model'
4
- require 'stuff_arc'
5
- # require 'active_model'
6
-
7
- class StuffArcHelper
8
- include StuffArc
9
- include ActiveModel::Serializers::JSON
10
-
11
- class <<self
12
- attr_accessor :db
13
-
14
- def init_db
15
- self.db = []
16
- end
17
- end
18
-
19
- attr_accessor :foo, :bar
20
-
21
- def initialize args = nil
22
- unless args.nil?
23
- self.foo = args[:foo] if args[:foo]
24
- self.bar = args[:bar] if args[:bar]
25
- end
26
- end
27
-
28
- def ==(other)
29
- self.foo == other.foo && self.bar == other.bar
30
- end
31
-
32
- def attributes
33
- {'foo' => @foo, 'bar' => @bar}
34
- end
35
-
36
- def attributes=(hash)
37
- hash.each do |k,v|
38
- self.instance_variable_set "@#{k}", v
39
- end
40
- end
41
-
42
- def self.all
43
- [self.new({foo: 'foo', bar: 'bar'}), self.new({foo: 'foo2', bar: 'bar2'})]
44
- end
45
-
46
- def save!
47
- StuffArcHelper.db << self
48
- end
49
- end
50
-
51
- class StuffArcTest < Test::Unit::TestCase
52
- # def setup
53
- # puts "StuffArcHelper.public_methods: #{StuffArcHelper.public_methods.grep /arc/}"
54
- # end
55
-
56
- # def teardown
57
- # fname = StuffArcHelper.to_s.underscore.pluralize + '.json'
58
- # [fname, fname + '~'].each do |fn|
59
- # File.unlink(fn) if File.exists? fn
60
- # end
61
- # end
62
-
63
- def test_methods_exist
64
- assert StuffArcHelper.respond_to?(:archive), "StuffArcHelper has class method :archive"
65
- assert StuffArcHelper.respond_to?(:unarchive), "StuffArcHelper has class method :unarchive"
66
- end
67
-
68
- def test_creates_archive
69
- StuffArcHelper.archive
70
- fname = StuffArcHelper.to_s.underscore
71
- assert_equal 'stuff_arc_helper', fname, "underscore should transform class name correctly"
72
- assert File.exists?('stuff_arc_helpers.json'), "archive creates a file"
73
- end
74
-
75
- def test_reads_archive
76
- StuffArcHelper.archive
77
- StuffArcHelper.init_db
78
- assert_equal [], StuffArcHelper.db, "StuffArcHelper.init_db should empty db"
79
- StuffArcHelper.unarchive
80
- assert_equal StuffArcHelper.all, StuffArcHelper.db, "Unarchiving should fill db"
81
- end
82
-
83
- def test_full_path_to_archive
84
- path = File.join(Dir.pwd, 'path-to-archive')
85
- Dir.mkdir(path) unless File.exists? path
86
- StuffArcHelper.archive :lib_dir => path
87
- assert File.exists?(File.join(path, 'stuff_arc_helpers.json')), "archive should be in #{path}"
88
- File.unlink File.join(path, 'stuff_arc_helpers.json') if File.exists? File.join(path, 'stuff_arc_helpers.json')
89
- File.unlink File.join(path, 'stuff_arc_helpers.json~') if File.exists? File.join(path, 'stuff_arc_helpers.json~')
90
- Dir.rmdir path if File.exists? path
91
- end
92
-
93
- def test_fname_override
94
- fname = 'foo-stuff'
95
- StuffArcHelper.archive :fname => fname
96
- assert File.exists?(fname), "file #{fname} should exist"
97
- File.unlink(fname) if File.exists? fname
98
- end
99
- end