acts_as_temporary 0.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Daniel Reedy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # ActsAsTemporary
2
+
3
+ On occasion I've had the need to store data for a short period of time within an application's database but did not want it to artificially inflate the ID numbers.
4
+ This gem uses a _TemporaryObject_ model that stores the "definition" (read: attributes) of a model. It can then be recalled at a later date. Upon saving a recalled object the temporary version is deleted.
5
+
6
+ ## Requirements
7
+
8
+ This project builds upon ActiveRecord. In theory this should work with nothing outside of a Rails application
9
+
10
+ ## Installation
11
+
12
+ This project is distributed as a gem and it should be as simple as adding the following line to your **Gemfile**.
13
+
14
+ gem 'acts_as_temporary', '~> 0.0.1'
15
+
16
+ You'll need to copy and run the migration from the engine.
17
+
18
+ $ cd _/your/rails/app_
19
+ $ rake acts_as_temporary_engine:install:migrations
20
+ $ rake db:migrate
21
+
22
+ ## Configuration
23
+
24
+ In your _{environment}.rb_ file you can set the shelf life of a temporary object with the following configuration definition:
25
+
26
+ config.acts_as_temporary_shelf_life = 1.day # Set the duration to something that makes sense
27
+
28
+ By default the shelf life of a temporary object is _365.days_.
29
+
30
+ # Usage
31
+
32
+ ## Class Methods
33
+
34
+ ### can\_be\_temporary
35
+
36
+ Within the model you would like to be able to temporarily store you just need to add the following line.
37
+
38
+ can_be_temporary
39
+
40
+ For example, if you wish to make the _Registration_ model one that can be temporary the model would look like this to start.
41
+
42
+ class Registration < ActiveRecord::Base
43
+ can_be_temporary
44
+ end
45
+
46
+ ### clear\_stale\_objects
47
+
48
+ Rolls through the temporary objects table and clears any old temporary objects. By default anything older than 24 hours is considered stale.
49
+
50
+ ## Instance Methods
51
+
52
+ ### #store
53
+
54
+ The _#store_ instance method takes the instance and stores it as a temporary object. The temporary object's ID is then stored within the objects _@temporary\_id_ instance variable.
55
+
56
+ ### #recall
57
+
58
+ The _#recall_ instance method takes the ID of a temporary object and attempts to instantiate that data.
59
+
60
+ ### #is_temporary?
61
+
62
+ Returns __true__ if the current object has a _@temporary\_id_.
63
+
64
+ ## Deleting Temporary Objects
65
+
66
+ There are two methods for deleting temporary objects. The first, and most common, is to simply call _save_ and the second is to call _drop\_temporary_ on an object that has been recalled.
67
+
68
+ ### #save
69
+
70
+ If the object saves without error the temporary object will be destroyed.
71
+
72
+ ### #drop\_temporary
73
+
74
+ Deletes the associated temporary object from the database without saving the calling object
75
+
76
+ ### Example
77
+
78
+ registration = Registration.new(params[:registration])
79
+ registration.is_temporary? # => false
80
+
81
+ registration.store # => this object's data is stored in the database and the ID of the temporary object is interally assigned
82
+ registration.id # => nil
83
+ registration.temporary_id # => 14123
84
+ registration.is_temporary? # => true
85
+
86
+ registration = Registration.recall(14123)
87
+ registration.id # => nil
88
+ registration.is_temporary? # => true
89
+
90
+ registration.save # => true
91
+ registration.is_temporary? # => false
92
+
93
+
94
+ This project uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ActsAsTemporary'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
@@ -0,0 +1,5 @@
1
+ class TemporaryObject < ActiveRecord::Base
2
+ validates_presence_of :permanent_class
3
+ validates_presence_of :definition
4
+ serialize :definition
5
+ end
@@ -0,0 +1,10 @@
1
+ class CreateTemporaryObjects < ActiveRecord::Migration
2
+ def change
3
+ create_table :temporary_objects do |t|
4
+ t.string :permanent_class
5
+ t.text :definition
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,61 @@
1
+ module ActsAsTemporary
2
+ def can_be_temporary
3
+ #has_many :reviews, :as=>:reviewable, :dependent=>:destroy
4
+ include InstanceMethods
5
+ attr_reader :temporary_id
6
+ after_save :drop_temporary
7
+
8
+ def recall(temp_id=nil)
9
+ raise ArgumentError, "Please provide a temporary object ID" if temp_id.blank?
10
+ temporary_object = TemporaryObject.find(temp_id)
11
+ if temporary_object
12
+ raise TypeError, "Temporary object and calling object must be the same class" unless temporary_object.permanent_class.eql?(self.name)
13
+ new_object = self.new(temporary_object.definition)
14
+ new_object.instance_variable_set("@temporary_id",temp_id)
15
+ new_object
16
+ end
17
+ end
18
+
19
+ def clear_stale_objects
20
+ expiry_date = Time.now - Rails.application.config.acts_as_temporary_shelf_life
21
+ TemporaryObject.delete_all(["permanent_class = ? AND created_at < ?", self.name, expiry_date])
22
+ end
23
+ end
24
+ module InstanceMethods
25
+ def can_be_temporary?
26
+ true
27
+ end
28
+
29
+ def shelf_life
30
+ Rails.application.config.acts_as_temporary_shelf_life
31
+ end
32
+
33
+ def store
34
+ temporary_object = TemporaryObject.new
35
+ temporary_object.permanent_class = self.class.name
36
+ temporary_object.definition = self.attributes
37
+ temporary_object.save
38
+ @temporary_id = temporary_object.id
39
+ end
40
+
41
+ def recall(temp_id=nil)
42
+ temp_id ||= @temporary_id
43
+ self.class.send(:recall, temp_id)
44
+ end
45
+
46
+ def is_temporary?
47
+ @temporary_id.present?
48
+ end
49
+
50
+ def drop_temporary
51
+ if @temporary_id.present?
52
+ TemporaryObject.destroy(@temporary_id)
53
+ @temporary_id = nil
54
+ end
55
+ end
56
+
57
+
58
+ end
59
+ end
60
+
61
+ ActiveRecord::Base.extend ActsAsTemporary
@@ -0,0 +1,10 @@
1
+ require 'acts_as_temporary'
2
+ require 'rails'
3
+
4
+ module ActsAsTemporary
5
+ class Engine < Rails::Engine
6
+ config.before_initialize do |app|
7
+ app.config.acts_as_temporary_shelf_life = 365.days
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsTemporary
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "acts_as_temporary/acts_as_temporary"
2
+ require "acts_as_temporary/engine"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_temporary do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_temporary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Reedy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70293734107740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70293734107740
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70293734107240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70293734107240
36
+ - !ruby/object:Gem::Dependency
37
+ name: steak
38
+ requirement: &70293734106660 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70293734106660
47
+ description: In the event that you do not want to clutter your production databases
48
+ with what could be temporary data you can use this plugin to temporarily store and
49
+ later retreive data with your database.
50
+ email:
51
+ - danreedy@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - app/models/temporary_object.rb
57
+ - db/migrate/20110919190814_create_temporary_objects.rb
58
+ - lib/acts_as_temporary/acts_as_temporary.rb
59
+ - lib/acts_as_temporary/engine.rb
60
+ - lib/acts_as_temporary/version.rb
61
+ - lib/acts_as_temporary.rb
62
+ - lib/tasks/acts_as_temporary_tasks.rake
63
+ - MIT-LICENSE
64
+ - Rakefile
65
+ - README.md
66
+ homepage: http://about.me/reedy
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.6
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Adds the abililty to temporarily store an object through ActiveRecord
90
+ test_files: []