activerecord-lookml 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 9a07c3476ccf7972ccb3d63e9980b9bcc80b946948bb916747394fe07cff35a4
4
- data.tar.gz: 90e41c624eedc09d131db0710d55528d6380c6dffa55d7e7c0ce3587cc0f5e36
3
+ metadata.gz: 27459b87408c0d5f3432f0b0242db4f630a10fce282dde64cc7d61fe12793fe7
4
+ data.tar.gz: 6c23d271541d72ad3ef203d2e15b1b999a036cda1255bdd8fbfd90a69956d65e
5
5
  SHA512:
6
- metadata.gz: f4a5fa4bdc77dfdd52f36da8b24b0ffe30a12667c5c49483e24eaf5e38003835dd2302d7e2916ebd2def73da9e310438b47c7e5cc46165a613441066d6fa27f6
7
- data.tar.gz: 0af9abfbccd4c6cb0507dda2a1f42d6d788f3c4249e685ff90bc73b7dc66c3b3897ab9668b89fcb12b25455397abffb73916357f169c10b6d924b2ac17c4e160
6
+ metadata.gz: fbd0a06d11c2fd45c50118254ce498a6b7546f42fc99315bb3b4fa66fdd39d42aeff9f18c9ef1a2622125f598227081731ffd0ea18db78fd0ffb22e1a1af0ccb
7
+ data.tar.gz: aa64159cbc81f1fa94c122515d2805ebabafb1ded067c44822969ac98f66ff8cce9cc9d35575557db00454427bb7b311a4116964341a793badc380c7b641b7cd
@@ -0,0 +1,49 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.7', '3.0']
23
+
24
+ services:
25
+ postgres:
26
+ image: postgres:12
27
+ env:
28
+ POSTGRES_HOST_AUTH_METHOD: trust
29
+ POSTGRES_USER: postgres
30
+ options: >-
31
+ --health-cmd pg_isready
32
+ --health-interval 10s
33
+ --health-timeout 5s
34
+ --health-retries 5
35
+ ports:
36
+ - 5432:5432
37
+
38
+ env:
39
+ RUBYOPT: -W:deprecated
40
+
41
+ steps:
42
+ - uses: actions/checkout@v2
43
+ - name: Set up Ruby
44
+ uses: ruby/setup-ruby@v1
45
+ with:
46
+ ruby-version: ${{ matrix.ruby-version }}
47
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
48
+ - name: Run tests
49
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -10,3 +10,5 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  Gemfile.lock
13
+
14
+ *.gem
data/CHANGELOG.md CHANGED
@@ -0,0 +1,7 @@
1
+ ## Unreleased
2
+
3
+ ## 0.1.0
4
+
5
+ Initial version
6
+
7
+ - Support LookML generation from ActiveRecord::Enum definitions
data/README.md CHANGED
@@ -10,7 +10,7 @@ Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
12
  group :development do
13
- gem 'activerecord-lookml', git: 'git@github.com:wantedly/activerecord-looker.git'
13
+ gem 'activerecord-lookml'
14
14
  end
15
15
  ```
16
16
 
@@ -1,4 +1,4 @@
1
- require_relative 'lib/activerecord/lookml/version'
1
+ require_relative 'lib/active_record/lookml/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "activerecord-lookml"
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.description = %q{[Experimental] Generate LookML}
11
11
  spec.homepage = "https://github.com/wantedly/activerecord-lookml"
12
12
  spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.6")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
  spec.metadata["source_code_uri"] = "https://github.com/wantedly/activerecord-lookml"
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.add_dependency 'activerecord', '>= 6'
30
30
 
31
31
  spec.add_development_dependency 'activerecord', '>= 6'
32
+ spec.add_development_dependency 'pg', '>= 1'
32
33
  end
@@ -1,5 +1,5 @@
1
1
  require 'active_support/all'
2
- require "activerecord/lookml/version"
2
+ require "active_record/lookml/version"
3
3
 
4
4
  module ActiveRecord
5
5
  module LookML
@@ -9,6 +9,6 @@ module ActiveRecord
9
9
  end
10
10
 
11
11
  ActiveSupport.on_load :active_record do
12
- require 'activerecord/lookml/core'
12
+ require 'active_record/lookml/core'
13
13
  ::ActiveRecord::Base.send :include, ActiveRecord::LookML::Core
14
14
  end
@@ -0,0 +1,176 @@
1
+ module ActiveRecord
2
+ module LookML
3
+ module Core
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ # Generates LookML dimension from ruby hash.
8
+ #
9
+ # Input format:
10
+ #
11
+ # {"manager_tutorial_state"=>
12
+ # {"start_tutorial"=>0,
13
+ # "explain_condition_survay"=>1,
14
+ # "ask_condition"=>2,
15
+ # "explain_high_fives"=>3,
16
+ # "try_high_fives"=>4,
17
+ # "request_members_to_get_started"=>5,
18
+ # "done_announce_to_member"=>6,
19
+ # "done_tutorial"=>7}}
20
+ #
21
+ #
22
+ # Output format:
23
+ #
24
+ # dimension: manager_tutorial_state {
25
+ # case: {
26
+ # when: {
27
+ # sql: ${TABLE}.manager_tutorial_state = 0 ;;
28
+ # label: "start_tutorial"
29
+ # }
30
+ # when: {
31
+ # sql: ${TABLE}.manager_tutorial_state = 1 ;;
32
+ # label: "explain_condition_survay"
33
+ # }
34
+ # when: {
35
+ # sql: ${TABLE}.manager_tutorial_state = 2 ;;
36
+ # label: "ask_condition"
37
+ # }
38
+ # when: {
39
+ # sql: ${TABLE}.manager_tutorial_state = 3 ;;
40
+ # label: "explain_high_fives"
41
+ # }
42
+ # when: {
43
+ # sql: ${TABLE}.manager_tutorial_state = 4 ;;
44
+ # label: "try_high_fives"
45
+ # }
46
+ # when: {
47
+ # sql: ${TABLE}.manager_tutorial_state = 5 ;;
48
+ # label: "request_members_to_get_started"
49
+ # }
50
+ # when: {
51
+ # sql: ${TABLE}.manager_tutorial_state = 6 ;;
52
+ # label: "done_announce_to_member"
53
+ # }
54
+ # when: {
55
+ # sql: ${TABLE}.manager_tutorial_state = 7 ;;
56
+ # label: "done_tutorial"
57
+ # }
58
+ # }
59
+ # }
60
+ # @see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/enum.rb
61
+ def enum_to_lookml
62
+ defined_enums.map do |name, enum_values|
63
+ when_lines_lookml = enum_values.map do |label, value|
64
+ <<-LOOKML
65
+ when: {
66
+ sql: ${TABLE}.#{name} = #{value} ;;
67
+ label: "#{label}"
68
+ }
69
+ LOOKML
70
+ end.join
71
+
72
+ <<-LOOKML
73
+ dimension: #{name} {
74
+ case: {
75
+ #{when_lines_lookml}
76
+ }
77
+ }
78
+ LOOKML
79
+ end.join("\n")
80
+ end
81
+
82
+ def to_lookml(table_name_prefix: 'wantedly-1371.rdb.pulse_')
83
+ dimensions_lookml = attribute_types.map do |attribute, type|
84
+ attribute_type_to_dimension_lookml(attribute, type)
85
+ end.join("\n")
86
+
87
+ fields = attribute_types.map do |attribute, type|
88
+ attribute_type_to_set_detail_field(attribute, type)
89
+ end.compact
90
+ fields_lookml = fields.map { |field| " #{field}" }.join(",\n")
91
+
92
+ set_lookml = <<-LOOKML
93
+ set: detail {
94
+ fields: [
95
+ #{fields_lookml}
96
+ ]
97
+ }
98
+ LOOKML
99
+
100
+ <<-LOOKML
101
+ view: pulse_onboarding_statuses {
102
+ sql_table_name: `#{table_name_prefix}#{table_name}`;;
103
+
104
+ #{dimensions_lookml}
105
+ #{set_lookml}}
106
+ LOOKML
107
+ end
108
+
109
+ private
110
+ def attribute_type_to_dimension_lookml(attribute, type)
111
+ case type
112
+ when ActiveModel::Type::Integer
113
+ params = {
114
+ type: "number",
115
+ primary_key: attribute == "id" ? "yes" : nil,
116
+ sql: "${TABLE}.#{attribute} ;;"
117
+ }.compact
118
+
119
+ params_lookml = params.map { |k, v| " #{k}: #{v}" }.join("\n")
120
+
121
+ <<-LOOKML
122
+ dimension: #{attribute} {
123
+ #{params_lookml}
124
+ }
125
+ LOOKML
126
+ when ActiveModel::Type::Boolean
127
+ <<-LOOKML
128
+ dimension: #{attribute} {
129
+ type: yesno
130
+ sql: ${TABLE}.#{attribute} ;;
131
+ }
132
+ LOOKML
133
+ when ActiveRecord::Type::DateTime
134
+ <<-LOOKML
135
+ dimension_group: #{attribute} {
136
+ type: time
137
+ sql: ${TABLE}.#{attribute} ;;
138
+ }
139
+ LOOKML
140
+ when ActiveRecord::Enum::EnumType
141
+ enum_values = defined_enums[attribute]
142
+ when_lines_lookml = enum_values.map do |label, value|
143
+ <<-LOOKML
144
+ when: {
145
+ sql: ${TABLE}.#{attribute} = #{value} ;;
146
+ label: "#{label}"
147
+ }
148
+ LOOKML
149
+ end.join
150
+
151
+ <<-LOOKML
152
+ dimension: #{attribute} {
153
+ case: {
154
+ #{when_lines_lookml}
155
+ }
156
+ }
157
+ LOOKML
158
+ else
159
+ raise "Unknown attribute type: #{type.class}"
160
+ end
161
+ end
162
+
163
+ def attribute_type_to_set_detail_field(attribute, type)
164
+ case type
165
+ when ActiveModel::Type::Integer, ActiveModel::Type::Boolean, ActiveRecord::Enum::EnumType
166
+ attribute
167
+ when ActiveRecord::Type::DateTime
168
+ "#{attribute}_time"
169
+ else
170
+ raise "Unknown attribute type: #{type.class}"
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module LookML
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-lookml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshinori Kawasaki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
55
69
  description: "[Experimental] Generate LookML"
56
70
  email:
57
71
  - yoshi@wantedly.com
@@ -59,9 +73,9 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
76
+ - ".github/workflows/ruby.yml"
62
77
  - ".gitignore"
63
78
  - ".rspec"
64
- - ".travis.yml"
65
79
  - CHANGELOG.md
66
80
  - CODE_OF_CONDUCT.md
67
81
  - Gemfile
@@ -71,9 +85,9 @@ files:
71
85
  - activerecord-lookml.gemspec
72
86
  - bin/console
73
87
  - bin/setup
74
- - lib/activerecord/lookml.rb
75
- - lib/activerecord/lookml/core.rb
76
- - lib/activerecord/lookml/version.rb
88
+ - lib/active_record/lookml.rb
89
+ - lib/active_record/lookml/core.rb
90
+ - lib/active_record/lookml/version.rb
77
91
  homepage: https://github.com/wantedly/activerecord-lookml
78
92
  licenses:
79
93
  - MIT
@@ -89,14 +103,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
103
  requirements:
90
104
  - - ">="
91
105
  - !ruby/object:Gem::Version
92
- version: '2.6'
106
+ version: '2.7'
93
107
  required_rubygems_version: !ruby/object:Gem::Requirement
94
108
  requirements:
95
109
  - - ">="
96
110
  - !ruby/object:Gem::Version
97
111
  version: '0'
98
112
  requirements: []
99
- rubygems_version: 3.0.3
113
+ rubygems_version: 3.2.3
100
114
  signing_key:
101
115
  specification_version: 4
102
116
  summary: ActiveRecord extension for LookML (Looker)
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.6.6
6
- before_install: gem install bundler -v 2.1.4
@@ -1,84 +0,0 @@
1
- module ActiveRecord
2
- module LookML
3
- module Core
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- # Generates LookML dimension from ruby hash.
8
- #
9
- # Input format:
10
- #
11
- # {"manager_tutorial_state"=>
12
- # {"start_tutorial"=>0,
13
- # "explain_condition_survay"=>1,
14
- # "ask_condition"=>2,
15
- # "explain_high_fives"=>3,
16
- # "try_high_fives"=>4,
17
- # "request_members_to_get_started"=>5,
18
- # "done_announce_to_member"=>6,
19
- # "done_tutorial"=>7}}
20
- #
21
- #
22
- # Output format:
23
- #
24
- # dimension: manager_tutorial_state {
25
- # case: {
26
- # when: {
27
- # sql: ${TABLE}.manager_tutorial_state = 0 ;;
28
- # label: "start_tutorial"
29
- # }
30
- # when: {
31
- # sql: ${TABLE}.manager_tutorial_state = 1 ;;
32
- # label: "explain_condition_survay"
33
- # }
34
- # when: {
35
- # sql: ${TABLE}.manager_tutorial_state = 2 ;;
36
- # label: "ask_condition"
37
- # }
38
- # when: {
39
- # sql: ${TABLE}.manager_tutorial_state = 3 ;;
40
- # label: "explain_high_fives"
41
- # }
42
- # when: {
43
- # sql: ${TABLE}.manager_tutorial_state = 4 ;;
44
- # label: "try_high_fives"
45
- # }
46
- # when: {
47
- # sql: ${TABLE}.manager_tutorial_state = 5 ;;
48
- # label: "request_members_to_get_started"
49
- # }
50
- # when: {
51
- # sql: ${TABLE}.manager_tutorial_state = 6 ;;
52
- # label: "done_announce_to_member"
53
- # }
54
- # when: {
55
- # sql: ${TABLE}.manager_tutorial_state = 7 ;;
56
- # label: "done_tutorial"
57
- # }
58
- # }
59
- # }
60
- # @see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/enum.rb
61
- def enum_to_lookml
62
- defined_enums.map do |name, enum_values|
63
- when_lines_lookml = enum_values.map do |label, value|
64
- <<-LOOKML
65
- when: {
66
- sql: ${TABLE}.#{name} = #{value} ;;
67
- label: "#{label}"
68
- }
69
- LOOKML
70
- end.join
71
-
72
- <<-LOOKML
73
- dimension: #{name} {
74
- case: {
75
- #{when_lines_lookml}
76
- }
77
- }
78
- LOOKML
79
- end.join("\n")
80
- end
81
- end
82
- end
83
- end
84
- end