bigqueryid 0.5.2 → 0.5.3

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: 70c88dd1296fdab6ac0f993a6aee623eeec9a186
4
- data.tar.gz: 61698805be2a892b6668d8564ab8b1d843410004
3
+ metadata.gz: 581f61be916e1263baf264135277f138768453ce
4
+ data.tar.gz: 1fa8ba7020e174a3f4e6655009834fb71fed0b3d
5
5
  SHA512:
6
- metadata.gz: f1f311616135eb0b4007070f3cf6b945fa8e5f594ffec55ff3ef56f93b6aae2ad8c17f42c8bb830db68e0b7041e43a50b252e71720ad05af37ddb4951a1e980a
7
- data.tar.gz: b0bd58efb62b7622929ed0c237cbe12d92b648ae07d273496bce07a57652d5e94512984e6ba73145dc290c6024253098d353f420b57df7d9f4b301b6cd4b108e
6
+ metadata.gz: aae16fa93c3f03338b9bce0bcd1aab0f09b38123b856d4112a79f6d0c337e8033b878e796b845d1455cfcc2896139eb6eb2d9795ea36401597067f5396508545
7
+ data.tar.gz: 5085c1e1c1d0aec450b390893236bccf0b0550d74fec3f792e93658445e690080e0cba8dce8da5ae1cb0fb3ddcd63724f9161fbd52abae8ff027af6b07f7ba2b
@@ -5,10 +5,12 @@ module Bigqueryid
5
5
 
6
6
  included do
7
7
  class_attribute :attributes
8
+ class_attribute :coercer
8
9
 
9
10
  self.attributes = {}
11
+ self.coercer = Coercer.new(attributes)
10
12
 
11
- field :id
13
+ field :id, type: String
12
14
 
13
15
  def attributes=(attributes)
14
16
  if attributes.is_a? ::Hash
@@ -42,64 +44,6 @@ module Bigqueryid
42
44
  end
43
45
  hash.sort.to_h
44
46
  end
45
-
46
- def coercer
47
- @coercer ||= Coercible::Coercer.new
48
- end
49
-
50
- def coerce(value, type)
51
- if value.nil? || !type
52
- value
53
- else
54
- case
55
- when Array == type || Array === type then coerce_array(value, type)
56
- when Hash == type || Hash === type then coerce_hash(value, type)
57
- when type == Time then coerce_time(value)
58
- else coerce_other(value, type)
59
- end
60
- end
61
- end
62
-
63
-
64
- def coerce_array(value, type)
65
- # type: generic Array
66
- if type == Array
67
- coerce_other(value, type)
68
- # type: Array[Something]
69
- elsif value.respond_to?(:map)
70
- value.map do |element|
71
- coerce(element, type[0])
72
- end
73
- else
74
- raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
75
- end
76
- end
77
-
78
- def coerce_hash(value, type)
79
- # type: generic Hash
80
- if type == Hash
81
- coerce_other(value, type)
82
- # type: Hash[Something => Other thing]
83
- elsif value.respond_to?(:to_h)
84
- k_type, v_type = type.to_a[0]
85
- value.to_h.map{ |k,v| [ coerce(k, k_type), coerce(v, v_type) ] }.to_h
86
- else
87
- raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
88
- end
89
- end
90
-
91
- def coerce_time(value)
92
- case value
93
- when Integer, Float then Time.at(value)
94
- when /\d{4}-\d{2}-\d{2}/ then Time.parse(value)
95
- when /\A\d+(\.\d+)?/ then Time.at(value.to_f)
96
- else value
97
- end
98
- end
99
-
100
- def coerce_other(value, type)
101
- coercer[value.class].send("to_#{type.to_s.downcase}", value)
102
- end
103
47
  end
104
48
 
105
49
  class_methods do
@@ -121,7 +65,7 @@ module Bigqueryid
121
65
  end
122
66
 
123
67
  define_method("#{name}=") do |value| # Define set method
124
- instance_variable_set("@#{name}", coerce(value, attributes[name][:type]))
68
+ instance_variable_set("@#{name}", coercer.coerce(name, value))
125
69
  end
126
70
  end
127
71
  end
@@ -0,0 +1,72 @@
1
+ module Bigqueryid
2
+ class Coercer
3
+ attr_reader :coercer, :schema
4
+
5
+ def initialize(schema, coercer = Coercible::Coercer.new)
6
+ @coercer = coercer
7
+ @schema = schema
8
+ end
9
+
10
+ def coerce(name, value)
11
+ return value if value.nil? || !has_type?(name)
12
+
13
+ coerce_by_type(value, schema[name.to_s][:type])
14
+ end
15
+
16
+ private
17
+
18
+ def has_type?(name)
19
+ schema.include?(name.to_s) &&
20
+ schema[name.to_s].include?(:type)
21
+ end
22
+
23
+ def coerce_by_type(value, type)
24
+ case
25
+ when Array == type || Array === type then coerce_array(value, type)
26
+ when Hash == type || Hash === type then coerce_hash(value, type)
27
+ when type == Time then coerce_time(value)
28
+ else coerce_other(value, type)
29
+ end
30
+ end
31
+
32
+ def coerce_array(value, type)
33
+ # type: generic Array
34
+ if type == Array
35
+ coerce_other(value, type)
36
+ # type: Array[Something]
37
+ elsif value.respond_to?(:map)
38
+ value.map do |element|
39
+ coerce_by_type(element, type[0])
40
+ end
41
+ else
42
+ raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
43
+ end
44
+ end
45
+
46
+ def coerce_hash(value, type)
47
+ # type: generic Hash
48
+ if type == Hash
49
+ coerce_other(value, type)
50
+ # type: Hash[Something => Other thing]
51
+ elsif value.respond_to?(:to_h)
52
+ k_type, v_type = type.to_a[0]
53
+ value.to_h.map { |k,v| [coerce_by_type(k, k_type), coerce_by_type(v, v_type)] }.to_h
54
+ else
55
+ raise ArgumentError.new "Invalid coercion: #{value.class} => #{type}"
56
+ end
57
+ end
58
+
59
+ def coerce_time(value)
60
+ case value
61
+ when Integer, Float then Time.at(value)
62
+ when /\d{4}-\d{2}-\d{2}/ then Time.parse(value)
63
+ when /\A\d+(\.\d+)?/ then Time.at(value.to_f)
64
+ else value
65
+ end
66
+ end
67
+
68
+ def coerce_other(value, type)
69
+ coercer[value.class].send("to_#{type.to_s.downcase}", value)
70
+ end
71
+ end
72
+ end
@@ -6,6 +6,24 @@ module Bigqueryid
6
6
 
7
7
  class_methods do
8
8
 
9
+ def where(filter)
10
+ return [] if filter.empty?
11
+
12
+ where = []
13
+ filter.each do |key, value|
14
+ filter[key] = coercer.coerce(key, value)
15
+ where << "#{key} = @#{key}"
16
+ end
17
+
18
+ query = "SELECT * FROM #{dataset_name}.#{table_name}" \
19
+ " WHERE #{where.join(' AND ')}"
20
+
21
+ result = bigquery.query(query, params: filter)
22
+ result.map do |row|
23
+ new(row.select { |f| attributes.include?(f) })
24
+ end
25
+ end
26
+
9
27
  def run(query)
10
28
  result = bigquery.query query
11
29
  new(result.first) if result.count == 1
@@ -1,5 +1,5 @@
1
1
  module Bigqueryid
2
2
  # version string
3
3
  # @api public
4
- VERSION = '0.5.2'
4
+ VERSION = '0.5.3'
5
5
  end
data/lib/bigqueryid.rb CHANGED
@@ -8,6 +8,7 @@ module Bigqueryid
8
8
  require 'bigqueryid/attributes'
9
9
  require 'bigqueryid/criteria/queryable'
10
10
  require 'bigqueryid/criteria'
11
+ require 'bigqueryid/coercer'
11
12
  require 'bigqueryid/base/initializable'
12
13
  require 'bigqueryid/base'
13
14
  require 'bigqueryid/timestamps'
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bigqueryid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Tomio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-15 00:00:00.000000000 Z
11
+ date: 2017-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.13'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.13'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '5.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: google-cloud-bigquery
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0.21'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.21'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ~>
74
74
  - !ruby/object:Gem::Version
75
75
  version: '5.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: activemodel
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ~>
88
88
  - !ruby/object:Gem::Version
89
89
  version: '5.0'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ~>
95
95
  - !ruby/object:Gem::Version
96
96
  version: '5.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: coercible
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - "~>"
101
+ - - ~>
102
102
  - !ruby/object:Gem::Version
103
103
  version: '1.0'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - "~>"
108
+ - - ~>
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.0'
111
111
  description: Simple ORM for Google BigQuery
@@ -114,8 +114,8 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
- - ".gitignore"
118
- - ".travis.yml"
117
+ - .gitignore
118
+ - .travis.yml
119
119
  - CHANGELOG.md
120
120
  - Gemfile
121
121
  - LICENSE
@@ -128,6 +128,7 @@ files:
128
128
  - lib/bigqueryid/attributes.rb
129
129
  - lib/bigqueryid/base.rb
130
130
  - lib/bigqueryid/base/initializable.rb
131
+ - lib/bigqueryid/coercer.rb
131
132
  - lib/bigqueryid/criteria.rb
132
133
  - lib/bigqueryid/criteria/queryable.rb
133
134
  - lib/bigqueryid/errors/bigquery_error.rb
@@ -143,17 +144,17 @@ require_paths:
143
144
  - lib
144
145
  required_ruby_version: !ruby/object:Gem::Requirement
145
146
  requirements:
146
- - - ">="
147
+ - - '>='
147
148
  - !ruby/object:Gem::Version
148
149
  version: '0'
149
150
  required_rubygems_version: !ruby/object:Gem::Requirement
150
151
  requirements:
151
- - - ">="
152
+ - - '>='
152
153
  - !ruby/object:Gem::Version
153
154
  version: '0'
154
155
  requirements: []
155
156
  rubyforge_project:
156
- rubygems_version: 2.6.8
157
+ rubygems_version: 2.0.14.1
157
158
  signing_key:
158
159
  specification_version: 4
159
160
  summary: Google BigQuery ORM