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: 9192c10faa23a478681dcb27ee417137a5c87fb0
4
- data.tar.gz: 5aab4259565e9e2bceeac4e99657ed2b2741513c
3
+ metadata.gz: 71ac9fbebc69db610a118ff0b8653d5c3f205d24
4
+ data.tar.gz: 69018e554e6d26845da497baf003843eeda34e4d
5
5
  SHA512:
6
- metadata.gz: 8c2da7d9a5ff97a8e1d740e8ffbc74b02aeb67c6d666d0852cbcb5add1dab58e9a73ec9e47df8edefe6076ae2970b834a2c6326be16cd34913482abcc7877d13
7
- data.tar.gz: d17eb5518af3df8dd980fdf5e2c423ae831473c5cb87a8ff42a2a5f2672526bfe1f1c420cdb3a7d234f2cefe4bbb842e771f7215733ee809f6a74bfde6ab3a8c
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
- type = init_type(type, cast_type)
21
- default = init_default(default, cast_type, type)
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
- 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
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(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
36
+ def init_default(attr_values)
37
+ match_abstract(:replacement_default, :default, Attributes.registered_defaults, attr_values)
47
38
  end
48
39
 
49
- class DBToRubyType < Virtus::Attribute
50
- def coerce(value)
51
- return value if value.nil?
52
- return value unless value.is_a?(String) || value.is_a?(Symbol)
53
- case value.to_sym
54
- when :integer
55
- Fixnum
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
- def registered(value)
80
- if (a = Attributes.registered_types.detect { |name, _, _| value.to_sym == name.to_sym })
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
- end
86
- end
87
- attribute :type, DBToRubyType
88
- attribute :precision, Fixnum
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
- def registered(value)
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
- attribute :default, DefaultValueType
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: nil)
9
- registered_types << [name, klass, cast_type]
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
- # @param [String] name original default value from schema
17
- # @param [Object] klass the replacement value
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(name:, klass:, cast_type: nil, type: nil)
21
- registered_defaults << [name, klass, cast_type, type]
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
 
@@ -1,3 +1,3 @@
1
1
  class ActiveRecordSchemaScrapper
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  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.3.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-09 00:00:00.000000000 Z
11
+ date: 2016-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord