persisted 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/persisted.rb +71 -0
- data/lib/persisted/record_creator.rb +17 -0
- data/lib/persisted/record_finder.rb +17 -0
- data/lib/persisted/version.rb +3 -0
- data/persisted.gemspec +22 -0
- metadata +88 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use ruby-1.9.3-p194@persisted > /dev/null
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Derek Lindahl
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Persisted
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'persisted'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install persisted
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/persisted.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
require 'persisted/version'
|
4
|
+
require 'persisted/record_creator'
|
5
|
+
require 'persisted/record_finder'
|
6
|
+
|
7
|
+
# Adds persistence to a Ruby Object
|
8
|
+
module Persisted
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
include RecordFinder
|
12
|
+
include RecordCreator
|
13
|
+
|
14
|
+
def persisted_record=( record )
|
15
|
+
@persisted_record = record
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing( method_name, *args )
|
19
|
+
if persisted_record.respond_to? method_name
|
20
|
+
persisted_record.send method_name, *args
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def respond_to_missing?( method_name, include_private = false )
|
27
|
+
super || persisted_record.respond_to?( method_name )
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
attr_accessor :persisted_record_class
|
32
|
+
|
33
|
+
# Instructs the persistence layer which ORM-backed class to use to persist
|
34
|
+
# the model.
|
35
|
+
#
|
36
|
+
# Usage:
|
37
|
+
# persist_with :user, attributes: %w{username password}
|
38
|
+
#
|
39
|
+
# Configuration options:
|
40
|
+
# attributes: An array of shared attributes between models used for DB
|
41
|
+
# interactions. Basically, use this to find the appropriate record.
|
42
|
+
# Defaults to ['id']
|
43
|
+
def persist_with( model_name, options = {} )
|
44
|
+
self.persisted_record_class = model_name.to_s.camelize.constantize
|
45
|
+
|
46
|
+
if options && options.is_a?( Hash )
|
47
|
+
options.symbolize_keys!
|
48
|
+
|
49
|
+
if options[:attributes].is_a?( Array )
|
50
|
+
options[:attributes] ||= ['id']
|
51
|
+
|
52
|
+
@shared_attributes = options[:attributes]
|
53
|
+
end
|
54
|
+
else
|
55
|
+
raise ArgumentError, 'An array of shared attributes must be supplied as the :attributes option of the configuration hash'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def method_missing( method_name, *args )
|
60
|
+
if respond_to_missing? method_name
|
61
|
+
persisted_record_class.send method_name, *args
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def respond_to_missing?( method_name, include_private = false )
|
68
|
+
persisted_record_class.respond_to? method_name
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Persisted
|
2
|
+
module RecordCreator
|
3
|
+
def persisted_record
|
4
|
+
@persisted_record ||= super || create_record
|
5
|
+
end
|
6
|
+
|
7
|
+
def attributes_for_creation
|
8
|
+
attrs = self.class.persisted_record_class.accessible_attributes - [''] # No idea where there is a blank string in there...
|
9
|
+
|
10
|
+
attrs.each_with_object({}) { |k, a| a[k] = @params[k] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_record
|
14
|
+
self.class.persisted_record_class.create attributes_for_creation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Persisted
|
2
|
+
module RecordFinder
|
3
|
+
def persisted_record
|
4
|
+
@persisted_record ||= existing_record
|
5
|
+
end
|
6
|
+
|
7
|
+
def attributes_for_finding
|
8
|
+
shared_attrs = self.class.instance_variable_get '@shared_attributes'
|
9
|
+
|
10
|
+
shared_attrs.each_with_object({}){ |k, a| a[k] = @params[k] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def existing_record
|
14
|
+
self.class.send(:persisted_record_class).where( attributes_for_finding ).first
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/persisted.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'persisted/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'persisted'
|
8
|
+
gem.version = Persisted::VERSION
|
9
|
+
gem.authors = ['Derek Lindahl']
|
10
|
+
gem.email = ['dlindahl@customink.com']
|
11
|
+
gem.description = 'Persists Ruby Objects with an ActiveRecord model'
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = 'https://github.com/dlindahl/persisted'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_dependency 'activesupport', '~> 3.2'
|
21
|
+
gem.add_dependency 'activerecord', '~> 3.2'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: persisted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Derek Lindahl
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
description: Persists Ruby Objects with an ActiveRecord model
|
47
|
+
email:
|
48
|
+
- dlindahl@customink.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rvmrc
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/persisted.rb
|
60
|
+
- lib/persisted/record_creator.rb
|
61
|
+
- lib/persisted/record_finder.rb
|
62
|
+
- lib/persisted/version.rb
|
63
|
+
- persisted.gemspec
|
64
|
+
homepage: https://github.com/dlindahl/persisted
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Persists Ruby Objects with an ActiveRecord model
|
88
|
+
test_files: []
|