relationizer 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21ee2cd0fd6597dfd1dca5a5dbf1373cb47bf97d
4
- data.tar.gz: 0aa7d5f6331dde1fca3c8600b966a5fdcb7896c3
3
+ metadata.gz: 48cac1b36f04f43929750f209e07f9e2bda3c0ce
4
+ data.tar.gz: ae607d94583019150e2b0bd5961cb5d90a351143
5
5
  SHA512:
6
- metadata.gz: 5ffba7e872f1b15455e2e79609945e74de8705757a91b28d6f72ebee9f5d0bb8aec792640114df838b9071f3eed56cffc5303d822424537ce39860a7cbd3cdca
7
- data.tar.gz: 041d84c052407fb3fc16abced7eafe832bc73f3c7d64c6168c81b48808bbdb0aeccbebc553f0420de6b424a6bd6c41f26ba1c7da1e1f622d5bd13b70a6045694
6
+ metadata.gz: f4f8034c808c4dbecad7aa1ec1f13135b5b41090d4129a65b7531fb82d669b8ba226649f9890d97865f4502ba6c0b21cae64f1802e96cde8f5973b593f0382f8
7
+ data.tar.gz: 2a71e9995be92db05ea108977384484f6565b9b16c4e39ee3262c2dbee340cec0478bde738ed80704911262089e0c1dc559bd8183452e29dd7efc52dcee8ebc6
@@ -6,17 +6,23 @@ module Relationizer
6
6
  module Standard
7
7
  class ReasonlessTypeError < StandardError; end
8
8
 
9
- DEFAULT_TYPES = {
10
- Integer => :INT64,
11
- BigDecimal => :FLOAT64,
12
- Float => :FLOAT64,
13
- String => :STRING,
14
- TrueClass => :BOOL,
15
- FalseClass => :BOOL,
16
- Date => :DATE,
17
- Time => :TIMESTAMP,
18
- DateTime => :TIMESTAMP,
19
- Array => :ARRAY
9
+ KNOWN_TYPES = [:INT64, :FLOAT64, :STRING, :BOOL, :TIMESTAMP, :DATE]
10
+
11
+ DEFAULT_TYPES = -> (obj) {
12
+ case obj
13
+ when Integer then :INT64
14
+ when BigDecimal then :FLOAT64
15
+ when Float then :FLOAT64
16
+ when String then :STRING
17
+ when TrueClass then :BOOL
18
+ when FalseClass then :BOOL
19
+ when Time then :TIMESTAMP
20
+ when DateTime then :TIMESTAMP
21
+ when Date then :DATE
22
+ when Array then :ARRAY
23
+ else
24
+ nil
25
+ end
20
26
  }
21
27
 
22
28
  def create_relation_literal(schema, tuples)
@@ -49,7 +55,7 @@ module Relationizer
49
55
  raise ReasonlessTypeError.new("Ambiguous type of element in array: #{classes}")
50
56
  end
51
57
 
52
- DEFAULT_TYPES[classes.first] || :STRING
58
+ DEFAULT_TYPES[array.first] || :STRING
53
59
  end
54
60
 
55
61
  def types_exp(names, types)
@@ -76,16 +82,16 @@ module Relationizer
76
82
  if values.map { |o| o.is_a?(Array) }.all?
77
83
  types = values.
78
84
  map(&method(:array_type)).uniq.
79
- tap(&method(:many_candidate_check)).
80
- tap(&method(:empty_candidate_check))
85
+ tap(&method(:empty_candidate_check)).
86
+ tap(&method(:many_candidate_check))
81
87
 
82
88
  next "ARRAY<#{types.first}>".to_sym
83
89
  end
84
90
 
85
- values.map(&:class).uniq.
91
+ values.
86
92
  map(&DEFAULT_TYPES).compact.uniq.
87
- tap(&method(:many_candidate_check)).
88
93
  tap(&method(:empty_candidate_check)).
94
+ tap(&method(:many_candidate_check)).
89
95
  first || :STRING
90
96
  }
91
97
  end
@@ -99,7 +105,7 @@ module Relationizer
99
105
  obj.map { |e| to_literal(e, t) }.join(', ').tap { |s| break "[#{s}]"}
100
106
  when /^ARRAY\<.+\>$/
101
107
  t = /^ARRAY\<(.+)\>$/.match(type).to_a&.dig(1).to_sym
102
- raise "Unknown type: #{t}" unless DEFAULT_TYPES.values.include?(t)
108
+ raise "Unknown type: #{t}" unless KNOWN_TYPES.include?(t)
103
109
  obj.map { |e| to_literal(e, t) }.join(', ').tap { |s| break "[#{s}]"}
104
110
  when :TIMESTAMP
105
111
  %Q{'#{obj.strftime('%Y-%m-%d %H:%M:%S')}'}
@@ -5,16 +5,29 @@ module Relationizer
5
5
  module Postgresql
6
6
  class ReasonlessTypeError < StandardError; end
7
7
 
8
- DEFAULT_TYPES = {
9
- Integer => :INT8,
10
- BigDecimal => :DECIMAL,
11
- Float => :FLOAT8,
12
- String => :TEXT,
13
- TrueClass => :BOOLEAN,
14
- FalseClass => :BOOLEAN,
15
- Date => :DATE,
16
- Time => :TIMESTAMPTZ,
17
- DateTime => :TIMESTAMPTZ
8
+ DEFAULT_TYPES = -> (obj) {
9
+ case obj
10
+ when Integer
11
+ :INT8
12
+ when BigDecimal
13
+ :DECIMAL
14
+ when Float
15
+ :FLOAT8
16
+ when String
17
+ :TEXT
18
+ when TrueClass
19
+ :BOOLEAN
20
+ when FalseClass
21
+ :BOOLEAN
22
+ when Time
23
+ :TIMESTAMPTZ
24
+ when DateTime
25
+ :TIMESTAMPTZ
26
+ when Date
27
+ :DATE
28
+ else
29
+ nil
30
+ end
18
31
  }
19
32
 
20
33
  def create_relation_literal(schema, tuples)
@@ -39,10 +52,9 @@ module Relationizer
39
52
  next "#{name}::#{type.to_s.upcase}" if type
40
53
 
41
54
  values.
42
- map(&:class).uniq.
43
55
  map(&DEFAULT_TYPES).compact.uniq.
44
- tap(&method(:many_candidate_check)).
45
56
  tap(&method(:empty_candidate_check)).
57
+ tap(&method(:many_candidate_check)).
46
58
  first.
47
59
  to_s.upcase.
48
60
  tap { |fixed_type| break "#{name}::#{fixed_type}" }
@@ -1,3 +1,3 @@
1
1
  module Relationizer
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relationizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yancya