gardener 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/gardener.rb +164 -144
  2. metadata +4 -4
data/lib/gardener.rb CHANGED
@@ -1,154 +1,174 @@
1
1
  module Gardener
2
- def self.included(base)
3
- base.extend(ClassMethods)
4
- end
5
-
6
- module ClassMethods
7
-
8
- attr_accessor :infinite_choice
9
-
10
-
11
- # Dump every instance of a class (every record in the database for a model) into a file
12
- # to be reconstituted later.
13
- def sow
14
- c = self.to_s.underscore.pluralize
15
- dir = File.join [Rails.root, 'db', 'garden']
16
- Dir.mkdir(dir) unless File.exists?(dir)
17
-
18
- File.open(File.join([Rails.root, 'db', 'garden', "#{c}.yml"] ), 'w') do |file|
19
- self.find_each{ |x| file << x.to_yaml }
20
- end
21
- end
22
-
23
-
24
- # Take the things that were dumped to a file and put them in the database.
25
- def reap
26
- c = self.to_s.underscore.pluralize
27
- str = ''
28
- File.open(File.join([Rails.root, 'db', 'garden', "#{c}.yml"] ), 'r').each_line do |line|
29
- if line.match /^---.*/
30
- sprout(str)
31
- str = ''
32
- end
33
- str << line
34
- end
35
- sprout(str) unless str.blank?
36
- end
37
-
38
-
39
- # Helper function to reconstitute a record from it's yaml representation.
40
- def sprout(str)
41
- return false if str.blank?
42
- # puts str.inspect
43
-
44
- foo = YAML.load(str)
45
- return unless foo # YAML.load('') == false
46
- bob = self.new(foo.attributes)
47
- bob.id = foo.id
48
-
49
- # Grr, have to check to see if anything in the inheritance hierarchy has the id.
50
- base_class = self.base_class
51
- scott = base_class.find_by_id(bob.id)
52
-
53
- if scott
54
- puts "\n\n#{bob.class.name} with id #{bob.id} already exists, differences are"
55
- bar = scott.diff(bob)
56
- if bar.blank?
57
- 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."
58
- else
59
- pp bar
60
- end
61
-
62
- if @infinite_choice
63
- res = @infinite_choice
64
- else
65
- 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) ? "
66
- res = gets
67
- @infinite_choice = res if res.match(/z/i)
68
- end
69
-
70
- if res.match(/y/i)
71
- scott = bob
72
- scott.save
73
- elsif res.match(/a/i)
74
- raise "Salting the earth. Nothing will grow here for some time ..."
75
- elsif res.match(/i/i)
76
- bob.id = nil
77
- bob.save
78
- else
79
- puts "#{bob.class.name} with id #{bob.id} already exists, doing nothing."
80
- end
81
- else
82
- bob.save
83
- end
84
- end
85
-
86
- end # End ClassMethods
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
- def diff(obj)
94
- buff = {}
95
- self.attributes.each do |k, v|
96
- tmp = obj.send(k) if obj.respond_to?(k)
97
- if tmp.eql?(v)
98
- # they're the same, do nothing
99
- else
100
- buff[k] = {:original => v, :new => tmp}
101
- end
102
- end # end self.attributes.each
103
- buff
104
- end
105
-
106
-
107
- # Called on an particular instance of a class to render it down into a file, from which it can be
108
- # _reaped_ later.
109
- def plant_seed(options = {})
110
- c = self.class.to_s.underscore.pluralize
111
- # Code to render down associated items exists, but nothing has been done to pull those objects back out.
112
- # Not automatically at least, but of course reaping the correct model will pull them up no problem.
113
-
114
-
115
- # TODO
116
- # need to load the file up and see if the thing i'm trying to add is in it
117
- # or just overwrite the whole file every time.
118
- # Do something to prevent duplicates from slipping in.
119
- dir = File.join [Rails.root, 'db', 'garden']
120
- Dir.mkdir(dir) unless File.exists?(dir)
121
-
122
-
123
- File.open(File.join([Rails.root, 'db', 'garden', "#{c}.yml"] ), 'a') do |file|
124
- file << self.to_yaml
125
- end
126
-
127
- includes = things_to_include(options)
128
- puts includes
129
- includes.each do |k, v|
130
- tom = self.send(k) if self.respond_to?(k)
131
- [*tom].each{|y| y.plant_seed({:include => v})}
132
- end
133
-
134
- end
135
-
136
-
137
- def things_to_include(options)
138
- # [*THING] is so that THING can be an array or a single instance of a class.
139
- # .each will blow up if THING is just an instance, not an array, unless we explode and re-array it.
140
- return [] unless options[:include]
141
- case options[:include]
142
- when Hash
143
- options[:include].inject({}){|memo, v| memo[v[0]] = v[1] if self.respond_to?(v[0]); memo}
144
- else
145
- [*options[:include]]
146
- end
147
- end
148
-
149
- def to_fixture
150
- {"#{self.class.to_s.underscore}_#{self.id}" => self.attributes}.to_yaml
151
- end
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
- - 2
9
- version: 1.1.2
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-02-09 00:00:00 -05:00
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