active_record_analyzer 0.1.0 → 0.2.0

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: 077c4ceaaef76f919c954c872faf2cdc116e28bc
4
- data.tar.gz: 4e208e5ba80873246ee658f375e23667046e5251
3
+ metadata.gz: c0dd5291013bb06400e6ad9fbd0f67f6caef9248
4
+ data.tar.gz: c5cd455a34119a916e5742e7d86b9d66d83ae16d
5
5
  SHA512:
6
- metadata.gz: 2e5b31c24a97665b4945a5ab0654824afd6eb434df41f1873b59bfc837caeadf96afcd4dd5c3dadcc9ac90a5acb5fc98c0f7a822ce59e883c6f3ba403288076e
7
- data.tar.gz: 2b894968e7180d3a7505e34aeb0fd90f3fd0d90b3d6012648e5a94b3df9b13ee2904fc9f0fd87faf85851ea59ba1e4eb2760325f14066b75c5128ddda572927a
6
+ metadata.gz: ad85a3d38216069edfaf2d668fd1cc55541e244977b1361902a6a44a291b25dba418801d8f2a3d0f107110c947b6d12a671048b52c348e38c81d8eb0721a246b
7
+ data.tar.gz: 77da4f15e4e94be3ec84ac26b8977946145d367f4a4384b58063381784e41a524ccb5b10920aa9cb3effd5660c37cc1e018aab8216973ecdb4ae0aa83ac49f6a
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # ChangeLog
2
+
3
+ ## 0.2.0 (2018-02-25)
4
+
5
+ - Add specific associations reflections (has_many and belongs_to).
data/README.md CHANGED
@@ -53,11 +53,27 @@ analyzer.reflect(:accounts).simple_attribute? # => false
53
53
  An **Association** means foreign keys and association names.
54
54
 
55
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
56
+ analyzer.reflect(:name).association? # => false
57
+ analyzer.reflect(:created_at).association? # => false
58
+ analyzer.reflect(:company_id).association? # => true
59
+ analyzer.reflect(:company).association? # => true
60
+ analyzer.reflect(:accounts).association? # => true
61
+ ```
62
+
63
+ #### belongs_to (One to Many)
64
+
65
+ ```ruby
66
+ analyzer.reflect(:company_id).one_to_many? # => true
67
+ analyzer.reflect(:company).one_to_many? # => true
68
+ analyzer.reflect(:accounts).one_to_many? # => false
69
+ ```
70
+
71
+ #### has_many
72
+
73
+ ```ruby
74
+ analyzer.reflect(:company_id).has_many? # => false
75
+ analyzer.reflect(:company).has_many? # => false
76
+ analyzer.reflect(:accounts).has_many? # => true
61
77
  ```
62
78
 
63
79
  ## Development
@@ -21,11 +21,11 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency "activerecord"
24
+ spec.add_dependency "activerecord", ">= 4.0"
25
25
 
26
26
  spec.add_development_dependency "bundler", "~> 1.14"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
- spec.add_development_dependency "pry"
30
- spec.add_development_dependency "sqlite3"
29
+ spec.add_development_dependency "pry", ">= 0.11"
30
+ spec.add_development_dependency "sqlite3", ">= 1.3"
31
31
  end
@@ -0,0 +1,17 @@
1
+ class ActiveRecordAnalyzer::Attribute::BelongsTo
2
+ def association?
3
+ true
4
+ end
5
+
6
+ def simple_attribute?
7
+ false
8
+ end
9
+
10
+ def has_many?
11
+ false
12
+ end
13
+
14
+ def one_to_many?
15
+ true
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class ActiveRecordAnalyzer::Attribute::BelongsTo
2
+ def association?
3
+ true
4
+ end
5
+
6
+ def simple_attribute?
7
+ false
8
+ end
9
+
10
+ def has_many?
11
+ true
12
+ end
13
+
14
+ def one_to_many?
15
+ false
16
+ end
17
+ end
@@ -6,4 +6,12 @@ class ActiveRecordAnalyzer::Attribute::Simple
6
6
  def simple_attribute?
7
7
  true
8
8
  end
9
+
10
+ def has_many?
11
+ false
12
+ end
13
+
14
+ def one_to_many?
15
+ false
16
+ end
9
17
  end
@@ -0,0 +1,32 @@
1
+ class ActiveRecordAnalyzer::Reflector::BelongsTo
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::BelongsTo
12
+ end
13
+
14
+ # This returns foreign_keys and association names (ex: :company_id and :company).
15
+ def attributes
16
+ @attributes ||= begin
17
+ belongs_to_associations.map do |assoc|
18
+ [assoc.foreign_key.to_sym, assoc.name]
19
+ end.flatten
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def belongs_to_associations
26
+ @belongs_to_associations ||= begin
27
+ @klass.reflect_on_all_associations.select do |assoc|
28
+ assoc.is_a?(ActiveRecord::Reflection::BelongsToReflection)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ class ActiveRecordAnalyzer::Reflector::HasMany
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::HasMany
12
+ end
13
+
14
+ # This returns foreign_keys and association names (ex: :company_id and :company).
15
+ def attributes
16
+ @attributes ||= has_many_associations.map { |assoc| assoc.name }
17
+ end
18
+
19
+ private
20
+
21
+ def has_many_associations
22
+ @belongs_to_associations ||= begin
23
+ @klass.reflect_on_all_associations.select do |assoc|
24
+ assoc.is_a?(ActiveRecord::Reflection::HasManyReflection)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -30,6 +30,6 @@ class ActiveRecordAnalyzer::Reflector::Simple
30
30
  end
31
31
 
32
32
  def foreign_keys
33
- @foreign_keys ||= ActiveRecordAnalyzer::Reflector::Association.new(@klass).attributes
33
+ @foreign_keys ||= ActiveRecordAnalyzer::Reflector::BelongsTo.new(@klass).attributes
34
34
  end
35
35
  end
@@ -1,5 +1,5 @@
1
1
  class ActiveRecordAnalyzer::Reflector
2
- REFLECTORS = %i(association simple).freeze
2
+ REFLECTORS = %i(belongs_to has_many simple).freeze
3
3
 
4
4
  def initialize(klass)
5
5
  @klass = klass
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordAnalyzer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,7 +2,8 @@ require "active_record_analyzer/version"
2
2
  require "active_record_analyzer/attribute"
3
3
  require "active_record_analyzer/attribute/simple"
4
4
  require "active_record_analyzer/reflector"
5
- require "active_record_analyzer/reflector/association"
5
+ require "active_record_analyzer/reflector/belongs_to"
6
+ require "active_record_analyzer/reflector/has_many"
6
7
  require "active_record_analyzer/reflector/simple"
7
8
  require "active_record_analyzer/analyzer"
8
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ariel Scherman
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -72,28 +72,28 @@ dependencies:
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '0.11'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '0.11'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '1.3'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '1.3'
97
97
  description: Analyze an ActiveRecord class to get information about its associations.
98
98
  You will be able to know all its has_many and belongs_to for instance.
99
99
  email:
@@ -105,6 +105,7 @@ files:
105
105
  - ".gitignore"
106
106
  - ".rspec"
107
107
  - ".travis.yml"
108
+ - CHANGELOG.md
108
109
  - Gemfile
109
110
  - LICENSE.txt
110
111
  - README.md
@@ -115,10 +116,12 @@ files:
115
116
  - lib/active_record_analyzer.rb
116
117
  - lib/active_record_analyzer/analyzer.rb
117
118
  - lib/active_record_analyzer/attribute.rb
118
- - lib/active_record_analyzer/attribute/association.rb
119
+ - lib/active_record_analyzer/attribute/belongs_to.rb
120
+ - lib/active_record_analyzer/attribute/has_many.rb
119
121
  - lib/active_record_analyzer/attribute/simple.rb
120
122
  - lib/active_record_analyzer/reflector.rb
121
- - lib/active_record_analyzer/reflector/association.rb
123
+ - lib/active_record_analyzer/reflector/belongs_to.rb
124
+ - lib/active_record_analyzer/reflector/has_many.rb
122
125
  - lib/active_record_analyzer/reflector/simple.rb
123
126
  - lib/active_record_analyzer/version.rb
124
127
  homepage: https://github.com/arielscherman/active_record_analyzer
@@ -146,4 +149,3 @@ signing_key:
146
149
  specification_version: 4
147
150
  summary: Analyze an ActiveRecord class to get information about its associations.
148
151
  test_files: []
149
- has_rdoc:
@@ -1,9 +0,0 @@
1
- class ActiveRecordAnalyzer::Attribute::Association
2
- def association?
3
- true
4
- end
5
-
6
- def simple_attribute?
7
- false
8
- end
9
- end
@@ -1,22 +0,0 @@
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