dropkiq 0.1.8 → 0.1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +41 -2
- data/lib/dropkiq.rb +2 -0
- data/lib/dropkiq/drop_class_analyzer.rb +7 -1
- data/lib/dropkiq/drop_method_analyzer.rb +15 -6
- data/lib/dropkiq/drop_method_instance_simulator.rb +58 -0
- data/lib/dropkiq/drop_method_name_classifier.rb +52 -0
- data/lib/dropkiq/tasks/dropkiq/schema.rake +4 -2
- data/lib/dropkiq/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79323cdebbbd66a06c273745adeec22144d81d84
|
4
|
+
data.tar.gz: 38fe7ff01b60bddac23b649811d22232fc18d902
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a3ea5a984d07fb2ea97ecb330f7f21a07f303c7a03ff015ea862fea5f378a544d3b75fb03a9a42a25c7beacb2a189210214ef813901ba92dcefd8718424ba41
|
7
|
+
data.tar.gz: 5e804c999f7f5505eda283bbe3b83a4f4af4f58e12644383bab045ea29e210e95a7b174f2297a3ead7ae2c9ef656a3d6a316a2fb0387feaf955bc60f41319793
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -30,7 +30,7 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
This Gem makes several assumptions about your Ruby on Rails application:
|
32
32
|
|
33
|
-
* Drop classes are expected to have the same name as the corresponding Rails model. For example, if you have an ActiveRecord model with name of `Person`, the drop class is expected to be called `PersonDrop`
|
33
|
+
* Drop classes are expected to have the same name as the corresponding Rails model. For example, if you have an ActiveRecord model with name of `Person`, the drop class is expected to be called `PersonDrop` (Ruby on Rails applications using the legacy `liquid_methods` method with [PR #568](https://github.com/Shopify/liquid/pull/568) are also supported.)
|
34
34
|
* Drop class methods are expected to return the same data type as the corresponding Rails-model getter methods for relationships and columns.
|
35
35
|
* This Gem has not been tested with Rails models that have non-default primary key (other than `id`).
|
36
36
|
|
@@ -44,7 +44,7 @@ bundle exec rake dropkiq:schema
|
|
44
44
|
|
45
45
|
You should now have a `db/dropkiq_schema.yaml` file. This file describes all tables associated to Drop classes in your application, along with all methods avaialble to the Drop class. It is important that you **DO** allow this file to be checked in to version control so that you can maintain your Dropkiq schema over time as you add/remove/modify your Drop classes.
|
46
46
|
|
47
|
-
Notice that `type` has NOT been set on all methods. The Dropkiq Gem is only able to infer the method type for methods that correspond to a column or relationship on the ActiveRecord model. Please take a moment to manually add `type` values for all methods that were not able to be inferred. Notice that if you run `bundle exec rake dropkiq:schema` again, your changes are saved!
|
47
|
+
Notice that `type` has NOT been set on all methods. The Dropkiq Gem is only able to infer the method type for methods that correspond to a column or relationship on the ActiveRecord model. Please take a moment to manually add `type` values for all methods that were not able to be inferred. (Hint: Search the dropkiq_schema.yaml file for the word "CHANGEME" to see what needs manual attention.) Notice that if you run `bundle exec rake dropkiq:schema` again, your changes are saved!
|
48
48
|
|
49
49
|
|
50
50
|
#### Ruby on Rails Column to Dropkiq Data Type Mappings
|
@@ -122,6 +122,45 @@ class ProductDrop < Liquid::Drop
|
|
122
122
|
end
|
123
123
|
```
|
124
124
|
|
125
|
+
#### Dropkiq Method Classification
|
126
|
+
|
127
|
+
In the event that a liquid method is not an exact match to a column or association, the Dropkiq Ruby Gem will attempt to make a best guess about the data type of the method based on the name. The rules for matching are as follows:
|
128
|
+
|
129
|
+
| Method Name Ends With | Dropkiq Data Type |
|
130
|
+
| --- | --- |
|
131
|
+
| `_id` | `ColumnTypes::Numeric` |
|
132
|
+
| `_count` | `ColumnTypes::Numeric` |
|
133
|
+
| `?` | `ColumnTypes::Boolean` |
|
134
|
+
| `_present` | `ColumnTypes::Boolean` |
|
135
|
+
| `_changed` | `ColumnTypes::Boolean` |
|
136
|
+
| `description` | `ColumnTypes::Text` |
|
137
|
+
| `name` | `ColumnTypes::String` |
|
138
|
+
| `password` | `ColumnTypes::String` |
|
139
|
+
| `type` | `ColumnTypes::String` |
|
140
|
+
| `title` | `ColumnTypes::String` |
|
141
|
+
| `to_s` | `ColumnTypes::String` |
|
142
|
+
| `to_string` | `ColumnTypes::String` |
|
143
|
+
| `_url` | `ColumnTypes::String` |
|
144
|
+
| `_email` | `ColumnTypes::String` |
|
145
|
+
| `_partial` | `ColumnTypes::String` |
|
146
|
+
| `_email_address` | `ColumnTypes::String` |
|
147
|
+
| `_uuid` | `ColumnTypes::String` |
|
148
|
+
|
149
|
+
The Dropkiq Ruby Gem will also attempt to classify liquid methods by creating a sample instance of the Liquid Drop class to test. The liquid method will be called, and the value will attempt to be classified using the foollowing:
|
150
|
+
|
151
|
+
| Value Result | Dropkiq Data Type |
|
152
|
+
| --- | --- |
|
153
|
+
| `TrueClass` | `ColumnTypes::Boolean` |
|
154
|
+
| `FalseClass` | `ColumnTypes::Boolean` |
|
155
|
+
| `String` | `ColumnTypes::String` |
|
156
|
+
| `Symbol` | `ColumnTypes::String` |
|
157
|
+
| `Numeric` | `ColumnTypes::Numeric` |
|
158
|
+
| `Date` | `ColumnTypes::DateTime` |
|
159
|
+
| `Time` | `ColumnTypes::DateTime` |
|
160
|
+
| `DateTime` | `ColumnTypes::DateTime` |
|
161
|
+
|
162
|
+
Dropkiq will attempt to classify relationships in the event the method returns a single ActiveRecord object (`ColumnTypes::HasOne`), or a collection or ActiveRecord objects (`ColumnTypes::HasMany`).
|
163
|
+
|
125
164
|
## Development
|
126
165
|
|
127
166
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/dropkiq.rb
CHANGED
@@ -7,6 +7,8 @@ require "dropkiq/constants"
|
|
7
7
|
|
8
8
|
require "dropkiq/drop_class_analyzer"
|
9
9
|
require "dropkiq/drop_method_analyzer"
|
10
|
+
require "dropkiq/drop_method_name_classifier"
|
11
|
+
require "dropkiq/drop_method_instance_simulator"
|
10
12
|
require 'dropkiq/railtie' if defined?(Rails)
|
11
13
|
|
12
14
|
module Dropkiq
|
@@ -52,8 +52,14 @@ module Dropkiq
|
|
52
52
|
default_methods = (Liquid::Drop.instance_methods + Object.instance_methods)
|
53
53
|
instance_methods = (liquid_drop_class.instance_methods - default_methods)
|
54
54
|
|
55
|
+
sample_instance = active_record_class.first
|
56
|
+
sample_drop = begin
|
57
|
+
liquid_drop_class.new(sample_instance)
|
58
|
+
rescue
|
59
|
+
end
|
60
|
+
|
55
61
|
instance_methods.inject({}) do |hash, method|
|
56
|
-
analyzer = Dropkiq::DropMethodAnalyzer.new(self, method)
|
62
|
+
analyzer = Dropkiq::DropMethodAnalyzer.new(self, method, sample_drop)
|
57
63
|
analyzer.analyze
|
58
64
|
hash.merge!(analyzer.to_param)
|
59
65
|
end.sort_by { |key| key }.to_h
|
@@ -1,13 +1,16 @@
|
|
1
1
|
module Dropkiq
|
2
2
|
class DropMethodAnalyzer
|
3
|
+
CHANGEME = "CHANGEME"
|
4
|
+
|
3
5
|
attr_accessor :drop_class_analyzer, :drop_method,
|
4
|
-
:dropkiq_type, :foreign_table_name
|
6
|
+
:dropkiq_type, :foreign_table_name, :sample_drop
|
5
7
|
|
6
8
|
delegate :active_record_class, to: :drop_class_analyzer
|
7
9
|
|
8
|
-
def initialize(drop_class_analyzer, drop_method)
|
10
|
+
def initialize(drop_class_analyzer, drop_method, sample_drop=nil)
|
9
11
|
self.drop_class_analyzer = drop_class_analyzer
|
10
12
|
self.drop_method = drop_method
|
13
|
+
self.sample_drop = sample_drop
|
11
14
|
end
|
12
15
|
|
13
16
|
def analyze
|
@@ -15,15 +18,21 @@ module Dropkiq
|
|
15
18
|
relationship_to_dropkiq_type_classifier
|
16
19
|
elsif is_column?
|
17
20
|
column_to_dropkiq_type_classifier
|
21
|
+
else
|
22
|
+
Dropkiq::DropMethodNameClassifier.new(drop_method).classify
|
23
|
+
end
|
24
|
+
|
25
|
+
if dropkiq_type.blank?
|
26
|
+
result = Dropkiq::DropMethodInstanceSimulator.new(drop_method, sample_drop).classify
|
27
|
+
self.dropkiq_type = result.dropkiq_type
|
28
|
+
self.foreign_table_name = result.foreign_table_name
|
18
29
|
end
|
19
30
|
end
|
20
31
|
|
21
32
|
def to_param
|
22
|
-
return {} if dropkiq_type.blank?
|
23
|
-
|
24
33
|
{
|
25
34
|
"#{drop_method}" => {
|
26
|
-
"type" => dropkiq_type,
|
35
|
+
"type" => (dropkiq_type.presence || CHANGEME),
|
27
36
|
"foreign_table_name" => foreign_table_name
|
28
37
|
}
|
29
38
|
}
|
@@ -45,7 +54,7 @@ module Dropkiq
|
|
45
54
|
begin
|
46
55
|
self.foreign_table_name = reflection.class_name.constantize.table_name
|
47
56
|
rescue
|
48
|
-
puts "WARNING: Could not find #{drop_method} on #{active_record_class.name}
|
57
|
+
puts "WARNING: Could not find #{drop_method} on #{active_record_class.name}"
|
49
58
|
return
|
50
59
|
end
|
51
60
|
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Dropkiq
|
2
|
+
class DropMethodInstanceSimulator
|
3
|
+
attr_accessor :drop_method, :sample_drop
|
4
|
+
attr_accessor :dropkiq_type, :foreign_table_name
|
5
|
+
|
6
|
+
def initialize(drop_method, sample_drop=nil)
|
7
|
+
self.drop_method = drop_method.to_s
|
8
|
+
self.sample_drop = sample_drop
|
9
|
+
end
|
10
|
+
|
11
|
+
def classify
|
12
|
+
value = begin
|
13
|
+
sample_drop.try(drop_method)
|
14
|
+
rescue
|
15
|
+
end
|
16
|
+
|
17
|
+
self.dropkiq_type = ruby_data_type_to_dropkiq_type(value)
|
18
|
+
self.dropkiq_type ||= test_for_relationship(value)
|
19
|
+
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def test_for_relationship(value)
|
26
|
+
if value.is_a?(ActiveRecord::Base)
|
27
|
+
self.foreign_table_name = value.class.table_name
|
28
|
+
return Dropkiq::HAS_ONE_TYPE
|
29
|
+
elsif value.respond_to?(:first) && value.first.is_a?(ActiveRecord::Base)
|
30
|
+
self.foreign_table_name = value.first.class.table_name
|
31
|
+
return Dropkiq::HAS_MANY_TYPE
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def ruby_data_type_to_dropkiq_type(value)
|
36
|
+
case value
|
37
|
+
when NilClass
|
38
|
+
when TrueClass
|
39
|
+
Dropkiq::BOOLEAN_TYPE
|
40
|
+
when FalseClass
|
41
|
+
Dropkiq::BOOLEAN_TYPE
|
42
|
+
when String
|
43
|
+
Dropkiq::STRING_TYPE
|
44
|
+
when Symbol
|
45
|
+
Dropkiq::STRING_TYPE
|
46
|
+
when Numeric
|
47
|
+
Dropkiq::NUMERIC_TYPE
|
48
|
+
when Date
|
49
|
+
Dropkiq::DATE_TIME_TYPE
|
50
|
+
when Time
|
51
|
+
Dropkiq::DATE_TIME_TYPE
|
52
|
+
when DateTime
|
53
|
+
Dropkiq::DATE_TIME_TYPE
|
54
|
+
else
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Dropkiq
|
2
|
+
class DropMethodNameClassifier
|
3
|
+
attr_accessor :drop_method
|
4
|
+
|
5
|
+
def initialize(drop_method)
|
6
|
+
self.drop_method = drop_method.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def classify
|
10
|
+
if numeric_type_match?
|
11
|
+
Dropkiq::NUMERIC_TYPE
|
12
|
+
elsif boolean_type_match?
|
13
|
+
Dropkiq::BOOLEAN_TYPE
|
14
|
+
elsif text_type_match?
|
15
|
+
Dropkiq::TEXT_TYPE
|
16
|
+
elsif string_type_match?
|
17
|
+
Dropkiq::STRING_TYPE
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def numeric_type_match?
|
24
|
+
drop_method.ends_with?("_id") ||
|
25
|
+
drop_method.ends_with?("_count")
|
26
|
+
end
|
27
|
+
|
28
|
+
def boolean_type_match?
|
29
|
+
drop_method.ends_with?("?") ||
|
30
|
+
drop_method.ends_with?("_present") ||
|
31
|
+
drop_method.ends_with?("_changed")
|
32
|
+
end
|
33
|
+
|
34
|
+
def text_type_match?
|
35
|
+
drop_method.ends_with?("description")
|
36
|
+
end
|
37
|
+
|
38
|
+
def string_type_match?
|
39
|
+
drop_method.ends_with?("name") ||
|
40
|
+
drop_method.ends_with?("password") ||
|
41
|
+
drop_method.ends_with?("type") ||
|
42
|
+
drop_method.ends_with?("title") ||
|
43
|
+
drop_method.ends_with?("to_s") ||
|
44
|
+
drop_method.ends_with?("to_string") ||
|
45
|
+
drop_method.ends_with?("_url") ||
|
46
|
+
drop_method.ends_with?("_email") ||
|
47
|
+
drop_method.ends_with?("_partial") ||
|
48
|
+
drop_method.ends_with?("_email_address") ||
|
49
|
+
drop_method.ends_with?("_uuid")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -11,8 +11,10 @@ namespace :dropkiq do
|
|
11
11
|
stdout, stderr = StringIO.new, StringIO.new
|
12
12
|
$stdout, $stderr = stdout, stderr
|
13
13
|
Dir.glob("#{Rails.root}#{Dropkiq::DEFAULT_MODEL_PATH}/**/*.rb").each do |f|
|
14
|
-
|
15
|
-
|
14
|
+
begin
|
15
|
+
load f
|
16
|
+
rescue
|
17
|
+
end
|
16
18
|
end
|
17
19
|
$stdout, $stderr = STDOUT, STDERR
|
18
20
|
|
data/lib/dropkiq/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dropkiq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Darrah
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -160,6 +160,8 @@ files:
|
|
160
160
|
- lib/dropkiq/constants.rb
|
161
161
|
- lib/dropkiq/drop_class_analyzer.rb
|
162
162
|
- lib/dropkiq/drop_method_analyzer.rb
|
163
|
+
- lib/dropkiq/drop_method_instance_simulator.rb
|
164
|
+
- lib/dropkiq/drop_method_name_classifier.rb
|
163
165
|
- lib/dropkiq/railtie.rb
|
164
166
|
- lib/dropkiq/tasks/dropkiq/schema.rake
|
165
167
|
- lib/dropkiq/version.rb
|