active_record_schema_scrapper 0.3.0 → 0.4.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: 71ac9fbebc69db610a118ff0b8653d5c3f205d24
|
4
|
+
data.tar.gz: 69018e554e6d26845da497baf003843eeda34e4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb40ede397f3378c6c3775d1fd1e26fb655e5bb1a4e3c8c8a9fe55f9965891021f2736708571f1916a5e678d6dea5dee24572b030d9bd4cfcaea9bc2126bb010
|
7
|
+
data.tar.gz: 8c1a428a4593e8b596948b22232f69066556ec9fe9b6ed6e9d9eeec2fc33ebb9fd06584412773e600c3dc67243a292e27f0d1d17d1b45be850b8cfad858d95eb
|
@@ -17,98 +17,51 @@ class ActiveRecordSchemaScrapper
|
|
17
17
|
limit: nil,
|
18
18
|
null: nil)
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
default = init_default({ default: default, cast_type: cast_type, type: type })
|
21
|
+
type = init_type(type, { type: type, cast_type: cast_type })
|
22
22
|
super
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
private
|
26
|
+
|
27
|
+
def init_type(name, attr_values)
|
28
|
+
type = match_abstract(:replacement_type, :type, Attributes.registered_types, attr_values)
|
29
|
+
if type.is_a?(String) || type.is_a?(Symbol)
|
30
|
+
raise UnregisteredType.new "Database type '#{attr_values[:type]}' is not a registered type.\nTo register use ActiveRecordSchemaScrapper::Attributes.register_type(name: :#{name}, klass: <RubyClass>)"
|
31
|
+
else
|
32
|
+
type
|
33
33
|
end
|
34
|
-
(registered_type && !registered_type.empty?) ? registered_type[1] : type
|
35
34
|
end
|
36
35
|
|
37
|
-
def init_default(
|
38
|
-
|
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
|
36
|
+
def init_default(attr_values)
|
37
|
+
match_abstract(:replacement_default, :default, Attributes.registered_defaults, attr_values)
|
47
38
|
end
|
48
39
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
when :float
|
57
|
-
Float
|
58
|
-
when :decimal
|
59
|
-
BigDecimal
|
60
|
-
when :timestamp, :time
|
61
|
-
Time
|
62
|
-
when :datetime
|
63
|
-
DateTime
|
64
|
-
when :date
|
65
|
-
Date
|
66
|
-
when :text, :string, :binary
|
67
|
-
String
|
68
|
-
when :boolean
|
69
|
-
Axiom::Types::Boolean
|
70
|
-
when :hstore
|
71
|
-
Hash
|
72
|
-
else
|
73
|
-
registered(value) do
|
74
|
-
raise UnregisteredType.new "Database type '#{value}' is not a registered type.\nTo register use ActiveRecordSchemaScrapper::Attributes.register_type(name: :#{value}, klass: <RubyClass>)"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
40
|
+
def match_abstract(replacement_key, default, registers, attr_values)
|
41
|
+
return unless attr_values[default]
|
42
|
+
top_ranked_match = nil
|
43
|
+
last_top_match_count = 0
|
44
|
+
registers.each do |register|
|
45
|
+
all_given = register.reject { |_, v| v == :not_given }.dup
|
46
|
+
all_given.delete(replacement_key)
|
78
47
|
|
79
|
-
|
80
|
-
|
81
|
-
a.last
|
82
|
-
else
|
83
|
-
yield
|
48
|
+
matches = all_given.map do |k, v|
|
49
|
+
attr_values.has_key?(k) ? (v === attr_values[k] || v == attr_values[k]) : true
|
84
50
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
attribute :scale, Fixnum
|
90
|
-
class DefaultValueType < Virtus::Attribute
|
91
|
-
def coerce(value)
|
92
|
-
return value unless value.is_a?(String) || value.is_a?(Symbol)
|
93
|
-
case value.to_s
|
94
|
-
when 'f'
|
95
|
-
false
|
96
|
-
when 't'
|
97
|
-
true
|
98
|
-
else
|
99
|
-
registered(value)
|
51
|
+
max_match_count = matches.inject(0) { |sum, bool| bool ? sum += 1 : sum -= 10 }
|
52
|
+
if max_match_count > 0 && max_match_count > last_top_match_count
|
53
|
+
last_top_match_count = max_match_count
|
54
|
+
top_ranked_match = register
|
100
55
|
end
|
101
56
|
end
|
102
57
|
|
103
|
-
|
104
|
-
if (r = Attributes.registered_defaults.detect { |name, _, _| name.to_s == value.to_s })
|
105
|
-
r[1]
|
106
|
-
else
|
107
|
-
value
|
108
|
-
end
|
109
|
-
end
|
58
|
+
top_ranked_match ? top_ranked_match[replacement_key] : attr_values[default]
|
110
59
|
end
|
111
|
-
|
60
|
+
|
61
|
+
attribute :type
|
62
|
+
attribute :precision, Fixnum
|
63
|
+
attribute :scale, Fixnum
|
64
|
+
attribute :default
|
112
65
|
attribute :cast_type
|
113
66
|
end
|
114
67
|
end
|
@@ -1,28 +1,78 @@
|
|
1
1
|
class ActiveRecordSchemaScrapper
|
2
2
|
class Attributes
|
3
|
+
DEFAULT_REGISTERED_TYPES = [
|
4
|
+
{ type: :integer, replacement_type: Integer },
|
5
|
+
{ type: :float, replacement_type: Float },
|
6
|
+
{ type: :decimal, replacement_type: BigDecimal },
|
7
|
+
{ type: Proc.new { |t| [:timestamp, :time].include?(t) }, replacement_type: Time },
|
8
|
+
{ type: :datetime, replacement_type: DateTime },
|
9
|
+
{ type: :date, replacement_type: Date },
|
10
|
+
{ type: Proc.new { |t| %i{text string binary}.include?(t) }, replacement_type: String },
|
11
|
+
{ type: :boolean, replacement_type: Axiom::Types::Boolean },
|
12
|
+
{ type: :hstore, replacement_type: Hash },
|
13
|
+
].freeze
|
14
|
+
|
15
|
+
DEFAULT_REGISTERED_DEFAULTS = [
|
16
|
+
{ default: Proc.new { |d| d == "t" }, replacement_default: true },
|
17
|
+
{ default: Proc.new { |d| d == "f" }, replacement_default: false }
|
18
|
+
].freeze
|
3
19
|
|
4
20
|
class << self
|
5
21
|
# @param [Symbol] name original type from schema
|
6
22
|
# @param [Object, Virtus::Attribute] klass a ruby type used to coerce values
|
7
23
|
# @param [Object#===, Proc#===] cast_type to be compared to the db schema returned value
|
8
|
-
def register_type(name:, klass:, cast_type:
|
9
|
-
registered_types <<
|
24
|
+
def register_type(name:, klass:, cast_type: :not_given)
|
25
|
+
registered_types << { type: name, replacement_type: klass, cast_type: cast_type }
|
10
26
|
end
|
11
27
|
|
12
28
|
def registered_types
|
13
|
-
@registered_types ||=
|
29
|
+
@registered_types ||= DEFAULT_REGISTERED_TYPES.dup
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset_registered_types
|
33
|
+
@registered_types = DEFAULT_REGISTERED_TYPES.dup
|
34
|
+
end
|
35
|
+
|
36
|
+
module DeprecatedRegisterDefaultKeywords
|
37
|
+
# @param [String] name original default value from schema
|
38
|
+
# @param [Object] klass the replacement value
|
39
|
+
# @param [Object#===, Proc#===] cast_type to be compared to the db schema returned value
|
40
|
+
# @param [Symbol] type matches the type from the schema
|
41
|
+
def register_default(**args)
|
42
|
+
if args.has_key?(:name)
|
43
|
+
deprecation_warning(:name, :default)
|
44
|
+
args[:default] = args.delete(:name)
|
45
|
+
end
|
46
|
+
if args.has_key?(:klass)
|
47
|
+
deprecation_warning(:klass, :replacement_default)
|
48
|
+
args[:replacement_default] = args.delete(:klass)
|
49
|
+
end
|
50
|
+
super(args)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def deprecation_warning(old_key, new_key)
|
56
|
+
puts "Deprecation warning ActiveRecordSchemaScrapper::Attributes.register_default: keyword `#{old_key}` is replaced by `#{new_key}`"
|
57
|
+
end
|
14
58
|
end
|
15
59
|
|
16
|
-
|
17
|
-
|
60
|
+
prepend DeprecatedRegisterDefaultKeywords
|
61
|
+
|
62
|
+
# @param [String] default original default value from schema
|
63
|
+
# @param [Object] replacement_default the replacement value
|
18
64
|
# @param [Object#===, Proc#===] cast_type to be compared to the db schema returned value
|
19
65
|
# @param [Symbol] type matches the type from the schema
|
20
|
-
def register_default(
|
21
|
-
registered_defaults <<
|
66
|
+
def register_default(default:, replacement_default:, cast_type: :not_given, type: :not_given)
|
67
|
+
registered_defaults << { default: default, replacement_default: replacement_default, cast_type: cast_type, type: type }
|
22
68
|
end
|
23
69
|
|
24
70
|
def registered_defaults
|
25
|
-
@registered_defaults ||=
|
71
|
+
@registered_defaults ||= DEFAULT_REGISTERED_DEFAULTS.dup
|
72
|
+
end
|
73
|
+
|
74
|
+
def reset_registered_defaults
|
75
|
+
@registered_defaults = DEFAULT_REGISTERED_DEFAULTS.dup
|
26
76
|
end
|
27
77
|
end
|
28
78
|
|
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.4.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-08-
|
11
|
+
date: 2016-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|