active_record_schema_scrapper 0.2.2 → 0.3.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9192c10faa23a478681dcb27ee417137a5c87fb0
|
4
|
+
data.tar.gz: 5aab4259565e9e2bceeac4e99657ed2b2741513c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c2da7d9a5ff97a8e1d740e8ffbc74b02aeb67c6d666d0852cbcb5add1dab58e9a73ec9e47df8edefe6076ae2970b834a2c6326be16cd34913482abcc7877d13
|
7
|
+
data.tar.gz: d17eb5518af3df8dd980fdf5e2c423ae831473c5cb87a8ff42a2a5f2672526bfe1f1c420cdb3a7d234f2cefe4bbb842e771f7215733ee809f6a74bfde6ab3a8c
|
data/README.md
CHANGED
@@ -40,6 +40,36 @@ Or install it yourself as:
|
|
40
40
|
#=> []
|
41
41
|
```
|
42
42
|
|
43
|
+
#### Registering Types
|
44
|
+
|
45
|
+
This gem does not include all the mappings from any DB type to a Ruby type. (most common types are included)
|
46
|
+
So any arbitrary type can be registered.
|
47
|
+
|
48
|
+
# Postgres Array Type
|
49
|
+
```ruby
|
50
|
+
|
51
|
+
# schema.rb
|
52
|
+
#...
|
53
|
+
t.string :tags, array: true, default: []
|
54
|
+
#...
|
55
|
+
|
56
|
+
# initilizers/active_mocker.rb
|
57
|
+
ActiveRecordSchemaScrapper::Attributes.register_type(
|
58
|
+
name: :string,
|
59
|
+
klass: Array[String],
|
60
|
+
cast_type: -> (c) { c.class.name.include?("Array") }
|
61
|
+
)
|
62
|
+
|
63
|
+
ActiveRecordSchemaScrapper::Attributes.register_default(
|
64
|
+
name: "{}",
|
65
|
+
klass: [],
|
66
|
+
cast_type: -> (c) { c.class.name.include?("Array") }
|
67
|
+
)
|
68
|
+
|
69
|
+
ActiveRecordSchemaScrapper::Attributes.new(model: User).map(&:to_h)
|
70
|
+
#=>[{ name: "tags", type: Array[String], default: [] }]
|
71
|
+
```
|
72
|
+
|
43
73
|
### Associations
|
44
74
|
```ruby
|
45
75
|
ActiveRecordSchemaScrapper::Associations.new(model: User).map(&:to_h)
|
@@ -8,9 +8,48 @@ class ActiveRecordSchemaScrapper
|
|
8
8
|
include Virtus.model
|
9
9
|
attribute :name
|
10
10
|
|
11
|
+
def initialize(type: nil,
|
12
|
+
precision: nil,
|
13
|
+
scale: nil,
|
14
|
+
default: nil,
|
15
|
+
cast_type: nil,
|
16
|
+
name: nil,
|
17
|
+
limit: nil,
|
18
|
+
null: nil)
|
19
|
+
|
20
|
+
type = init_type(type, cast_type)
|
21
|
+
default = init_default(default, cast_type, type)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def init_type(type, cast_type)
|
26
|
+
return type unless type
|
27
|
+
registered_type = Attributes.registered_types.detect do |reg_type, klass, reg_cast_type|
|
28
|
+
if type.to_sym == reg_type.to_sym && reg_cast_type && reg_cast_type === cast_type
|
29
|
+
klass
|
30
|
+
elsif type.to_sym == reg_type.to_sym
|
31
|
+
klass
|
32
|
+
end
|
33
|
+
end
|
34
|
+
(registered_type && !registered_type.empty?) ? registered_type[1] : type
|
35
|
+
end
|
36
|
+
|
37
|
+
def init_default(default, cast_type, type)
|
38
|
+
return default unless default
|
39
|
+
registered_default = Attributes.registered_defaults.detect do |reg_default, klass, reg_cast_type, reg_type|
|
40
|
+
if (default.to_s == reg_default.to_s) && ((reg_cast_type && reg_cast_type === cast_type) || (type === reg_type))
|
41
|
+
klass
|
42
|
+
elsif default.to_s == reg_default.to_s
|
43
|
+
klass
|
44
|
+
end
|
45
|
+
end
|
46
|
+
(registered_default && !registered_default.empty?) ? registered_default[1] : default
|
47
|
+
end
|
48
|
+
|
11
49
|
class DBToRubyType < Virtus::Attribute
|
12
50
|
def coerce(value)
|
13
51
|
return value if value.nil?
|
52
|
+
return value unless value.is_a?(String) || value.is_a?(Symbol)
|
14
53
|
case value.to_sym
|
15
54
|
when :integer
|
16
55
|
Fixnum
|
@@ -38,7 +77,7 @@ class ActiveRecordSchemaScrapper
|
|
38
77
|
end
|
39
78
|
|
40
79
|
def registered(value)
|
41
|
-
if (a = Attributes.registered_types.detect { |name,
|
80
|
+
if (a = Attributes.registered_types.detect { |name, _, _| value.to_sym == name.to_sym })
|
42
81
|
a.last
|
43
82
|
else
|
44
83
|
yield
|
@@ -50,6 +89,7 @@ class ActiveRecordSchemaScrapper
|
|
50
89
|
attribute :scale, Fixnum
|
51
90
|
class DefaultValueType < Virtus::Attribute
|
52
91
|
def coerce(value)
|
92
|
+
return value unless value.is_a?(String) || value.is_a?(Symbol)
|
53
93
|
case value.to_s
|
54
94
|
when 'f'
|
55
95
|
false
|
@@ -61,13 +101,14 @@ class ActiveRecordSchemaScrapper
|
|
61
101
|
end
|
62
102
|
|
63
103
|
def registered(value)
|
64
|
-
if (r = Attributes.registered_defaults.detect { |name, _|
|
65
|
-
r
|
104
|
+
if (r = Attributes.registered_defaults.detect { |name, _, _| name.to_s == value.to_s })
|
105
|
+
r[1]
|
66
106
|
else
|
67
107
|
value
|
68
108
|
end
|
69
109
|
end
|
70
110
|
end
|
71
111
|
attribute :default, DefaultValueType
|
112
|
+
attribute :cast_type
|
72
113
|
end
|
73
|
-
end
|
114
|
+
end
|
@@ -2,16 +2,23 @@ class ActiveRecordSchemaScrapper
|
|
2
2
|
class Attributes
|
3
3
|
|
4
4
|
class << self
|
5
|
-
|
6
|
-
|
5
|
+
# @param [Symbol] name original type from schema
|
6
|
+
# @param [Object, Virtus::Attribute] klass a ruby type used to coerce values
|
7
|
+
# @param [Object#===, Proc#===] cast_type to be compared to the db schema returned value
|
8
|
+
def register_type(name:, klass:, cast_type: nil)
|
9
|
+
registered_types << [name, klass, cast_type]
|
7
10
|
end
|
8
11
|
|
9
12
|
def registered_types
|
10
13
|
@registered_types ||= []
|
11
14
|
end
|
12
15
|
|
13
|
-
|
14
|
-
|
16
|
+
# @param [String] name original default value from schema
|
17
|
+
# @param [Object] klass the replacement value
|
18
|
+
# @param [Object#===, Proc#===] cast_type to be compared to the db schema returned value
|
19
|
+
# @param [Symbol] type matches the type from the schema
|
20
|
+
def register_default(name:, klass:, cast_type: nil, type: nil)
|
21
|
+
registered_defaults << [name, klass, cast_type, type]
|
15
22
|
end
|
16
23
|
|
17
24
|
def registered_defaults
|
@@ -49,24 +56,33 @@ class ActiveRecordSchemaScrapper
|
|
49
56
|
scale: v.scale,
|
50
57
|
default: v.default,
|
51
58
|
null: v.null,
|
59
|
+
cast_type: cast_type(v)
|
52
60
|
)
|
53
61
|
end
|
54
62
|
rescue NoMethodError => e
|
55
63
|
@errors << ErrorObject.new(class_name: model.name,
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
64
|
+
message: "#{model.name} is not a valid ActiveRecord model.",
|
65
|
+
original_error: e,
|
66
|
+
level: :error,
|
67
|
+
type: :invalid_model)
|
60
68
|
[]
|
61
69
|
rescue ActiveRecord::StatementInvalid => e
|
62
70
|
level = model.abstract_class? ? :warn : :error
|
63
71
|
message = model.abstract_class? ? "#{model.name} is an abstract class and has no associated table." : e.message
|
64
72
|
@errors << ErrorObject.new(class_name: model.name,
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
73
|
+
message: message,
|
74
|
+
original_error: e,
|
75
|
+
level: level,
|
76
|
+
type: :no_table)
|
69
77
|
[]
|
70
78
|
end
|
79
|
+
|
80
|
+
def cast_type(v)
|
81
|
+
if v.respond_to?(:cast_type)
|
82
|
+
v.cast_type
|
83
|
+
elsif v.respond_to?(:sql_type_metadata)
|
84
|
+
v.sql_type_metadata
|
85
|
+
end
|
86
|
+
end
|
71
87
|
end
|
72
88
|
end
|
@@ -1,23 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
attr_reader :level, :message, :class_name, :type, :original_error
|
4
|
-
|
5
|
-
def initialize(level: :warn, message:, class_name:, type:, original_error: nil)
|
6
|
-
@level = level
|
7
|
-
@message = message
|
8
|
-
@class_name = class_name
|
9
|
-
@type = type
|
10
|
-
@original_error = original_error
|
11
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "virtus"
|
12
3
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
4
|
+
class ActiveRecordSchemaScrapper
|
5
|
+
if defined?(ActiveMocker::ErrorObject)
|
6
|
+
ErrorObject = Class.new(ActiveMocker::ErrorObject)
|
7
|
+
else
|
8
|
+
class ErrorObject
|
9
|
+
include Virtus.model
|
10
|
+
attribute :message
|
11
|
+
attribute :level
|
12
|
+
attribute :original_error
|
13
|
+
attribute :type
|
14
|
+
attribute :class_name
|
21
15
|
end
|
22
16
|
end
|
23
|
-
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_schema_scrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|