automodel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +81 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/.yardopts +1 -0
  8. data/Gemfile +6 -0
  9. data/Gemfile.lock +75 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +216 -0
  12. data/Rakefile +6 -0
  13. data/automodel.gemspec +37 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/docs/Automodel.html +161 -0
  17. data/docs/Automodel/AdapterAlreadyRegistered.html +144 -0
  18. data/docs/Automodel/AdapterAlreadyRegisteredError.html +144 -0
  19. data/docs/Automodel/CannotFindOnCompoundPrimaryKey.html +144 -0
  20. data/docs/Automodel/Connectors.html +117 -0
  21. data/docs/Automodel/Error.html +139 -0
  22. data/docs/Automodel/FindOnCompoundPrimaryKeyError.html +144 -0
  23. data/docs/Automodel/Helpers.html +722 -0
  24. data/docs/Automodel/NameCollisionError.html +143 -0
  25. data/docs/Automodel/SchemaInspector.html +1046 -0
  26. data/docs/Automodel/UnregisteredAdapter.html +144 -0
  27. data/docs/_index.html +206 -0
  28. data/docs/class_list.html +51 -0
  29. data/docs/css/common.css +1 -0
  30. data/docs/css/full_list.css +58 -0
  31. data/docs/css/style.css +496 -0
  32. data/docs/file.README.html +312 -0
  33. data/docs/file_list.html +56 -0
  34. data/docs/frames.html +17 -0
  35. data/docs/index.html +312 -0
  36. data/docs/js/app.js +292 -0
  37. data/docs/js/full_list.js +216 -0
  38. data/docs/js/jquery.js +4 -0
  39. data/docs/method_list.html +155 -0
  40. data/docs/top-level-namespace.html +477 -0
  41. data/lib/automodel.rb +132 -0
  42. data/lib/automodel/automodel.rb +4 -0
  43. data/lib/automodel/connectors.rb +8 -0
  44. data/lib/automodel/errors.rb +24 -0
  45. data/lib/automodel/helpers.rb +141 -0
  46. data/lib/automodel/schema_inspector.rb +218 -0
  47. data/lib/automodel/version.rb +10 -0
  48. data/samples/database.yml +38 -0
  49. metadata +231 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da81c00d3b957121f44501f62a502cc1ce1e505951d5a1a149a0ef4cbec4d9c8
4
+ data.tar.gz: 61974f2626587231515afc44602fa02bf8d5bc4e4d25fcf7faf20ddbfccc6d2a
5
+ SHA512:
6
+ metadata.gz: adf50209186f56812edd38de441c0d619dfde4b282f206861eabb30674deeb12b3fee6fa965bb150b128d40230e3c82b9ef9e56947acec2c2488f734592c1985
7
+ data.tar.gz: 7471bb7acc15ee079f54ed5b748cda4b31a9ab1b5e70eb63534edcde9c7ce90addf4aeb8e4f8ffca36c14a815379c9f77f2d2b08def7c4bee46126961176d540
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /pkg/
6
+ /spec/reports/
7
+ /tmp/
8
+ /true/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,81 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Include:
4
+ - "**/*.rake"
5
+ - "**/Gemfile"
6
+ - "**/Rakefile"
7
+ - "**/Capfile"
8
+ - "**/Berksfile"
9
+ - "**/Cheffile"
10
+ Exclude:
11
+ - "vendor/**/*"
12
+ - "db/**/*"
13
+ - "tmp/**/*"
14
+ - "true/**/*"
15
+ Metrics/ClassLength:
16
+ Description: Avoid classes longer than 100 lines of code.
17
+ Enabled: false
18
+ CountComments: false
19
+ Max: 100
20
+ Metrics/LineLength:
21
+ Description: Limit lines to 100 characters.
22
+ Enabled: false
23
+ Max: 100
24
+ Metrics/BlockLength:
25
+ Exclude:
26
+ - 'spec/**/*.rb'
27
+ Metrics/MethodLength:
28
+ Description: Avoid methods longer than 10 lines of code.
29
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
30
+ Enabled: false
31
+ CountComments: false
32
+ Max: 10
33
+ Metrics/AbcSize:
34
+ Description: A calculated magnitude based on number of assignments, branches, and conditions.
35
+ Enabled: false
36
+ Max: 15
37
+ Metrics/CyclomaticComplexity:
38
+ Description: A complexity metric that is strongly correlated to the number of test cases needed to validate a method.
39
+ Enabled: false
40
+ Max: 6
41
+ Lint/Debugger:
42
+ Description: Warn in debugger entries
43
+ Enabled: false
44
+ Style/SymbolArray:
45
+ Description: Use %i or %I for arrays of symbols.
46
+ Enabled: false
47
+ Style/RegexpLiteral:
48
+ Description: Enforces using / or %r around regular expressions.
49
+ EnforcedStyle: percent_r
50
+ Style/AsciiComments:
51
+ # Disabling this so we can use non-breaking spaces (' ') in documentation comments, preventing browsers from collapsing multiple spaces in code blocks.
52
+ Description: This cop checks for non-ascii (non-English) characters in comments.
53
+ Enabled: false
54
+ Style/NumericLiterals:
55
+ Description: This cop checks for big numeric literals without _ between groups of digits in them.
56
+ Enabled: false
57
+ Style/Documentation:
58
+ Description: Document classes and non-namespace modules.
59
+ Enabled: false
60
+ Style/ClassAndModuleChildren:
61
+ Description: Use nested modules/class definitions instead of compact style.
62
+ Enabled: false
63
+ Style/FrozenStringLiteralComment:
64
+ Enabled: false
65
+ Style/EmptyMethod:
66
+ Enabled: false
67
+ Style/StderrPuts:
68
+ Enabled: true
69
+ Exclude:
70
+ - 'bin/**/*'
71
+ Style/BlockDelimiters:
72
+ Description: Check for uses of braces or do/end around single line or multi-line blocks.
73
+ Enabled: true
74
+ Exclude:
75
+ - 'spec/**/*.rb'
76
+ Style/RescueModifier:
77
+ Description: This cop checks for uses of rescue in its modifier form.
78
+ Enabled: false
79
+ Naming/UncommunicativeMethodParamName:
80
+ Description: This cop checks method parameter names for how descriptive they are.
81
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --output-dir=./docs --no-private --markup=markdown --format=html
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in automodel.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ automodel (0.1.0)
5
+ activerecord (~> 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.0)
11
+ activesupport (= 5.2.0)
12
+ activerecord (5.2.0)
13
+ activemodel (= 5.2.0)
14
+ activesupport (= 5.2.0)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.0)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ byebug (10.0.2)
23
+ coderay (1.1.2)
24
+ concurrent-ruby (1.0.5)
25
+ diff-lcs (1.3)
26
+ i18n (1.0.1)
27
+ concurrent-ruby (~> 1.0)
28
+ method_source (0.9.0)
29
+ minitest (5.11.3)
30
+ mysql2 (0.5.1)
31
+ pg (0.21.0)
32
+ pry (0.11.3)
33
+ coderay (~> 1.1.0)
34
+ method_source (~> 0.9.0)
35
+ pry-byebug (3.6.0)
36
+ byebug (~> 10.0)
37
+ pry (~> 0.10)
38
+ rake (10.5.0)
39
+ rspec (3.7.0)
40
+ rspec-core (~> 3.7.0)
41
+ rspec-expectations (~> 3.7.0)
42
+ rspec-mocks (~> 3.7.0)
43
+ rspec-core (3.7.1)
44
+ rspec-support (~> 3.7.0)
45
+ rspec-expectations (3.7.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.7.0)
48
+ rspec-mocks (3.7.0)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.7.0)
51
+ rspec-support (3.7.1)
52
+ rspec_junit_formatter (0.4.1)
53
+ rspec-core (>= 2, < 4, != 2.12.0)
54
+ sqlite3 (1.3.13)
55
+ thread_safe (0.3.6)
56
+ tzinfo (1.2.5)
57
+ thread_safe (~> 0.1)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ automodel!
64
+ bundler (~> 1.16)
65
+ mysql2 (~> 0.5.1)
66
+ pg (~> 0.21)
67
+ pry (~> 0.11)
68
+ pry-byebug (~> 3.6)
69
+ rake (~> 10.0)
70
+ rspec (~> 3.0)
71
+ rspec_junit_formatter (~> 0.3)
72
+ sqlite3 (~> 1.3.13)
73
+
74
+ BUNDLED WITH
75
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nestor Custodio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,216 @@
1
+ # Automodel [![Gem Version](https://badge.fury.io/rb/automodel.svg)](https://badge.fury.io/rb/automodel)
2
+
3
+ Connecting your Rails application to a database created outside of the Rails environment usually means either spending hours writing up class files for every table, or giving up on using the ActiveRecord query DSL and resigning yourself to building SQL strings and making `execute`/`exec_query` calls.
4
+
5
+ Are those SQL strings you're building even injection-safe? Hmm... 😟
6
+
7
+ *With a single command*, **Automodel** lets you connect to any database and access all of its tables via the ActiveRecord DSL you've grown to love!
8
+
9
+ It does this by analyzing the table structures and:
10
+ - automatically defining all of the corresponding model classes
11
+ - declaring column aliases so you can use Railsy names an idioms
12
+ - constructing model relations based on foreign key definitions
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'automodel'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install automodel
30
+
31
+
32
+ ## Using Automodel
33
+
34
+ The following examples all assume a Postgres database with the following tables:
35
+ ```sql
36
+ -- Create Table: Authors
37
+ CREATE TABLE public."Authors" (
38
+ "Author ID" serial NOT NULL ,
39
+ "Name" varchar NOT NULL DEFAULT '',
40
+ "Birthday" date NULL ,
41
+ "Address" varchar NOT NULL DEFAULT '',
42
+
43
+ CONSTRAINT authors__pk PRIMARY KEY ("Author ID")
44
+ ) WITH ( OIDS=FALSE );
45
+
46
+
47
+ -- Create Table: Publishers
48
+ CREATE TABLE public."Publishers" (
49
+ "Publisher ID" serial NOT NULL ,
50
+ "Name" varchar NOT NULL DEFAULT '',
51
+ "Address" varchar NOT NULL DEFAULT '',
52
+ "Website" varchar NOT NULL DEFAULT '',
53
+
54
+ CONSTRAINT publishers__pk PRIMARY KEY ("Publisher ID")
55
+ ) WITH ( OIDS=FALSE );
56
+
57
+
58
+ -- Create Table: Books
59
+ CREATE TABLE public."Books" (
60
+ "Book ID" serial NOT NULL ,
61
+ "Title" varchar NOT NULL DEFAULT '' ,
62
+ "Edition" int NOT NULL DEFAULT 1 ,
63
+ "ISBN Number" varchar NOT NULL DEFAULT '' ,
64
+ "Published On" date NOT NULL ,
65
+ "Is Out Of Print" bool NOT NULL DEFAULT FALSE,
66
+ "Author ID" bigint NOT NULL ,
67
+ "Publisher ID" bigint NOT NULL ,
68
+
69
+ CONSTRAINT books__pk PRIMARY KEY ("Book ID"),
70
+
71
+ CONSTRAINT books_authors_fk FOREIGN KEY ("Author ID")
72
+ REFERENCES public."Authors"("Author ID"),
73
+
74
+ CONSTRAINT books_publishers_fk FOREIGN KEY ("Publisher ID")
75
+ REFERENCES public."Publishers"("Publisher ID")
76
+ ) WITH ( OIDS=FALSE );
77
+ ```
78
+
79
+ ---
80
+
81
+ #### Connecting To The External Database
82
+
83
+ You can provide the connection spec inline ...
84
+
85
+ ```ruby
86
+ automodel adapter: 'postgresql' ,
87
+ encoding: 'unicode' ,
88
+ host: hostname ,
89
+ port: port_number ,
90
+ username: username ,
91
+ password: password ,
92
+ database: database_name
93
+ ```
94
+
95
+ ... or you can use a connection spec defined in "config/database.yml" ...
96
+
97
+ ```yml
98
+ ## In "database.yml" ...
99
+
100
+ ## ... (your application's own db connection stuff) ...
101
+
102
+
103
+ external_db:
104
+ adapter: postgresql
105
+ pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
106
+ timeouts: 5000
107
+ encoding: unicode
108
+ host: name_or_ip
109
+ port: port_number
110
+ username: username
111
+ password: password
112
+ database: sample_db
113
+ ```
114
+
115
+ ```ruby
116
+ ## In "config/puma.rb" or "config/unicorn.rb" ...
117
+
118
+ automodel :external_db
119
+ ```
120
+
121
+ ---
122
+
123
+ #### Using The Automodel'ed Objects
124
+
125
+ Connecting via either method above will allow you to issue all of the following expressions, just as
126
+ if these were your own models:
127
+
128
+ ```ruby
129
+ ## ISBNs for all non-first-edition books.
130
+ isbn_list = Book.where.not(edition: 1).pluck(:isbn_number)
131
+
132
+ ## Take any book and look up some values.
133
+ book = Book.take
134
+ book.title
135
+ book.out_of_print?
136
+ book.publisher.name
137
+
138
+
139
+ ## Note that some ActiveRecord constructs surface real table names,
140
+ ## which can look awkward in code when working with tables with non-Railsy names:
141
+ ## (the uppercase "P" in "Publishers" below makes it look like a class reference)
142
+ Book.joins(:Publishers).where(Publishers: { name: test_value }).all
143
+ ```
144
+
145
+ ---
146
+
147
+ #### Automodel With Namespacing
148
+
149
+ If you're worried about model name collisions (or just want to keep the global namespace tidy), Automodel can define all of the new model classes under a module.
150
+
151
+ ```ruby
152
+ automodel adapter: 'postgresql' ,
153
+ encoding: 'unicode' ,
154
+ host: hostname ,
155
+ port: port_number ,
156
+ username: username ,
157
+ password: password ,
158
+ database: database_name,
159
+ namespace: 'ExternalDB'
160
+
161
+ ## Now you can do everything you'd expect, but the models are namespaced under ExternalDB.
162
+ ExternalDB::Book.find(5) ## => Book #5
163
+ ExternalDB::Book.take.author.class ## => ExternalDB::Author
164
+
165
+ ```
166
+
167
+ ---
168
+
169
+ [Consult the repo docs for the full Automodel documentation.](http://nestor-custodio.github.io/automodel/Automodel.html)
170
+
171
+
172
+ ## FAQs
173
+
174
+ - ##### Do I have to add anything to my Gemfile besides `'automodel'`?
175
+ Only if you want to use connection adapters that are not yet part of your gemset. (e.g. Don't expect to be able to connect to a MySQL database without having added `'mysql2'` to your Gemfile.
176
+
177
+ - ##### But what about my application's own models?
178
+ You can use Automodel **and** continue to use your application's own models without changing a single line of code.
179
+
180
+ - ##### Can I Automodel more than one database?
181
+ Yes! You can Automodel as many databases with as many different adapters as you like. Automodel takes care of connecting to the various databases and managing their connection pools for you.
182
+
183
+ - ##### What about model name collisions?
184
+ If an `automodel` call will result in a class name collision, an Automodel::NameCollisionError is raised *before* any classes are clobbered.
185
+
186
+ - ##### What if I want custom methods for certain models?
187
+ You can either monkey-patch your methods onto the applicable Automodel-generated classes once they've been defined, or you can monkey-patch the method onto the connection handler class returned by the `automodel` call itself, which will make it available for all models generated *by that call*.
188
+
189
+ - ##### What if I'm using ActiveRecord but not Rails?
190
+ That's no problem at all! The **automodel** gem's *only* dependency is ActiveRecord -- not Rails. Adding `'automodel'` to your Gemfile (along with any relevant connection adapters, of course) is all you need to make use of the tool in your vanilla-Ruby project. Just be mindful that -- since "config/database.yml" isn't available (as you're not using Rails) -- you'll always need to pass in a full connection spec to your `automodel` calls (as in the very first example, under *"Connecting To The External Database"* above).
191
+
192
+
193
+ ## Feature Roadmap / Future Development
194
+
195
+ Additional features/options coming in the future:
196
+
197
+ - **Naming**: Better generation of Railsy names for `:date`/`:datetime` column types.
198
+ - **Reads**: Support for `#find` on tables with composite primary keys.
199
+ - **Writes**: Better handling of missing `created_at`/`updated_at` columns on record creation/updates.
200
+ - **Traversal**: Support for `has_many` relations (only `belongs_to` is currently supported).
201
+ - **Traversal**: Support for self-referential foreign keys.
202
+ - **Traversal**: Support for multiple relations to the same target model.
203
+
204
+
205
+ ## Contribution / Development
206
+
207
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nestor-custodio/automodel.
208
+
209
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
210
+
211
+ Linting is courtesy of [Rubocop](https://github.com/bbatsov/rubocop) and documentation is built using [Yard](https://yardoc.org/). Neither is included in the Gemspec; you'll need to install these locally (`gem install rubocop yard`) to take advantage.
212
+
213
+
214
+ ## License
215
+
216
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec