active_record_schema_scrapper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Appraisals +11 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +20 -0
- data/active_record_schema_scrapper.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/gemfiles/activerecord_4.0.gemfile +7 -0
- data/gemfiles/activerecord_4.0.gemfile.lock +61 -0
- data/gemfiles/activerecord_4.1.gemfile +7 -0
- data/gemfiles/activerecord_4.1.gemfile.lock +61 -0
- data/gemfiles/activerecord_4.2.gemfile +7 -0
- data/gemfiles/activerecord_4.2.gemfile.lock +61 -0
- data/lib/active_record_schema_scrapper/association.rb +13 -0
- data/lib/active_record_schema_scrapper/associations.rb +47 -0
- data/lib/active_record_schema_scrapper/attribute.rb +73 -0
- data/lib/active_record_schema_scrapper/attributes.rb +52 -0
- data/lib/active_record_schema_scrapper/version.rb +3 -0
- data/lib/active_record_schema_scrapper.rb +32 -0
- metadata +167 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1bb5f2380fba44f87c33896211f1a4cde471abe8
|
4
|
+
data.tar.gz: 2c0233670efdc5067016cd132a5c0bd2c27b6ddd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c9428815ebb80cb3a2085f78fbefbcd04aac55d83ee3840ff2df6649b62ea9519e955259e6b54bb41589fe568f0cd5ed4e1a0d205fd25b2bb5e5f3bfa7d8bb2d
|
7
|
+
data.tar.gz: 76723da500c4e809c4cd4060297cf76af8ce515c485a3573ffcf5e892e5d42fb3f09eb89407423bd39b01495dc561bfc6f286999796b6129f624f8dbb9e7169c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Dustin Zeisler
|
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,49 @@
|
|
1
|
+
# ActiveRecordSchemaScrapper
|
2
|
+
[![Build Status](https://travis-ci.org/zeisler/dissociated_introspection.svg)](https://travis-ci.org/zeisler/dissociated_introspection)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/zeisler/active_record_schema_scrapper/badges/gpa.svg)](https://codeclimate.com/github/zeisler/active_record_schema_scrapper)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/zeisler/active_record_schema_scrapper/badges/coverage.svg)](https://codeclimate.com/github/zeisler/active_record_schema_scrapper/coverage)
|
5
|
+
|
6
|
+
This gem gives a simple API for ActiveRecord model meta data, including attributes and associations.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'active_record_schema_scrapper'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install active_record_schema_scrapper
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
ActiveRecordSchemaScrapper::Attributes.new(model: User).to_h
|
27
|
+
#=>[{ name: "id", type: Fixnum },
|
28
|
+
{ name: "name", type: String },
|
29
|
+
{ name: "email", type: String, default: "" },
|
30
|
+
{ name: "credits", type: BigDecimal, precision: 19, scale: 6 },
|
31
|
+
{ name: "created_at", type: DateTime },
|
32
|
+
{ name: "updated_at", type: DateTime },
|
33
|
+
{ name: "password_digest", type: String },
|
34
|
+
{ name: "remember_token", type: Axiom::Types::Boolean, default: true },
|
35
|
+
{ name: "admin", type: Axiom::Types::Boolean, default: false }]
|
36
|
+
|
37
|
+
## Development
|
38
|
+
|
39
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
40
|
+
|
41
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/[my-github-username]/active_record_schema_scrapper/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
task :default => 'specs'
|
4
|
+
|
5
|
+
task :specs do
|
6
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION >= "1.9"
|
7
|
+
module Kernel
|
8
|
+
alias :__at_exit :at_exit
|
9
|
+
|
10
|
+
def at_exit(&block)
|
11
|
+
__at_exit do
|
12
|
+
exit_status = $!.status if $!.is_a?(SystemExit)
|
13
|
+
block.call
|
14
|
+
exit exit_status if exit_status
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
sh "bundle exec rspec"
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_record_schema_scrapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_record_schema_scrapper"
|
8
|
+
spec.version = ActiveRecordSchemaScrapper::VERSION
|
9
|
+
spec.authors = ["Dustin Zeisler"]
|
10
|
+
spec.email = ["dustin@zeisler.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{A wrapper around an active_record model.}
|
13
|
+
spec.description = %q{A supporting library for active_mocker and wrapper around an active_record model.}
|
14
|
+
spec.homepage = "https://github.com/zeisler/active_record_schema_scrapper"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "virtus", "~> 1.0"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
26
|
+
spec.add_development_dependency "sqlite3", "~>1.3"
|
27
|
+
spec.add_development_dependency "activerecord", "~>4.2"
|
28
|
+
spec.add_development_dependency "appraisal", "~> 2.0"
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_record_schema_scrapper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
active_record_schema_scrapper (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (4.2.1)
|
10
|
+
activesupport (= 4.2.1)
|
11
|
+
builder (~> 3.1)
|
12
|
+
activerecord (4.2.1)
|
13
|
+
activemodel (= 4.2.1)
|
14
|
+
activesupport (= 4.2.1)
|
15
|
+
arel (~> 6.0)
|
16
|
+
activesupport (4.2.1)
|
17
|
+
i18n (~> 0.7)
|
18
|
+
json (~> 1.7, >= 1.7.7)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
appraisal (2.0.1)
|
23
|
+
activesupport (>= 3.2.21)
|
24
|
+
bundler
|
25
|
+
rake
|
26
|
+
thor (>= 0.14.0)
|
27
|
+
arel (6.0.0)
|
28
|
+
builder (3.2.2)
|
29
|
+
diff-lcs (1.2.5)
|
30
|
+
i18n (0.7.0)
|
31
|
+
json (1.8.3)
|
32
|
+
minitest (5.7.0)
|
33
|
+
rake (10.4.2)
|
34
|
+
rspec (3.3.0)
|
35
|
+
rspec-core (~> 3.3.0)
|
36
|
+
rspec-expectations (~> 3.3.0)
|
37
|
+
rspec-mocks (~> 3.3.0)
|
38
|
+
rspec-core (3.3.1)
|
39
|
+
rspec-support (~> 3.3.0)
|
40
|
+
rspec-expectations (3.3.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.3.0)
|
43
|
+
rspec-mocks (3.3.1)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.3.0)
|
46
|
+
rspec-support (3.3.0)
|
47
|
+
thor (0.19.1)
|
48
|
+
thread_safe (0.3.5)
|
49
|
+
tzinfo (1.2.2)
|
50
|
+
thread_safe (~> 0.1)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
active_record_schema_scrapper!
|
57
|
+
activerecord (~> 4.0)
|
58
|
+
appraisal (~> 2.0)
|
59
|
+
bundler (~> 1.9)
|
60
|
+
rake (~> 10.0)
|
61
|
+
rspec (~> 3.2)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
active_record_schema_scrapper (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (4.2.1)
|
10
|
+
activesupport (= 4.2.1)
|
11
|
+
builder (~> 3.1)
|
12
|
+
activerecord (4.2.1)
|
13
|
+
activemodel (= 4.2.1)
|
14
|
+
activesupport (= 4.2.1)
|
15
|
+
arel (~> 6.0)
|
16
|
+
activesupport (4.2.1)
|
17
|
+
i18n (~> 0.7)
|
18
|
+
json (~> 1.7, >= 1.7.7)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
appraisal (2.0.1)
|
23
|
+
activesupport (>= 3.2.21)
|
24
|
+
bundler
|
25
|
+
rake
|
26
|
+
thor (>= 0.14.0)
|
27
|
+
arel (6.0.0)
|
28
|
+
builder (3.2.2)
|
29
|
+
diff-lcs (1.2.5)
|
30
|
+
i18n (0.7.0)
|
31
|
+
json (1.8.3)
|
32
|
+
minitest (5.7.0)
|
33
|
+
rake (10.4.2)
|
34
|
+
rspec (3.3.0)
|
35
|
+
rspec-core (~> 3.3.0)
|
36
|
+
rspec-expectations (~> 3.3.0)
|
37
|
+
rspec-mocks (~> 3.3.0)
|
38
|
+
rspec-core (3.3.1)
|
39
|
+
rspec-support (~> 3.3.0)
|
40
|
+
rspec-expectations (3.3.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.3.0)
|
43
|
+
rspec-mocks (3.3.1)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.3.0)
|
46
|
+
rspec-support (3.3.0)
|
47
|
+
thor (0.19.1)
|
48
|
+
thread_safe (0.3.5)
|
49
|
+
tzinfo (1.2.2)
|
50
|
+
thread_safe (~> 0.1)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
active_record_schema_scrapper!
|
57
|
+
activerecord (~> 4.1)
|
58
|
+
appraisal (~> 2.0)
|
59
|
+
bundler (~> 1.9)
|
60
|
+
rake (~> 10.0)
|
61
|
+
rspec (~> 3.2)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
active_record_schema_scrapper (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (4.2.1)
|
10
|
+
activesupport (= 4.2.1)
|
11
|
+
builder (~> 3.1)
|
12
|
+
activerecord (4.2.1)
|
13
|
+
activemodel (= 4.2.1)
|
14
|
+
activesupport (= 4.2.1)
|
15
|
+
arel (~> 6.0)
|
16
|
+
activesupport (4.2.1)
|
17
|
+
i18n (~> 0.7)
|
18
|
+
json (~> 1.7, >= 1.7.7)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
appraisal (2.0.1)
|
23
|
+
activesupport (>= 3.2.21)
|
24
|
+
bundler
|
25
|
+
rake
|
26
|
+
thor (>= 0.14.0)
|
27
|
+
arel (6.0.0)
|
28
|
+
builder (3.2.2)
|
29
|
+
diff-lcs (1.2.5)
|
30
|
+
i18n (0.7.0)
|
31
|
+
json (1.8.3)
|
32
|
+
minitest (5.7.0)
|
33
|
+
rake (10.4.2)
|
34
|
+
rspec (3.3.0)
|
35
|
+
rspec-core (~> 3.3.0)
|
36
|
+
rspec-expectations (~> 3.3.0)
|
37
|
+
rspec-mocks (~> 3.3.0)
|
38
|
+
rspec-core (3.3.1)
|
39
|
+
rspec-support (~> 3.3.0)
|
40
|
+
rspec-expectations (3.3.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.3.0)
|
43
|
+
rspec-mocks (3.3.1)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.3.0)
|
46
|
+
rspec-support (3.3.0)
|
47
|
+
thor (0.19.1)
|
48
|
+
thread_safe (0.3.5)
|
49
|
+
tzinfo (1.2.2)
|
50
|
+
thread_safe (~> 0.1)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
active_record_schema_scrapper!
|
57
|
+
activerecord (~> 4.2)
|
58
|
+
appraisal (~> 2.0)
|
59
|
+
bundler (~> 1.9)
|
60
|
+
rake (~> 10.0)
|
61
|
+
rspec (~> 3.2)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class ActiveRecordSchemaScrapper
|
2
|
+
class Association
|
3
|
+
include Virtus.model
|
4
|
+
attribute :name, Symbol
|
5
|
+
attribute :class_name, Symbol
|
6
|
+
attribute :type, Symbol
|
7
|
+
attribute :through, Symbol
|
8
|
+
attribute :source, Symbol
|
9
|
+
attribute :foreign_key, Symbol
|
10
|
+
attribute :join_table, Symbol
|
11
|
+
attribute :dependent, Symbol
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class ActiveRecordSchemaScrapper
|
2
|
+
class Associations
|
3
|
+
|
4
|
+
def initialize(model:, types: self.class.types)
|
5
|
+
@model = model
|
6
|
+
@types = types
|
7
|
+
end
|
8
|
+
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
def each
|
12
|
+
types.each do |type|
|
13
|
+
model.reflect_on_all_associations(type).each do |a|
|
14
|
+
hash = if a.try(:delegate_reflection)
|
15
|
+
{ source: a.delegate_reflection.options[:source],
|
16
|
+
through: a.delegate_reflection.options[:through],
|
17
|
+
dependent: a.delegate_reflection.options[:dependent],
|
18
|
+
}
|
19
|
+
else
|
20
|
+
{ source: a.try(:delegate_reflection).try(:name),
|
21
|
+
through: a.try(:through),
|
22
|
+
dependent: a.options[:dependent] }
|
23
|
+
end.merge({
|
24
|
+
name: a.name,
|
25
|
+
foreign_key: a.foreign_key,
|
26
|
+
class_name: a.klass.name,
|
27
|
+
type: type,
|
28
|
+
})
|
29
|
+
yield(ActiveRecordSchemaScrapper::Association.new(hash))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_a
|
35
|
+
map {|v| v}
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.types
|
39
|
+
[:has_and_belongs_to_many, :belongs_to, :has_one, :has_many]
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
attr_reader :model, :types
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require "virtus"
|
2
|
+
|
3
|
+
class ActiveRecordSchemaScrapper
|
4
|
+
class UnregisteredType < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class Attribute
|
8
|
+
include Virtus.model
|
9
|
+
attribute :name
|
10
|
+
|
11
|
+
class DBToRubyType < Virtus::Attribute
|
12
|
+
def coerce(value)
|
13
|
+
return value if value.nil?
|
14
|
+
case value.to_sym
|
15
|
+
when :integer
|
16
|
+
Fixnum
|
17
|
+
when :float
|
18
|
+
Float
|
19
|
+
when :decimal
|
20
|
+
BigDecimal
|
21
|
+
when :timestamp, :time
|
22
|
+
Time
|
23
|
+
when :datetime
|
24
|
+
DateTime
|
25
|
+
when :date
|
26
|
+
Date
|
27
|
+
when :text, :string, :binary
|
28
|
+
String
|
29
|
+
when :boolean
|
30
|
+
Axiom::Types::Boolean
|
31
|
+
when :hstore
|
32
|
+
Hash
|
33
|
+
else
|
34
|
+
registered(value) do
|
35
|
+
raise UnregisteredType.new "Database type '#{value}' is not a registered type.\nTo register use ActiveRecordSchemaScrapper::Attributes.register_type(name: :#{value}, klass: <RubyClass>)"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def registered(value)
|
41
|
+
if (a = Attributes.registered_types.detect { |name, klass| value.to_sym == name.to_sym })
|
42
|
+
a.last
|
43
|
+
else
|
44
|
+
yield
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
attribute :type, DBToRubyType
|
49
|
+
attribute :precision, Fixnum
|
50
|
+
attribute :scale, Fixnum
|
51
|
+
class DefaultValueType < Virtus::Attribute
|
52
|
+
def coerce(value)
|
53
|
+
case value.to_s
|
54
|
+
when 'f'
|
55
|
+
false
|
56
|
+
when 't'
|
57
|
+
true
|
58
|
+
else
|
59
|
+
registered(value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def registered(value)
|
64
|
+
if (r = Attributes.registered_defaults.detect { |name, _| value.to_s == name.to_s })
|
65
|
+
r.last
|
66
|
+
else
|
67
|
+
value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
attribute :default, DefaultValueType
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class ActiveRecordSchemaScrapper
|
2
|
+
class Attributes
|
3
|
+
|
4
|
+
def initialize(model:)
|
5
|
+
@model = model
|
6
|
+
end
|
7
|
+
|
8
|
+
include Enumerable
|
9
|
+
|
10
|
+
def each
|
11
|
+
call.each { |attr| yield(attr) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_a
|
15
|
+
map { |v| v }
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.register_type(name:, klass:)
|
19
|
+
registered_types << [name, klass]
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.registered_types
|
23
|
+
@registered_types ||= []
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.register_default(name:, klass:)
|
27
|
+
registered_defaults << [name, klass]
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.registered_defaults
|
31
|
+
@registered_defaults ||= []
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :model
|
37
|
+
|
38
|
+
def call
|
39
|
+
@attributes ||= model.columns_hash.map do |k, v|
|
40
|
+
ActiveRecordSchemaScrapper::Attribute.new(
|
41
|
+
name: k,
|
42
|
+
type: v.type,
|
43
|
+
precision: v.cast_type.precision,
|
44
|
+
limit: v.cast_type.limit,
|
45
|
+
scale: v.cast_type.scale,
|
46
|
+
default: v.default,
|
47
|
+
null: v.null,
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "active_record_schema_scrapper/version"
|
2
|
+
require "active_record_schema_scrapper/attribute"
|
3
|
+
require "active_record_schema_scrapper/attributes"
|
4
|
+
require "active_record_schema_scrapper/association"
|
5
|
+
require "active_record_schema_scrapper/associations"
|
6
|
+
|
7
|
+
class ActiveRecordSchemaScrapper
|
8
|
+
def initialize(model:, association_opts: {}, attribute_opts: {})
|
9
|
+
@model = model
|
10
|
+
@association_opts = association_opts.merge(model: model)
|
11
|
+
@attribute_opts = attribute_opts.merge(model: model)
|
12
|
+
end
|
13
|
+
|
14
|
+
def associations
|
15
|
+
@associations ||= Associations.new(association_opts)
|
16
|
+
end
|
17
|
+
|
18
|
+
def attributes
|
19
|
+
@attributes ||= Attributes.new(attribute_opts)
|
20
|
+
end
|
21
|
+
|
22
|
+
def table_name
|
23
|
+
model.table_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def abstract_class?
|
27
|
+
model.abstract_class? || false
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
attr_reader :model, :association_opts, :attribute_opts
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_schema_scrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dustin Zeisler
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: virtus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: appraisal
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.0'
|
111
|
+
description: A supporting library for active_mocker and wrapper around an active_record
|
112
|
+
model.
|
113
|
+
email:
|
114
|
+
- dustin@zeisler.net
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Appraisals
|
123
|
+
- CODE_OF_CONDUCT.md
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- active_record_schema_scrapper.gemspec
|
129
|
+
- bin/console
|
130
|
+
- bin/setup
|
131
|
+
- gemfiles/activerecord_4.0.gemfile
|
132
|
+
- gemfiles/activerecord_4.0.gemfile.lock
|
133
|
+
- gemfiles/activerecord_4.1.gemfile
|
134
|
+
- gemfiles/activerecord_4.1.gemfile.lock
|
135
|
+
- gemfiles/activerecord_4.2.gemfile
|
136
|
+
- gemfiles/activerecord_4.2.gemfile.lock
|
137
|
+
- lib/active_record_schema_scrapper.rb
|
138
|
+
- lib/active_record_schema_scrapper/association.rb
|
139
|
+
- lib/active_record_schema_scrapper/associations.rb
|
140
|
+
- lib/active_record_schema_scrapper/attribute.rb
|
141
|
+
- lib/active_record_schema_scrapper/attributes.rb
|
142
|
+
- lib/active_record_schema_scrapper/version.rb
|
143
|
+
homepage: https://github.com/zeisler/active_record_schema_scrapper
|
144
|
+
licenses:
|
145
|
+
- MIT
|
146
|
+
metadata: {}
|
147
|
+
post_install_message:
|
148
|
+
rdoc_options: []
|
149
|
+
require_paths:
|
150
|
+
- lib
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
requirements: []
|
162
|
+
rubyforge_project:
|
163
|
+
rubygems_version: 2.2.5
|
164
|
+
signing_key:
|
165
|
+
specification_version: 4
|
166
|
+
summary: A wrapper around an active_record model.
|
167
|
+
test_files: []
|