Class: Transplant::Planter

Inherits:
Object
  • Object
show all
Defined in:
lib/transplant/planter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, connection) ⇒ Planter

Returns a new instance of Planter.



6
7
8
9
10
11
12
# File 'lib/transplant/planter.rb', line 6

def initialize(app_name, connection)
  @app_name = app_name
  @connection = connection
  @statistician = Stats.new(self)
  @queries ||= []
  @results ||= {}
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



4
5
6
# File 'lib/transplant/planter.rb', line 4

def app_name
  @app_name
end

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'lib/transplant/planter.rb', line 4

def connection
  @connection
end

#statisticianObject

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

#failuresObject



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.full_messages)
    @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

#successesObject



71
72
73
# File 'lib/transplant/planter.rb', line 71

def successes
  @successes ||= Hash.new
end

#tablesObject



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_successesObject



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_allObject



51
52
53
# File 'lib/transplant/planter.rb', line 51

def truncate_all
  truncate *tables
end