active_record_analyzer 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 +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +6 -0
- data/active_record_analyzer.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_record_analyzer/analyzer.rb +5 -0
- data/lib/active_record_analyzer/attribute/association.rb +9 -0
- data/lib/active_record_analyzer/attribute/simple.rb +9 -0
- data/lib/active_record_analyzer/attribute.rb +2 -0
- data/lib/active_record_analyzer/reflector/association.rb +22 -0
- data/lib/active_record_analyzer/reflector/simple.rb +35 -0
- data/lib/active_record_analyzer/reflector.rb +26 -0
- data/lib/active_record_analyzer/version.rb +3 -0
- data/lib/active_record_analyzer.rb +10 -0
- metadata +149 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 077c4ceaaef76f919c954c872faf2cdc116e28bc
|
4
|
+
data.tar.gz: 4e208e5ba80873246ee658f375e23667046e5251
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e5b31c24a97665b4945a5ab0654824afd6eb434df41f1873b59bfc837caeadf96afcd4dd5c3dadcc9ac90a5acb5fc98c0f7a822ce59e883c6f3ba403288076e
|
7
|
+
data.tar.gz: 2b894968e7180d3a7505e34aeb0fd90f3fd0d90b3d6012648e5a94b3df9b13ee2904fc9f0fd87faf85851ea59ba1e4eb2760325f14066b75c5128ddda572927a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ariel Scherman
|
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,77 @@
|
|
1
|
+
# ActiveRecordAnalyzer
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_record_analyzer`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'active_record_analyzer'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install active_record_analyzer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You can analyze your ActiveRecord model with the analyzer:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
analyzer = ActiveRecordAnalyzer::Analyzer.new(User)
|
29
|
+
```
|
30
|
+
|
31
|
+
With the analyzer in place, given an attribute's name, you can get its type for the given class (`User` in the example):
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
attribute = analyzer.reflect(:name)
|
35
|
+
attribute.association? # => false
|
36
|
+
attribute.simple_attribute? # => true
|
37
|
+
```
|
38
|
+
|
39
|
+
### Simple attributes
|
40
|
+
|
41
|
+
A **Simple Attribute** means basically every attribute except of foreign keys.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
analyzer.reflect(:name).simple_attribute? # => true
|
45
|
+
analyzer.reflect(:created_at).simple_attribute? # => true
|
46
|
+
analyzer.reflect(:company_id).simple_attribute? # => false
|
47
|
+
analyzer.reflect(:company).simple_attribute? # => false
|
48
|
+
analyzer.reflect(:accounts).simple_attribute? # => false
|
49
|
+
```
|
50
|
+
|
51
|
+
### Associations
|
52
|
+
|
53
|
+
An **Association** means foreign keys and association names.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
analyzer.reflect(:name).simple_attribute? # => false
|
57
|
+
analyzer.reflect(:created_at).simple_attribute? # => false
|
58
|
+
analyzer.reflect(:company_id).simple_attribute? # => true
|
59
|
+
analyzer.reflect(:company).simple_attribute? # => true
|
60
|
+
analyzer.reflect(:accounts).simple_attribute? # => true
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
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.
|
66
|
+
|
67
|
+
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_analyzer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
72
|
+
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
77
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
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_analyzer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_record_analyzer"
|
8
|
+
spec.version = ActiveRecordAnalyzer::VERSION
|
9
|
+
spec.authors = ["Ariel Scherman"]
|
10
|
+
spec.email = ["arielscherman@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Analyze an ActiveRecord class to get information about its associations.}
|
13
|
+
spec.description = %q{Analyze an ActiveRecord class to get information about its associations. You will be able to know all its has_many and belongs_to for instance.}
|
14
|
+
spec.homepage = "https://github.com/arielscherman/active_record_analyzer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "activerecord"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
spec.add_development_dependency "sqlite3"
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_record_analyzer"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class ActiveRecordAnalyzer::Reflector::Association
|
2
|
+
def initialize(klass)
|
3
|
+
@klass = klass
|
4
|
+
end
|
5
|
+
|
6
|
+
def has_attribute?(attribute)
|
7
|
+
attributes.include?(attribute.to_sym)
|
8
|
+
end
|
9
|
+
|
10
|
+
def attribute_type
|
11
|
+
@attribute_type ||= ActiveRecordAnalyzer::Attribute::Association
|
12
|
+
end
|
13
|
+
|
14
|
+
# This returns foreign_keys and association names (ex: :company_id and :company).
|
15
|
+
def attributes
|
16
|
+
@attributes ||= begin
|
17
|
+
@klass.reflect_on_all_associations.map do |assoc|
|
18
|
+
[assoc.foreign_key.to_sym, assoc.name]
|
19
|
+
end.flatten
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class ActiveRecordAnalyzer::Reflector::Simple
|
2
|
+
def initialize(klass)
|
3
|
+
@klass = klass
|
4
|
+
end
|
5
|
+
|
6
|
+
def has_attribute?(attribute)
|
7
|
+
attributes.include?(attribute.to_sym)
|
8
|
+
end
|
9
|
+
|
10
|
+
def attribute_type
|
11
|
+
@attribute_type ||= ActiveRecordAnalyzer::Attribute::Simple
|
12
|
+
end
|
13
|
+
|
14
|
+
# This returns attributes which are not foreign keys (ex: :name, but not :company_id)
|
15
|
+
#
|
16
|
+
def attributes
|
17
|
+
attributes_except_foreign_keys
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def attributes_except_foreign_keys
|
23
|
+
@attributes ||= begin
|
24
|
+
all_attribute_names.select { |attr| !foreign_keys.include?(attr) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def all_attribute_names
|
29
|
+
@all_attribute_names ||= @klass.attribute_names.map(&:to_sym)
|
30
|
+
end
|
31
|
+
|
32
|
+
def foreign_keys
|
33
|
+
@foreign_keys ||= ActiveRecordAnalyzer::Reflector::Association.new(@klass).attributes
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class ActiveRecordAnalyzer::Reflector
|
2
|
+
REFLECTORS = %i(association simple).freeze
|
3
|
+
|
4
|
+
def initialize(klass)
|
5
|
+
@klass = klass
|
6
|
+
end
|
7
|
+
|
8
|
+
# Asks every reflector if they find the given attribute in the class,
|
9
|
+
# and returns the attribute type (if any matched).
|
10
|
+
#
|
11
|
+
def reflect(attribute)
|
12
|
+
reflectors.find do |reflector|
|
13
|
+
reflector.has_attribute?(attribute)
|
14
|
+
end.try(:attribute_type)
|
15
|
+
end
|
16
|
+
|
17
|
+
def reflectors
|
18
|
+
REFLECTORS.map { |reflector_name| find_reflector(reflector_name).new(@klass) }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def find_reflector(reflector_name)
|
24
|
+
"ActiveRecordAnalyzer::Reflector::#{reflector_name.to_s.classify}".constantize
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "active_record_analyzer/version"
|
2
|
+
require "active_record_analyzer/attribute"
|
3
|
+
require "active_record_analyzer/attribute/simple"
|
4
|
+
require "active_record_analyzer/reflector"
|
5
|
+
require "active_record_analyzer/reflector/association"
|
6
|
+
require "active_record_analyzer/reflector/simple"
|
7
|
+
require "active_record_analyzer/analyzer"
|
8
|
+
|
9
|
+
module ActiveRecordAnalyzer
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_analyzer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ariel Scherman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '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.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
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.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Analyze an ActiveRecord class to get information about its associations.
|
98
|
+
You will be able to know all its has_many and belongs_to for instance.
|
99
|
+
email:
|
100
|
+
- arielscherman@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- active_record_analyzer.gemspec
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- lib/active_record_analyzer.rb
|
116
|
+
- lib/active_record_analyzer/analyzer.rb
|
117
|
+
- lib/active_record_analyzer/attribute.rb
|
118
|
+
- lib/active_record_analyzer/attribute/association.rb
|
119
|
+
- lib/active_record_analyzer/attribute/simple.rb
|
120
|
+
- lib/active_record_analyzer/reflector.rb
|
121
|
+
- lib/active_record_analyzer/reflector/association.rb
|
122
|
+
- lib/active_record_analyzer/reflector/simple.rb
|
123
|
+
- lib/active_record_analyzer/version.rb
|
124
|
+
homepage: https://github.com/arielscherman/active_record_analyzer
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.4.5.1
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Analyze an ActiveRecord class to get information about its associations.
|
148
|
+
test_files: []
|
149
|
+
has_rdoc:
|