orphanage 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.
- checksums.yaml +7 -0
- data/lib/orphanage.rb +98 -0
- metadata +112 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 836545ad30600d71a455f70709b18d2b4cc8be81
|
|
4
|
+
data.tar.gz: 8b6244756796620d588c8f2a7b81990c6d1215fa
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1d66fef97ce142481663940b661df8f68849d78296e93dc2e41dcbc1503e0e092321b3635a995c2b513a1ed91ddcb8d95f1f9c0743788bc4d9434d5bc9038145
|
|
7
|
+
data.tar.gz: 371eb0170d728e6485ef993b185da8404e11a42425241d2be587cde0103660deeed35001eab083ec38ce7b505149b4f8218ea53201b475157b4d54ebbfffec17
|
data/lib/orphanage.rb
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
module Orphanage
|
|
2
|
+
def self.included(klass)
|
|
3
|
+
klass.send(:include, Orphanage::Methods)
|
|
4
|
+
klass.send(:extend, Orphanage::ClassMethods)
|
|
5
|
+
|
|
6
|
+
end # self.included
|
|
7
|
+
|
|
8
|
+
module Methods
|
|
9
|
+
|
|
10
|
+
def adopt fks, options={}
|
|
11
|
+
# creates a new record in the home table. Returns the created record
|
|
12
|
+
# fks(hash) mapping of foreign keys to values.
|
|
13
|
+
# options(hash): optionally override adoption options set in class
|
|
14
|
+
|
|
15
|
+
default_options = self.class.adopt_options
|
|
16
|
+
merged_options = default_options.deep_merge options
|
|
17
|
+
dest = merged_options[:home] # the destination model
|
|
18
|
+
|
|
19
|
+
# columns allowed in the destination model
|
|
20
|
+
allowed_cols = dest.column_names
|
|
21
|
+
allowed_cols.delete "id" # obviously this shouldn't carry over
|
|
22
|
+
|
|
23
|
+
# timestamps that shouldn't be carried over in the adption
|
|
24
|
+
timestamps_to_remove = merged_options[:update_timestamps]
|
|
25
|
+
.select{|k, v| v}
|
|
26
|
+
.map{|k, v| "#{k.to_s}_at" }
|
|
27
|
+
|
|
28
|
+
allowed_cols = allowed_cols - timestamps_to_remove
|
|
29
|
+
|
|
30
|
+
record = dest.new self
|
|
31
|
+
.attributes
|
|
32
|
+
.select {|k, v| allowed_cols.include? k}
|
|
33
|
+
|
|
34
|
+
dest.transaction do
|
|
35
|
+
record.update_attributes!(fks)
|
|
36
|
+
self.destroy! if merged_options[:destroy_on_adopt]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
return record
|
|
40
|
+
|
|
41
|
+
end # adopt
|
|
42
|
+
|
|
43
|
+
end # methods
|
|
44
|
+
|
|
45
|
+
module ClassMethods
|
|
46
|
+
|
|
47
|
+
def orphan(options = {})
|
|
48
|
+
# declare a class to be an orphan record class
|
|
49
|
+
# home(string or symbol): lower case singular name of table the orphan
|
|
50
|
+
# class will be adopted into. Example :exam or "exam"
|
|
51
|
+
|
|
52
|
+
# fks(array of strings or symbolds) array of foreign key names the orphan will use to attempt
|
|
53
|
+
# to move into the home table.
|
|
54
|
+
|
|
55
|
+
# options (hash)
|
|
56
|
+
# destroy_on_adopt (bool): if the orphan record should be destroyed after successful
|
|
57
|
+
# adoption
|
|
58
|
+
# update_timestamps (hash)
|
|
59
|
+
# created: (bool) if created_at timestamp should be updated
|
|
60
|
+
# at adoption
|
|
61
|
+
# updated (bool): if updated_at timestamp should be updated
|
|
62
|
+
# at adoption
|
|
63
|
+
|
|
64
|
+
default_options = {
|
|
65
|
+
home: self.name.gsub('Temp', '').constantize,
|
|
66
|
+
destroy_on_adopt: false,
|
|
67
|
+
update_timestamps: {
|
|
68
|
+
created: true,
|
|
69
|
+
updated: true
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
merged_options = default_options.deep_merge options
|
|
74
|
+
|
|
75
|
+
# self.home_model = home
|
|
76
|
+
self.adopt_options = merged_options
|
|
77
|
+
|
|
78
|
+
end # orphan_of
|
|
79
|
+
|
|
80
|
+
# def home_model
|
|
81
|
+
# @@home_model
|
|
82
|
+
# end # parent
|
|
83
|
+
#
|
|
84
|
+
# def home_model= home
|
|
85
|
+
# @@home_model = home.to_s.titleize.constantize
|
|
86
|
+
# end
|
|
87
|
+
|
|
88
|
+
def adopt_options
|
|
89
|
+
@@adopt_options
|
|
90
|
+
end #adopt_options
|
|
91
|
+
|
|
92
|
+
def adopt_options= options
|
|
93
|
+
@@adopt_options = options
|
|
94
|
+
end # adopt_options=
|
|
95
|
+
|
|
96
|
+
end # class methods
|
|
97
|
+
|
|
98
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: orphanage
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jacob Stoebel
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-03-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activerecord
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '5.0'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 5.0.2
|
|
23
|
+
type: :development
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - "~>"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '5.0'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 5.0.2
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: rspec
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.5'
|
|
40
|
+
type: :development
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.5'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: sqlite3
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.3'
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 1.3.13
|
|
57
|
+
type: :development
|
|
58
|
+
prerelease: false
|
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - "~>"
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '1.3'
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: 1.3.13
|
|
67
|
+
- !ruby/object:Gem::Dependency
|
|
68
|
+
name: pry
|
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - "~>"
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: 0.10.4
|
|
74
|
+
type: :development
|
|
75
|
+
prerelease: false
|
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - "~>"
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: 0.10.4
|
|
81
|
+
description: A simple library for storing temporary orphan records.
|
|
82
|
+
email: jstoebel@gmail.com
|
|
83
|
+
executables: []
|
|
84
|
+
extensions: []
|
|
85
|
+
extra_rdoc_files: []
|
|
86
|
+
files:
|
|
87
|
+
- lib/orphanage.rb
|
|
88
|
+
homepage: https://github.com/jstoebel/orphanage
|
|
89
|
+
licenses:
|
|
90
|
+
- MIT
|
|
91
|
+
metadata: {}
|
|
92
|
+
post_install_message:
|
|
93
|
+
rdoc_options: []
|
|
94
|
+
require_paths:
|
|
95
|
+
- lib
|
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
|
+
requirements:
|
|
103
|
+
- - ">="
|
|
104
|
+
- !ruby/object:Gem::Version
|
|
105
|
+
version: '0'
|
|
106
|
+
requirements: []
|
|
107
|
+
rubyforge_project:
|
|
108
|
+
rubygems_version: 2.5.1
|
|
109
|
+
signing_key:
|
|
110
|
+
specification_version: 4
|
|
111
|
+
summary: A simple library for storing temporary orphan records.
|
|
112
|
+
test_files: []
|