atchu 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e93c9db74950af711e707f4a011a5a8707fa63e4
4
- data.tar.gz: 3b0a15146f78e151b7893b5b486f96fc2fd58e90
3
+ metadata.gz: d42132be4b71d56a6624717847bdd28d3951e252
4
+ data.tar.gz: 8845b8d2edbf2382b6c29d764d5a195694e62dbd
5
5
  SHA512:
6
- metadata.gz: d083818d9262da3de18d9efe858bcd91dcc516e0e7015519b9b39ad46dc699da12c67db1fd7bbae9077b98f9090f7da542fc10edd4986c99d0ea240f69451f59
7
- data.tar.gz: 8fc7b3715b5548c6a036aad6c29de16593a96168e66c69a8e34ab88bb106bf6db462d7d9e7e177afc158d6d24f8851c263d0680c84c46dc13d924437d5cbf5fc
6
+ metadata.gz: 931a67e2bb69818e36a48cd254f3553ab3470c7a1b1ef98ce6181c50ca07965fa5e817d8c4a7bcbd56d9452e1ed1cc1acf159321c5cd2989802478787f88c5d4
7
+ data.tar.gz: f115e21ee0fa428eaf6364bdc47e33280685f06e875475690cbcdd1117a1f11efffca0e9e8b87b9e6c062574e6585ec566a07dfac2e19d769f54a4c7ac7e4c81
data/README.md CHANGED
@@ -2,3 +2,7 @@ atchu
2
2
  =====
3
3
 
4
4
  Make Active Records form DB with composite keys
5
+
6
+
7
+ ##Credits:
8
+ Inspired by [RMRE](https://github.com/bosko/rmre)
@@ -1 +1,21 @@
1
+ require "atchu/connection"
2
+ require "atchu/model_file_maker"
3
+ require "atchu/model"
1
4
 
5
+ binding.pry
6
+
7
+ module Atchu
8
+ extend self
9
+
10
+ def load db_config
11
+ Connection.connect db_config
12
+ generate_models
13
+ end
14
+
15
+ private
16
+ def generate_models
17
+ tables = Connection.get.tables
18
+ models = tables.map{|table| Model.for_table(table)}
19
+ models.each{|model|ModelFileMaker.new(model).write_to "tmp"}
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require "yaml"
2
+
3
+ module Atchu
4
+ module Connection
5
+ extend self
6
+
7
+ def connect yml_file
8
+ return if yml_file.empty?
9
+ if @connection.blank?
10
+ ActiveRecord::Base.establish_connection(YAML.load_file(yml_file)[:db])
11
+ @connection = ActiveRecord::Base.connection
12
+ end
13
+ @connection
14
+ end
15
+
16
+ def get
17
+ @connection
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ require "atchu/queries"
2
+ require "active_support/inflector"
3
+
4
+ class Model
5
+ attr_accessor :table_name
6
+
7
+ def self.for_table table_name
8
+ model = Model.new
9
+ model.table_name = table_name
10
+ model
11
+ end
12
+
13
+ def class_name
14
+ table_name.classify
15
+ end
16
+
17
+ def file_name
18
+ table_name.classify.underscore
19
+ end
20
+
21
+ def primary_key
22
+ query = Atchu::Queries.primary_key table_name
23
+ keys = Atchu::Connection.get.select_all(query).rows
24
+ return "" if keys.eql? []
25
+ keys.reduce(:+).map{|x|x.intern}.to_s.gsub("[","").gsub("]","")
26
+ end
27
+
28
+ end
@@ -0,0 +1,30 @@
1
+ require "erubis"
2
+
3
+ module Atchu
4
+ class ModelFileMaker
5
+
6
+ def initialize input_model
7
+ @model = input_model
8
+ end
9
+
10
+ def write_to output_folder
11
+ file_content = get_file_content
12
+ write_to_file output_folder,file_content
13
+ end
14
+
15
+ private
16
+ attr_accessor :model
17
+ def get_file_content
18
+ eruby = Erubis::Eruby.new(File.read(File.join(File.expand_path("./../template/", __FILE__), 'model.eruby')))
19
+ eruby.result({:class_name => model.class_name,:table_name=>model.table_name,:primary_key=>model.primary_key})
20
+ end
21
+
22
+ def write_to_file output_path,file_content
23
+ FileUtils.mkdir_p(output_path)
24
+ File.open(File.join(Dir.pwd,output_path, "#{model.file_name}.rb"), "w") { |file| file.write file_content }
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
@@ -0,0 +1,21 @@
1
+ module Atchu
2
+ module Queries
3
+ extend self
4
+
5
+ def primary_key table_name
6
+ sql = <<-SQL
7
+ SELECT
8
+ pg_attribute.attname
9
+ FROM pg_index, pg_class, pg_attribute
10
+ WHERE
11
+ pg_class.oid = '#{table_name}'::regclass AND
12
+ indrelid = pg_class.oid AND
13
+ pg_attribute.attrelid = pg_class.oid AND
14
+ pg_attribute.attnum = any(pg_index.indkey)
15
+ AND indisprimary
16
+ SQL
17
+ end
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,10 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ <% unless class_name == table_name %>
3
+ <%= "self.table_name = '#{table_name}'" -%>
4
+ <% end %>
5
+
6
+ <% unless "id" == primary_key || primary_key.nil? %>
7
+ <%= "self.primary_key = #{primary_key}" -%>
8
+ <% end %>
9
+
10
+ end
@@ -0,0 +1,2 @@
1
+ require "atchu"
2
+ require "pry"
@@ -0,0 +1,11 @@
1
+ describe Atchu::ModelFileMaker,"ModelFileMaker" do
2
+ before(:each) do
3
+ @model = instance_double("Model", :table_name => "table", :class_name => "klass", :file_name => "klass_file", :primary_key => [:id,:name])
4
+ @model_file = Atchu::ModelFileMaker.new(@model)
5
+ end
6
+
7
+ it "should create file for a model" do
8
+ @model_file.write_to "tmp"
9
+ expect(File).to exist("tmp/#{@model.file_name}.rb")
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ describe Model,"model with table name, 'car_parts'" do
2
+
3
+ before(:each) do
4
+ @model = Model.for_table("car_parts")
5
+ end
6
+
7
+ it "model class name is CarPart" do
8
+ expect(@model.class_name).to eq("CarPart")
9
+ end
10
+
11
+ it "model file name is car_part" do
12
+ expect(@model.file_name).to eq("car_part")
13
+ end
14
+ end
15
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atchu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Deepak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2014-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: erubis
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Reverse engineer DB with composite keys to Active Records
112
126
  email:
113
127
  executables: []
@@ -116,7 +130,14 @@ extra_rdoc_files: []
116
130
  files:
117
131
  - README.md
118
132
  - lib/atchu.rb
133
+ - lib/atchu/connection.rb
134
+ - lib/atchu/model.rb
135
+ - lib/atchu/model_file_maker.rb
136
+ - lib/atchu/queries.rb
137
+ - lib/atchu/template/model.eruby
119
138
  - spec/spec_helper.rb
139
+ - spec/unit/model_file_maker_spec.rb
140
+ - spec/unit/model_spec.rb
120
141
  homepage: https://github.com/paramadeep/atchu
121
142
  licenses:
122
143
  - MIT
@@ -142,4 +163,6 @@ signing_key:
142
163
  specification_version: 4
143
164
  summary: Reverse engineer DB with composite keys to Active Records
144
165
  test_files:
166
+ - spec/unit/model_file_maker_spec.rb
167
+ - spec/unit/model_spec.rb
145
168
  - spec/spec_helper.rb