active_shotgun 0.0.1 → 0.0.2

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: 89cab512ffc72e7abd251c9d871e6164db9fb8ee00b4976e2e7766586e63505c
4
- data.tar.gz: 3945e4c1b0ea88ebacafdb9465c1c177652bcc686401ee4d6ac8864e5a2b966a
3
+ metadata.gz: e2c263bcd899abc5f20b86c31586b9c973ff9620dd742f923c15f74285083585
4
+ data.tar.gz: 5ed4741b318d9d9ea168341bd52b5995a2a209762a4155323bcb516d4c326246
5
5
  SHA512:
6
- metadata.gz: 7c058b1a1a57e6203ae52bec0e9a7dfbe975ab7a4f69915c064d35b7a74c182a2d897bb8d1d1368bca019075b2d9535ae8cda31ec6b43604757fa0c035d40d82
7
- data.tar.gz: b7c194c33223601a1e81b588825c3d746598d06ae76d438a5e97c359886635c6261b8a39e5a7490276fc5af564407c8784daf44e8e206aae7993f11788a3b186
6
+ metadata.gz: 1e7a8d64f3b69ab20da321cca18ccb5c4016144a5cd8c60cc2b88b8ac8a4febc7620bc69a2d80c5a8eb47a88bf2c8c8babb63d5883587c38b76751f22ef4ffe7
7
+ data.tar.gz: dbe156034f91918614f051ed81a5e222fadde079f7c1c73e89a3761929ca07029d74bd395c6c94b73fe4d9ddf2cc23cfd79d1f2c6eeb86cfe61ff0b772c6c8eb
@@ -14,8 +14,10 @@ jobs:
14
14
  - name: Fetch main branch
15
15
  run: git fetch origin main:main
16
16
  - name: Verify if there's a change in version
17
- run: "git diff main lib/shotgun_api_ruby/version.rb | grep VERSION"
17
+ run: "git diff main lib/active_shotgun/version.rb | grep VERSION"
18
18
  - name: Print new version
19
- run: 'git diff main lib/shotgun_api_ruby/version.rb | grep -E "^\+.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?"'
19
+ run: 'git diff main lib/active_shotgun/version.rb | grep -E "^\+.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?"'
20
20
  - name: Verify if higher version
21
- run: '[[ $(git diff main lib/shotgun_api_ruby/version.rb | grep -E "^\+.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?") > $(git diff main lib/shotgun_api_ruby/version.rb | grep -E "^-.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?") ]]'
21
+ run: '[[ $(git diff main lib/active_shotgun/version.rb | grep -E "^\+.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?") > $(git diff main lib/active_shotgun/version.rb | grep -E "^-.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?") ]]'
22
+ - name: Verify if version is updated in Gemfile.lock
23
+ run: '[[ $(cat Gemfile.lock | grep "$(git diff main lib/active_shotgun/version.rb | grep -E "^\+.*VERSION" | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?")") ]]'
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .env
13
+ tmp
@@ -6,9 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.0.2] - 2020-12-16
10
+ ### Added
11
+ - save, create, update, find, delete, revive (and friends)
12
+ - Validations support
13
+ - Callbacks support
14
+ - AM::Dirty and many helpers
15
+ ### Missing
16
+ - Query interface (where, find_by, first, all, order, offset)
17
+ - Support Entity and Multi-Entity
18
+
9
19
  ## [0.0.1] - 2020-12-16
10
20
  ### Added
11
21
  - Gem init
12
22
 
13
- [Unreleased]: https://github.com/zaratan/active_shotgun/compare/v0.0.1...HEAD
14
- [0.0.1]: https://github.com/zaratan/active_shotgun/releases/tag/v0.1.1
23
+ [Unreleased]: https://github.com/zaratan/active_shotgun/compare/v0.0.2...HEAD
24
+ [0.0.2]: https://github.com/zaratan/active_shotgun/releases/tag/v0.0.2
25
+ [0.0.1]: https://github.com/zaratan/active_shotgun/releases/tag/v0.0.1
@@ -1,12 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_shotgun (0.0.1)
4
+ active_shotgun (0.0.2)
5
+ activemodel (>= 5.2)
6
+ shotgun_api_ruby (>= 0.0.8.2)
5
7
  zeitwerk (~> 2)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
9
11
  specs:
12
+ activemodel (6.1.0)
13
+ activesupport (= 6.1.0)
14
+ activesupport (6.1.0)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ tzinfo (~> 2.0)
19
+ zeitwerk (~> 2.3)
10
20
  ast (2.4.1)
11
21
  bundler-audit (0.7.0.1)
12
22
  bundler (>= 1.2.0, < 3)
@@ -17,12 +27,18 @@ GEM
17
27
  concurrent-ruby (1.1.7)
18
28
  diff-lcs (1.4.4)
19
29
  docile (1.3.2)
30
+ dotenv (2.7.6)
20
31
  faker (2.15.1)
21
32
  i18n (>= 1.6, < 2)
33
+ faraday (1.1.0)
34
+ multipart-post (>= 1.2, < 3)
35
+ ruby2_keywords
22
36
  i18n (1.8.5)
23
37
  concurrent-ruby (~> 1.0)
24
38
  iniparse (1.5.0)
25
39
  method_source (1.0.0)
40
+ minitest (5.14.2)
41
+ multipart-post (2.1.1)
26
42
  overcommit (0.57.0)
27
43
  childprocess (>= 0.6.3, < 5)
28
44
  iniparse (~> 1.4)
@@ -70,6 +86,11 @@ GEM
70
86
  rubocop (>= 0.90.0, < 2.0)
71
87
  rubocop-ast (>= 0.4.0)
72
88
  ruby-progressbar (1.10.1)
89
+ ruby2_keywords (0.0.2)
90
+ shotgun_api_ruby (0.0.8.2)
91
+ activesupport
92
+ faraday (~> 1)
93
+ zeitwerk (~> 2.2)
73
94
  simplecov (0.20.0)
74
95
  docile (~> 1.1)
75
96
  simplecov-html (~> 0.11)
@@ -79,6 +100,8 @@ GEM
79
100
  simplecov (>= 0.4.1)
80
101
  simplecov_json_formatter (0.1.2)
81
102
  thor (1.0.1)
103
+ tzinfo (2.0.3)
104
+ concurrent-ruby (~> 1.0)
82
105
  unicode-display_width (1.7.0)
83
106
  yard (0.9.25)
84
107
  zeitwerk (2.4.2)
@@ -90,6 +113,7 @@ DEPENDENCIES
90
113
  active_shotgun!
91
114
  bundler
92
115
  bundler-audit
116
+ dotenv
93
117
  overcommit
94
118
  pry-byebug
95
119
  rake (~> 12.0)
@@ -28,10 +28,13 @@ Gem::Specification.new do |spec|
28
28
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
30
 
31
+ spec.add_dependency 'activemodel', '>= 5.2'
32
+ spec.add_dependency 'shotgun_api_ruby', '>= 0.0.8.2'
31
33
  spec.add_dependency 'zeitwerk', '~> 2'
32
34
 
33
35
  spec.add_development_dependency 'bundler'
34
36
  spec.add_development_dependency 'bundler-audit'
37
+ spec.add_development_dependency "dotenv"
35
38
  spec.add_development_dependency 'overcommit'
36
39
  spec.add_development_dependency 'pry-byebug'
37
40
  spec.add_development_dependency 'rake'
@@ -10,5 +10,5 @@ require "active_shotgun"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
14
- IRB.start(__FILE__)
13
+ require "pry"
14
+ Pry.start(__FILE__)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+
6
+ require 'shotgun_api_ruby'
7
+
8
+ module ActiveShotgun
9
+ class Client
10
+ include Singleton
11
+ CONNECTION_TYPES = ["multi_entity", "entity"].freeze
12
+ READ_ONLY_TYPES = ["image", "summary"].freeze
13
+
14
+ def initialize
15
+ @shotgun = ShotgunApiRuby.new(
16
+ shotgun_site: Config.shotgun_site_name || Config.shotgun_site_url,
17
+ auth: { client_id: Config.shotgun_client_id, client_secret: Config.shotgun_client_secret }
18
+ )
19
+ end
20
+ attr_reader :shotgun
21
+
22
+ class << self
23
+ extend Forwardable
24
+ # Remove the need to call .instance everywhere
25
+ def_delegators(
26
+ :instance,
27
+ :shotgun,
28
+ :fetch_field_names_for_an_entity_type
29
+ )
30
+ end
31
+
32
+ def fetch_field_names_for_an_entity_type(type)
33
+ result = shotgun.entities(type).fields.to_h
34
+ {
35
+ writable: result.reject do |_, v|
36
+ (CONNECTION_TYPES + READ_ONLY_TYPES).include?(v.data_type) ||
37
+ !v.editable
38
+ end.keys,
39
+ readable: result.reject{ |_, v| CONNECTION_TYPES.include?(v.data_type) }.keys,
40
+ }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+
6
+ module ActiveShotgun
7
+ class Config
8
+ DEFAULT_CONFIG = {
9
+ shotgun_site_name: 'pasind3-prod',
10
+ shotgun_site_url: nil,
11
+ shotgun_client_id: nil,
12
+ shotgun_client_secret: nil,
13
+ }.freeze
14
+
15
+ include Singleton
16
+
17
+ def initialize
18
+ @config = Struct.new(*DEFAULT_CONFIG.keys).new(*DEFAULT_CONFIG.values)
19
+ end
20
+ attr_reader :config
21
+
22
+ class << self
23
+ extend Forwardable
24
+ # Remove the need to call .instance everywhere
25
+ def_delegators(
26
+ :instance,
27
+ :configure,
28
+ *DEFAULT_CONFIG.keys.flat_map{ |key| [key, "#{key}="] }
29
+ )
30
+ end
31
+
32
+ extend Forwardable
33
+ def_delegators(
34
+ :config,
35
+ *DEFAULT_CONFIG.keys.flat_map{ |key| [key, "#{key}="] }
36
+ )
37
+
38
+ def configure(&block)
39
+ return unless block
40
+
41
+ yield(@config)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+
5
+ module ActiveShotgun
6
+ module Model
7
+ def self.included(base_class)
8
+ base_class.include(ActiveModel::Validations)
9
+ base_class.extend(ActiveModel::Callbacks)
10
+ base_class.include(ActiveModel::Dirty)
11
+ base_class.include(ActiveModel::Serialization)
12
+ base_class.include(ActiveModel::Serializers::JSON)
13
+ base_class.extend(ActiveModel::Naming)
14
+
15
+ name = base_class.name
16
+ fetched_attributes = Client.fetch_field_names_for_an_entity_type(name)
17
+
18
+ writable_fetched_attributes = fetched_attributes[:writable]
19
+ readable_fetched_attributes = fetched_attributes[:readable]
20
+
21
+ base_class.instance_eval do
22
+ define_attribute_methods(*writable_fetched_attributes)
23
+ attr_reader(*readable_fetched_attributes, *writable_fetched_attributes)
24
+
25
+ define_singleton_method(:shotgun_type) do
26
+ name
27
+ end
28
+
29
+ define_singleton_method(:shotgun_readable_fetched_attributes) do
30
+ readable_fetched_attributes
31
+ end
32
+
33
+ define_singleton_method(:shotgun_writable_fetched_attributes) do
34
+ writable_fetched_attributes
35
+ end
36
+
37
+ define_singleton_method(:shotgun_client) do
38
+ Client.shotgun.entities(shotgun_type)
39
+ end
40
+
41
+ define_method(:shotgun_client) do
42
+ self.class.shotgun_client
43
+ end
44
+
45
+ writable_fetched_attributes.each do |attribute|
46
+ define_method("#{attribute}=") do |value|
47
+ send("#{attribute}_will_change!")
48
+ instance_variable_set("@#{attribute}", value)
49
+ end
50
+ end
51
+ end
52
+
53
+ base_class.include(Read)
54
+ base_class.extend(Read::ClassMethods)
55
+ base_class.include(Write)
56
+ base_class.extend(Write::ClassMethods)
57
+ base_class.include(Delete)
58
+ base_class.extend(Delete::ClassMethods)
59
+ base_class.prepend(Validations)
60
+ base_class.prepend(Callbacks)
61
+ end
62
+
63
+ module Write
64
+ def persisted?
65
+ !!id
66
+ end
67
+
68
+ def save
69
+ return false unless changed?
70
+
71
+ sg_result =
72
+ if persisted?
73
+ shotgun_client.update(id, changes.transform_values(&:last))
74
+ else
75
+ shotgun_client.create(changes.transform_values(&:last))
76
+ end
77
+ override_attributes!(sg_result.attributes.to_h.merge(id: sg_result.id))
78
+ changes_applied
79
+ true
80
+ end
81
+ alias_method :save!, :save
82
+
83
+ def mass_assign(assigned_attributes)
84
+ assigned_attributes.
85
+ transform_keys(&:to_sym).
86
+ slice(*self.class.shotgun_writable_fetched_attributes).
87
+ each do |k, v|
88
+ public_send("#{k}=", v)
89
+ end
90
+ end
91
+
92
+ def update(updated_attributes)
93
+ mass_assign(updated_attributes)
94
+ save
95
+ end
96
+
97
+ def update!(updated_attributes)
98
+ mass_assign(updated_attributes)
99
+ save!
100
+ end
101
+
102
+ module ClassMethods
103
+ def create(create_attributes)
104
+ new_entity = new
105
+ new_entity.mass_assign(create_attributes)
106
+ new_entity.save
107
+ end
108
+
109
+ def create!(create_attributes)
110
+ new_entity = new
111
+ new_entity.mass_assign(create_attributes)
112
+ new_entity.save!
113
+ end
114
+ end
115
+
116
+ private
117
+
118
+ def override_attributes!(new_attributes)
119
+ new_attributes.
120
+ transform_keys(&:to_sym).
121
+ slice(*self.class.shotgun_readable_fetched_attributes).
122
+ each do |k, v|
123
+ instance_variable_set("@#{k}", v)
124
+ end
125
+ end
126
+ end
127
+
128
+ module Validations
129
+ def save
130
+ validate && super
131
+ end
132
+
133
+ def save!
134
+ validate!
135
+ super
136
+ end
137
+ end
138
+
139
+ module Callbacks
140
+ def self.prepended(base)
141
+ base.define_model_callbacks :destroy
142
+ base.define_model_callbacks :update
143
+ base.define_model_callbacks :save
144
+ base.define_model_callbacks :create
145
+ base.define_model_callbacks :validation
146
+ end
147
+
148
+ def destroy
149
+ run_callbacks(:destroy) do
150
+ super
151
+ end
152
+ end
153
+
154
+ def save
155
+ run_callbacks(:save) do
156
+ run_callbacks(persisted? ? :update : :create) do
157
+ super
158
+ end
159
+ end
160
+ end
161
+
162
+ def validate
163
+ run_callbacks(:validation) do
164
+ super
165
+ end
166
+ end
167
+ end
168
+
169
+ module Delete
170
+ def delete
171
+ shotgun_client.delete(id)
172
+ end
173
+ alias_method :destroy, :delete
174
+
175
+ module ClassMethods
176
+ def revive(id)
177
+ shotgun_client.revive(id)
178
+ end
179
+ end
180
+ end
181
+
182
+ module Read
183
+ def attributes
184
+ self.class.shotgun_readable_fetched_attributes.map do |attribute|
185
+ [attribute, instance_variable_get("@#{attribute}")]
186
+ end.to_h
187
+ end
188
+
189
+ def writable_attributes
190
+ self.class.shotgun_writable_fetched_attributes.map do |attribute|
191
+ [attribute, instance_variable_get("@#{attribute}")]
192
+ end.to_h
193
+ end
194
+
195
+ def initialize(new_attributes = {})
196
+ new_attributes.slice(*self.class.shotgun_readable_fetched_attributes).each do |attribute, value|
197
+ instance_variable_set("@#{attribute}", value)
198
+ end
199
+ end
200
+
201
+ module ClassMethods
202
+ def first
203
+ sg_result = shotgun_client.first
204
+ new(sg_result.attributes.to_h.merge(id: sg_result.id))
205
+ end
206
+
207
+ def find(id)
208
+ sg_result = shotgun_client.find(id)
209
+ new(sg_result.attributes.to_h.merge(id: sg_result.id))
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveShotgun
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_shotgun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis <Zaratan> Pasin
@@ -10,6 +10,34 @@ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2020-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: shotgun_api_ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.8.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.8.2
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: zeitwerk
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +80,20 @@ dependencies:
52
80
  - - ">="
53
81
  - !ruby/object:Gem::Version
54
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
55
97
  - !ruby/object:Gem::Dependency
56
98
  name: overcommit
57
99
  requirement: !ruby/object:Gem::Requirement
@@ -221,6 +263,9 @@ files:
221
263
  - bin/console
222
264
  - bin/setup
223
265
  - lib/active_shotgun.rb
266
+ - lib/active_shotgun/client.rb
267
+ - lib/active_shotgun/config.rb
268
+ - lib/active_shotgun/model.rb
224
269
  - lib/active_shotgun/version.rb
225
270
  homepage: https://github.com/zaratan/active_shotgun
226
271
  licenses: