bigqueryid 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebd0bdb293a7eb11391a3da70f46f3b084301088
4
+ data.tar.gz: 0c7664612aa35d696e37bc303b257b8ed260df08
5
+ SHA512:
6
+ metadata.gz: 6fc71e931b932fb59d56a9c73c48d7ca0b86b9cb73a59d5d87942d02e2578ecd7214b04f11723c709b493e8468e722720f4956163a61fd2c09886ad8f55e3e53
7
+ data.tar.gz: 8573c4e24e1bdad7b1a177dfc534e7f8199821577bc60fb73cb154de5ba9c5d18464675bf07ba2a30452a04f610157cb0758de9bd50447432c3d9ca58db7162c
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # BigQueryID
2
+
3
+ BigQueryID is an ORM (Object-Relational-Mapper) framework for Google BigQuery in Ruby.
4
+
5
+ Install
6
+ -------
7
+ ```sh
8
+ gem build bigqueryid.gem
9
+ gem install bigqueryid-<version>.gem
10
+ ```
11
+ or
12
+ ```ruby
13
+ gem 'bigqueryid', :git => 'git://github.com/fabiotomio/bigqueryid.git'
14
+ ```
15
+
16
+ Configure
17
+ ---------
18
+ ```sh
19
+ export GCLOUD_PROJECT=my-todo-project-id
20
+ export GCLOUD_KEYFILE_JSON=/path/to/keyfile.json
21
+ ```
22
+
23
+ Use
24
+ -------
25
+ ```ruby
26
+ # Define product model product.rb
27
+ class Product
28
+
29
+ include Bigquery::Base
30
+
31
+ dataset 'core'
32
+ table 'products'
33
+
34
+ def self.create_table
35
+ bigquery.dataset(self.dataset_name).create_table self.table_name do |schema|
36
+ schema.string 'barcode'
37
+ schema.timestamp 'created_at'
38
+ schema.integer 'id'
39
+ schema.string 'name'
40
+ schema.float 'price'
41
+ schema.timestamp 'updated_at'
42
+ end unless table_exist?
43
+ end
44
+
45
+ def self.fetch_all
46
+ sql = <<-SQL.squish
47
+ SELECT
48
+ *
49
+ FROM core.products
50
+ ORDER BY
51
+ P.name
52
+ SQL
53
+ fetch sql
54
+ end
55
+ end
56
+
57
+ # Return if table exists
58
+ Product.table_exist?
59
+
60
+ # Delete table
61
+ Product.delete_table
62
+
63
+ # Delete and create table
64
+ Product.flush
65
+
66
+ # Fetch all rows
67
+ Product.fetch_all
68
+
69
+ ```
@@ -0,0 +1,70 @@
1
+ module Bigqueryid
2
+ # Define behaviour to attributes of entity.
3
+ module Attributes
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :attributes
8
+
9
+ self.attributes = {}
10
+
11
+ field :id
12
+
13
+ def attributes=(attributes)
14
+ if attributes.is_a? ::Hash
15
+ attributes.each_pair { |key, value| send("#{key}=", value) }
16
+ else
17
+ raise Bigqueryid::Errors::BigqueryError.new 'Attributes params need is Hash'
18
+ end
19
+ rescue
20
+ raise Bigqueryid::Errors::BigqueryError.new 'Attribute invalid'
21
+ end
22
+
23
+ def properties_names
24
+ attributes.keys
25
+ end
26
+
27
+ def set_default_values
28
+ attributes.each_pair do |name, options|
29
+ next unless options.key? :default
30
+
31
+ default = options[:default]
32
+ # Default might be a lambda
33
+ value = default.respond_to?(:call) ? default.call : default
34
+ send("#{name}=", value)
35
+ end
36
+ end
37
+
38
+ def to_hash
39
+ hash = {}
40
+ properties_names.each do |property|
41
+ hash[property] = send(property)
42
+ end
43
+ hash.sort.to_h
44
+ end
45
+ end
46
+
47
+ class_methods do
48
+ protected
49
+
50
+ def field(name, options = {})
51
+ add_field(name.to_s, options)
52
+ end
53
+
54
+ def add_field(name, options)
55
+ attributes[name] = options
56
+ create_accessors(name)
57
+ end
58
+
59
+ # https://www.leighhalliday.com/ruby-metaprogramming-creating-methods
60
+ def create_accessors(name)
61
+ define_method(name) do # Define get method
62
+ instance_variable_get("@#{name}")
63
+ end
64
+ define_method("#{name}=") do |value| # Define set method
65
+ instance_variable_set("@#{name}", value)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,20 @@
1
+ module Bigqueryid
2
+ module Base
3
+ # Define behaviour for initialization of Base.
4
+ module Initializable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def initialize(options = nil)
9
+ set_default_values
10
+ if options.is_a? Google::Cloud::Bigquery::Data
11
+ properties_names.each { |a| send("#{a}=", options[a.to_s]) }
12
+ send('id=', options.key.id)
13
+ elsif options.is_a? ::Hash
14
+ options.each_pair { |key, value| send("#{key}=", value) }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_support/concern'
2
+ # BigQuery DSL
3
+ module Bigqueryid
4
+ # Inject class methods in BigQuery Model
5
+ module Base
6
+ extend ActiveSupport::Concern
7
+
8
+ include Attributes
9
+ include Criteria
10
+
11
+ include Base::Initializable
12
+
13
+ included do
14
+ class_attribute :dataset_name
15
+ class_attribute :table_name
16
+
17
+ def self.dataset(name)
18
+ self.dataset_name = name
19
+ end
20
+
21
+ def self.table(name)
22
+ self.table_name = name
23
+ end
24
+
25
+ def self.gcloud
26
+ Google::Cloud.new
27
+ end
28
+
29
+ def self.bigquery
30
+ @bigquery_connection ||= gcloud.bigquery
31
+ end
32
+
33
+ def self.gcloud_table
34
+ bigquery.dataset(dataset_name).table(table_name)
35
+ end
36
+
37
+ def self.table_exist?
38
+ gcloud_table ? true : false
39
+ end
40
+
41
+ def self.delete_table
42
+ gcloud_table.delete if table_exist?
43
+ end
44
+
45
+ def self.flush
46
+ delete_table
47
+ create_table
48
+ end
49
+
50
+ def self.load_data_from_csv(gs_url)
51
+ job = gcloud_table.load gs_url
52
+ end
53
+
54
+ def self.run(query_string)
55
+ bigquery.query query_string
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ module Bigqueryid
2
+ module Criteria
3
+ # Defines behaviour for query operations.
4
+ module Queryable
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+
9
+ def run(query)
10
+ result = bigquery.query query
11
+ new(result.first) if result.count == 1
12
+ end
13
+
14
+ def fetch(query)
15
+ result = bigquery.query query
16
+ if result.count > 0
17
+ result.map { |element| new(element) }
18
+ else
19
+ []
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ module Bigqueryid
2
+ # Inject behaviour for query operations.
3
+ module Criteria
4
+ extend ActiveSupport::Concern
5
+
6
+ include Queryable
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Bigqueryid
2
+ module Errors
3
+ class BigqueryError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module Bigqueryid
2
+ # Define behaviour to created_at and updated_at data
3
+ module Timestamps
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ field :created_at, default: -> { Time.now.strftime('%Y-%m-%d %H:%M:%S') }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Bigqueryid
2
+ # version string
3
+ # @api public
4
+ VERSION = '0.4.0'
5
+ end
data/lib/bigqueryid.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Bigqueryid
2
+ require 'google/cloud/bigquery'
3
+ require 'active_support/concern'
4
+ require 'active_model'
5
+
6
+ require 'bigqueryid/errors/bigquery_error'
7
+ require 'bigqueryid/attributes'
8
+ require 'bigqueryid/criteria/queryable'
9
+ require 'bigqueryid/criteria'
10
+ require 'bigqueryid/base/initializable'
11
+ require 'bigqueryid/base'
12
+ require 'bigqueryid/timestamps'
13
+ require 'bigqueryid/version'
14
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigqueryid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Tomio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-bigquery
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.20'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.20'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ description: Simple ORM to Google BigQuery
56
+ email: fabiotomio@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - lib/bigqueryid.rb
63
+ - lib/bigqueryid/attributes.rb
64
+ - lib/bigqueryid/base.rb
65
+ - lib/bigqueryid/base/initializable.rb
66
+ - lib/bigqueryid/criteria.rb
67
+ - lib/bigqueryid/criteria/queryable.rb
68
+ - lib/bigqueryid/errors/bigquery_error.rb
69
+ - lib/bigqueryid/timestamps.rb
70
+ - lib/bigqueryid/version.rb
71
+ homepage: http://github.com/fabiotomio/bigqueryid
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.3.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.6.7
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Google BigQuery ORM
95
+ test_files: []