factory_bro 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/factory_bro.rb +36 -17
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a0d637ecdd62dc71833bfe6d0b98471b8214ba9
4
- data.tar.gz: 6351f82583eb758c6f014468e0ca2f8c3a8b3087
3
+ metadata.gz: 59a75451c5f0e7cb6413f88c1880be8fc307da79
4
+ data.tar.gz: 78fa6608aa32483b46a6dbae9aae9caddaab8800
5
5
  SHA512:
6
- metadata.gz: 2351bbf0d25e653fcf949f3f1303e18527ba8271eb76fce6d4582b08e236acf34d8c73109b11a4ad2acf7e89152f4599e440660fc1ad7b899ea0082cdce8dfeb
7
- data.tar.gz: d7991aa18411378cdbdd9231799f8ae59dcb091d9508e7a2ced9fc7fbf1cfadf32bbf38a450d3d171c2cfc403cf8b298bcbc1c7645c2f7f2ab38fc47d8591445
6
+ metadata.gz: b312e1ffd77694d46cc426574dd76a2986b3259c8d90555415272b346009cbcf2a51dfd5a991f040c3e80f44309166114b7d8d6df01f1b2425836cd70457d4a7
7
+ data.tar.gz: 080a2645b01f82a96b569a4d0786dbdcb485d71e9d70f29d5f32ddabe8a506f1c6dccee52886d42070ee79adcade03c64228a3ad34c32ff050d16cd13f9b4692
@@ -3,10 +3,19 @@ require 'faker'
3
3
  require 'json'
4
4
 
5
5
  class FactoryBro
6
- def self.connect(db)
6
+ def self.connect(name, db)
7
+ unless $conns
8
+ $conns = {}
9
+ else
10
+ $conns.each do |key, conn|
11
+ if conn.finished?
12
+ $conns[key] = ''
13
+ end
14
+ end
15
+ end
7
16
  if db.include? 'postgres://'
8
17
  dbData = parse_url(db)
9
- $conn = PG.connect( user: dbData[:user],
18
+ $conns[name] = PG.connect( user: dbData[:user],
10
19
  password: dbData[:pw],
11
20
  host: dbData[:host],
12
21
  port: dbData[:port],
@@ -14,35 +23,45 @@ class FactoryBro
14
23
  )
15
24
  else
16
25
  # take arguments of user and pw
17
- $conn = PG.connect( dbname: db)
26
+ $conns[name] = PG.connect( dbname: db)
18
27
  end
19
28
  end
20
29
 
21
- def self.close
22
- $conn.finish
30
+ def self.close(name)
31
+ $conns[name].finish
32
+ $conns[name] = ''
33
+ end
34
+
35
+ def self.close_all
36
+ $conns.each do |key, conn|
37
+ if conn.class == PG::Connection
38
+ conn.finish
39
+ end
40
+ end
41
+ $conns = nil
23
42
  end
24
43
 
25
- def self.exec(statement)
26
- run = $conn.exec(statement)
44
+ def self.exec(name, statement)
45
+ run = $conns[name].exec(statement)
27
46
  run.values
28
47
  end
29
48
 
30
- def self.create_bases
31
- tables = parse_tables
49
+ def self.create_bases(name)
50
+ tables = parse_tables(name)
32
51
  system 'mkdir', '-p', 'factories' unless File.directory?(Dir.pwd+"/factories")
33
52
  tables.each do |table|
34
53
  path = File.join(Dir.pwd, 'factories', "#{table}.rb")
35
54
  unless File.exists?(path)
36
55
 
37
- File.open(path, 'w') {|file| file.write(base_factory_content(table)) }
56
+ File.open(path, 'w') {|file| file.write(base_factory_content(name, table)) }
38
57
  # puts "#{table} factory created at: #{path}"
39
58
  end
40
59
  end
41
60
  end
42
61
 
43
- def self.generate_data(table, data)
62
+ def self.generate_data(name, table, data)
44
63
  helperData = generate_helper(data[:factoryData])
45
- res = $conn.exec("INSERT INTO #{table} (#{helperData[:columns]})
64
+ res = $conns[name].exec("INSERT INTO #{table} (#{helperData[:columns]})
46
65
  VALUES (#{helperData[:values]});")
47
66
  data[:meta]
48
67
  end
@@ -58,8 +77,8 @@ class FactoryBro
58
77
  }
59
78
  end
60
79
 
61
- def self.parse_tables
62
- res = $conn.exec("SELECT TABLE_NAME
80
+ def self.parse_tables(name)
81
+ res = $conns[name].exec("SELECT TABLE_NAME
63
82
  FROM INFORMATION_SCHEMA.TABLES
64
83
  WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='public'
65
84
  ORDER BY TABLE_NAME;")
@@ -68,20 +87,20 @@ class FactoryBro
68
87
 
69
88
  def self.parse_columns(table)
70
89
  # hash this
71
- res = $conn.exec(" SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
90
+ res = $conns[name].exec("SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
72
91
  FROM INFORMATION_SCHEMA.COLUMNS
73
92
  WHERE TABLE_NAME = '#{table}';")
74
93
  res.values # as tuple
75
94
  end
76
95
 
77
- def self.base_factory_content(table)
96
+ def self.base_factory_content(name, table)
78
97
  "module Factory
79
98
  class #{table.split('_').map { |word| word.capitalize }.join}
80
99
  def self.base
81
100
  # remember:
82
101
  # to return any needed metadata (i.e. ID)
83
102
  # run other required bases before running method below
84
- FactoryBro.generate_data('#{table}' , #{base_method_helper(table)})
103
+ FactoryBro.generate_data(#{name}, '#{table}' , #{base_method_helper(table)})
85
104
  end
86
105
  end
87
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Reynolds
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-11 00:00:00.000000000 Z
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg