gardener 1.1.2 → 1.1.3
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/lib/gardener.rb +164 -144
- metadata +4 -4
data/lib/gardener.rb
CHANGED
@@ -1,154 +1,174 @@
|
|
1
1
|
module Gardener
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
2
|
+
|
3
|
+
yaml_start = /^---/
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
attr_accessor :infinite_choice
|
12
|
+
|
13
|
+
|
14
|
+
# Dump every instance of a class (every record in the database for a model) into a file
|
15
|
+
# to be reconstituted later.
|
16
|
+
def sow
|
17
|
+
c = self.to_s.underscore.pluralize
|
18
|
+
dir = File.join [Rails.root, 'db', 'garden']
|
19
|
+
Dir.mkdir(dir) unless File.exists?(dir)
|
20
|
+
|
21
|
+
File.open(File.join([Rails.root, 'db', 'garden', "#{c}.yml"] ), 'w') do |file|
|
22
|
+
self.find_each{ |x| file << x.to_yaml }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def garden_path
|
27
|
+
File.join([Rails.root, 'db', 'garden', "#{self.to_s.underscore.pluralize}.yml"] )
|
28
|
+
end
|
29
|
+
|
30
|
+
# Loads each yaml representation of an object up into an array, so you can play with them individually.
|
31
|
+
def secret_garden
|
32
|
+
instance_eval "attr_accessor :abra"
|
33
|
+
@abra = []
|
34
|
+
str = ''
|
35
|
+
File.open(garden_path, 'r').each_line do |line|
|
36
|
+
if line.match yaml_start
|
37
|
+
@abra << str
|
38
|
+
str = ''
|
39
|
+
end
|
40
|
+
str << line
|
41
|
+
end
|
42
|
+
@abra << str unless str.blank?
|
43
|
+
end
|
44
|
+
|
45
|
+
# Take the things that were dumped to a file and put them in the database.
|
46
|
+
def reap
|
47
|
+
str = ''
|
48
|
+
File.open(garden_path, 'r').each_line do |line|
|
49
|
+
if line.match yaml_start
|
50
|
+
sprout(str)
|
51
|
+
str = ''
|
52
|
+
end
|
53
|
+
str << line
|
54
|
+
end
|
55
|
+
sprout(str) unless str.blank?
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
# Helper function to reconstitute a record from it's yaml representation.
|
60
|
+
def sprout(str)
|
61
|
+
return false if str.blank?
|
62
|
+
# puts str.inspect
|
63
|
+
|
64
|
+
foo = YAML.load(str)
|
65
|
+
return unless foo # YAML.load('') == false
|
66
|
+
bob = self.new(foo.attributes)
|
67
|
+
bob.id = foo.id
|
68
|
+
|
69
|
+
# Grr, have to check to see if anything in the inheritance hierarchy has the id.
|
70
|
+
base_class = self.base_class
|
71
|
+
scott = base_class.find_by_id(bob.id)
|
72
|
+
|
73
|
+
if scott
|
74
|
+
puts "\n\n#{bob.class.name} with id #{bob.id} already exists, differences are"
|
75
|
+
bar = scott.diff(bob)
|
76
|
+
if bar.blank?
|
77
|
+
puts "\tActually, there are no differences, even the ids are the same #{bob.id} == #{scott.id}.\n\tLooks like you're trying to bring in the exact same object that already exists in the db."
|
78
|
+
else
|
79
|
+
pp bar
|
80
|
+
end
|
81
|
+
|
82
|
+
if @infinite_choice
|
83
|
+
res = @infinite_choice
|
84
|
+
else
|
85
|
+
print "\n\nAdd a 'z' to your selection to apply it to everything\nOverwrite (y), do nothing (n), give a new ID (i), or abort (a) ? "
|
86
|
+
res = gets
|
87
|
+
@infinite_choice = res if res.match(/z/i)
|
88
|
+
end
|
89
|
+
|
90
|
+
if res.match(/y/i)
|
91
|
+
scott = bob
|
92
|
+
scott.save
|
93
|
+
elsif res.match(/a/i)
|
94
|
+
raise "Salting the earth. Nothing will grow here for some time ..."
|
95
|
+
elsif res.match(/i/i)
|
96
|
+
bob.id = nil
|
97
|
+
bob.save
|
98
|
+
else
|
99
|
+
puts "#{bob.class.name} with id #{bob.id} already exists, doing nothing."
|
100
|
+
end
|
101
|
+
else
|
102
|
+
bob.save
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end # End ClassMethods
|
87
107
|
|
88
108
|
|
89
109
|
|
90
110
|
|
91
111
|
##### Instance Methods #####
|
92
112
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
113
|
+
def diff(obj)
|
114
|
+
buff = {}
|
115
|
+
self.attributes.each do |k, v|
|
116
|
+
tmp = obj.send(k) if obj.respond_to?(k)
|
117
|
+
if tmp.eql?(v)
|
118
|
+
# they're the same, do nothing
|
119
|
+
else
|
120
|
+
buff[k] = {:original => v, :new => tmp}
|
121
|
+
end
|
122
|
+
end # end self.attributes.each
|
123
|
+
buff
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
# Called on an particular instance of a class to render it down into a file, from which it can be
|
128
|
+
# _reaped_ later.
|
129
|
+
def plant_seed(options = {})
|
130
|
+
c = self.class.to_s.underscore.pluralize
|
131
|
+
# Code to render down associated items exists, but nothing has been done to pull those objects back out.
|
132
|
+
# Not automatically at least, but of course reaping the correct model will pull them up no problem.
|
133
|
+
|
134
|
+
|
135
|
+
# TODO
|
136
|
+
# need to load the file up and see if the thing i'm trying to add is in it
|
137
|
+
# or just overwrite the whole file every time.
|
138
|
+
# Do something to prevent duplicates from slipping in.
|
139
|
+
dir = File.join [Rails.root, 'db', 'garden']
|
140
|
+
Dir.mkdir(dir) unless File.exists?(dir)
|
141
|
+
|
142
|
+
|
143
|
+
File.open(File.join([Rails.root, 'db', 'garden', "#{c}.yml"] ), 'a') do |file|
|
144
|
+
file << self.to_yaml
|
145
|
+
end
|
146
|
+
|
147
|
+
includes = things_to_include(options)
|
148
|
+
# puts includes
|
149
|
+
includes.each do |k, v|
|
150
|
+
tom = self.send(k) if self.respond_to?(k)
|
151
|
+
[*tom].each{|y| y.plant_seed({:include => v})}
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
def things_to_include(options)
|
158
|
+
# [*THING] is so that THING can be an array or a single instance of a class.
|
159
|
+
# .each will blow up if THING is just an instance, not an array, unless we explode and re-array it.
|
160
|
+
return [] unless options[:include]
|
161
|
+
case options[:include]
|
162
|
+
when Hash
|
163
|
+
options[:include].inject({}){|memo, v| memo[v[0]] = v[1] if self.respond_to?(v[0]); memo}
|
164
|
+
else
|
165
|
+
[*options[:include]]
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def to_fixture
|
170
|
+
{"#{self.class.to_s.underscore}_#{self.id}" => self.attributes}.to_yaml
|
171
|
+
end
|
152
172
|
|
153
173
|
|
154
174
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 3
|
9
|
+
version: 1.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Bob Larrick
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-03-10 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: "A Ruby on Rails Plugin to help you create seed data.Useful when you want to move a few records from one database or environment to another. "
|
21
|
+
description: "A Ruby on Rails Plugin to help you create seed data. Useful when you want to move a few records from one database or environment to another. "
|
22
22
|
email: larrick@gmail.com
|
23
23
|
executables: []
|
24
24
|
|