Class: Transplant::Planter
- Inherits:
-
Object
- Object
- Transplant::Planter
- Defined in:
- lib/transplant/planter.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
Returns the value of attribute app_name.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#statistician ⇒ Object
Returns the value of attribute statistician.
Instance Method Summary collapse
- #column_names(table_name) ⇒ Object
- #fail(klass_name) ⇒ Object
- #failures ⇒ Object
-
#initialize(app_name, connection) ⇒ Planter
constructor
A new instance of Planter.
- #query(sql) ⇒ Object
- #save(klass, other = {}) ⇒ Object
- #succeed(klass_name) ⇒ Object
- #successes ⇒ Object
- #tables ⇒ Object
- #total_successes ⇒ Object
- #truncate(*tables) ⇒ Object
- #truncate_all ⇒ Object
Constructor Details
Instance Attribute Details
#app_name ⇒ Object
Returns the value of attribute app_name.
4 5 6 |
# File 'lib/transplant/planter.rb', line 4 def app_name @app_name end |
#connection ⇒ Object
Returns the value of attribute connection.
4 5 6 |
# File 'lib/transplant/planter.rb', line 4 def connection @connection end |
#statistician ⇒ Object
Returns the value of attribute statistician.
4 5 6 |
# File 'lib/transplant/planter.rb', line 4 def statistician @statistician end |
Instance Method Details
#column_names(table_name) ⇒ Object
43 44 45 |
# File 'lib/transplant/planter.rb', line 43 def column_names(table_name) connection.columns(table_name).map(&:name) end |
#fail(klass_name) ⇒ Object
61 62 63 64 65 |
# File 'lib/transplant/planter.rb', line 61 def fail(klass_name) @failures ||= Hash.new @failures[klass_name] ||= 0 @failures[klass_name] += 1 end |
#failures ⇒ Object
67 68 69 |
# File 'lib/transplant/planter.rb', line 67 def failures @failures ||= Hash.new end |
#query(sql) ⇒ Object
14 15 16 17 18 |
# File 'lib/transplant/planter.rb', line 14 def query(sql) return @results[sql] if @queries.include?(sql) @queries << sql @results[sql] = @connection.execute(sql) end |
#save(klass, other = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/transplant/planter.rb', line 20 def save(klass, other = {}) klass_name = klass.class.to_s.tableize.humanize.singularize if klass.valid? klass.save! succeed klass_name klass else fail(klass_name) @statistician.output "Invalid #{klass_name} information:" @statistician.output("Additional Info about #{klass_name}", other) @statistician.output("#{klass_name} errors", klass.errors.) @statistician.output("#{klass_name} attributes", klass.attributes) return false end end |
#succeed(klass_name) ⇒ Object
55 56 57 58 59 |
# File 'lib/transplant/planter.rb', line 55 def succeed(klass_name) @successes ||= Hash.new @successes[klass_name] ||= 0 @successes[klass_name] += 1 end |
#successes ⇒ Object
71 72 73 |
# File 'lib/transplant/planter.rb', line 71 def successes @successes ||= Hash.new end |
#tables ⇒ Object
36 37 38 39 40 41 |
# File 'lib/transplant/planter.rb', line 36 def tables return @tables if @tables.present? @tables ||= @connection.tables @tables.delete 'schema_migrations' @tables end |
#total_successes ⇒ Object
75 76 77 |
# File 'lib/transplant/planter.rb', line 75 def total_successes @successes.map { |table, count| count }.inject{ |sum,x| sum + x } end |
#truncate(*tables) ⇒ Object
47 48 49 |
# File 'lib/transplant/planter.rb', line 47 def truncate(*tables) tables.each { |table| self.query "TRUNCATE TABLE #{table.to_s}" } end |
#truncate_all ⇒ Object
51 52 53 |
# File 'lib/transplant/planter.rb', line 51 def truncate_all truncate *tables end |