active_shotgun 0.0.2.1 → 0.0.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
  SHA256:
3
- metadata.gz: 3abb67e7c3b8be946a8f22ed9e4c7829446b3aea28fc40e7c8bafdd1a79d063c
4
- data.tar.gz: cf80eac1abcc79a182a88e8bb5850d02554cb51f7fdce66c1231c9d10b010722
3
+ metadata.gz: b1e2345b2533af34895ef3e5ba10ab8c161f24ad36edb318d4e101747dddeb9e
4
+ data.tar.gz: 39e87e5dfc26b9697094d2b826518ac9fa9a6021e24dcab684b56f08989720b1
5
5
  SHA512:
6
- metadata.gz: 316e77ebbe27f4c708f8bb1849b4cb79426b79aa319cc9952e42617dedef13841fb0204612c8abed1a5a87e47052441176313945c03199fcf32d0d4beb3c7f83
7
- data.tar.gz: b56bbd475751025fa8841281550ab33078bad9a88bfdc58af13157f516bb415c6db545f2286eae049694e403c1ebe0d5f575fff295bc69593d03af752a4cf7a6
6
+ metadata.gz: c37bf9627795d8a13f32755a0120fbe28b521b35d242e99727f3e15ceb9150b80928361f699ac4481d3f7424dea5418864e1852b957dc7285d6402d4f6f2139d
7
+ data.tar.gz: 2e949a1793fcd76a7cbf16fa894b9d23f5fe07eae3feaa0a035cddde61e38da69d102076df29935fd4b9b64dbc3859241e60f66a8e14cce3476424eb00e51190
@@ -6,6 +6,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.0.3] - 2020-12-16
10
+ ### Added
11
+ - First version of AR complient queries. Supports:
12
+ - find
13
+ - first(number)
14
+ - find_by
15
+ - all
16
+ - where => Partial support. Hash only.
17
+ - orders
18
+ - limit
19
+ - offset
20
+ - select
21
+ - pluck
22
+ - Behave as a Query as long as possible. Behave like an Array as soon as you look at the data.
23
+ ### Missing
24
+ - multi-entity, entity
25
+ - count, extra methods
26
+
9
27
  ## [0.0.2.1] - 2020-12-16
10
28
  ### Changed
11
29
  - Not everything is in one big file now
@@ -24,7 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
24
42
  ### Added
25
43
  - Gem init
26
44
 
27
- [Unreleased]: https://github.com/zaratan/active_shotgun/compare/v0.0.2.1...HEAD
45
+ [Unreleased]: https://github.com/zaratan/active_shotgun/compare/v0.0.3...HEAD
46
+ [0.0.3]: https://github.com/zaratan/active_shotgun/releases/tag/v0.0.3
28
47
  [0.0.2.1]: https://github.com/zaratan/active_shotgun/releases/tag/v0.0.2.1
29
48
  [0.0.2]: https://github.com/zaratan/active_shotgun/releases/tag/v0.0.2
30
49
  [0.0.1]: https://github.com/zaratan/active_shotgun/releases/tag/v0.0.1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_shotgun (0.0.2.1)
4
+ active_shotgun (0.0.3)
5
5
  activemodel (>= 5.2)
6
6
  shotgun_api_ruby (>= 0.0.8.2)
7
7
  zeitwerk (~> 2)
@@ -21,10 +21,41 @@ module ActiveShotgun
21
21
  end
22
22
  end
23
23
 
24
+ def reload
25
+ self.class.find(id)
26
+ end
27
+
24
28
  module ClassMethods
25
- def first
26
- sg_result = shotgun_client.first
27
- new(sg_result.attributes.to_h.merge(id: sg_result.id))
29
+ def all
30
+ prepare_new_query.all
31
+ end
32
+
33
+ def limit(number)
34
+ prepare_new_query.limit(number)
35
+ end
36
+
37
+ def offset(number)
38
+ prepare_new_query.limit(number)
39
+ end
40
+
41
+ def first(number = 1)
42
+ prepare_new_query.first(number)
43
+ end
44
+
45
+ def where(conditions)
46
+ prepare_new_query.where(conditions)
47
+ end
48
+
49
+ def find_by(conditions)
50
+ prepare_new_query.find_by(conditions)
51
+ end
52
+
53
+ def orders(new_orders)
54
+ prepare_new_query.orders(new_orders)
55
+ end
56
+
57
+ def prepare_new_query
58
+ Query.new(type: shotgun_type, klass: self)
28
59
  end
29
60
 
30
61
  def find(id)
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveShotgun
4
+ class Query
5
+ def initialize(type:, klass:)
6
+ @type = type
7
+ @klass = klass
8
+ @conditions = nil
9
+ @limit = 100
10
+ @offset = 0
11
+ @orders = nil
12
+ @fields = nil
13
+ end
14
+
15
+ extend Forwardable
16
+ def_delegators(
17
+ :to_a,
18
+ :each
19
+ )
20
+ include Enumerable
21
+
22
+ def all
23
+ page_calc = format_page_from_limit_and_offset(@limit, @offset)
24
+ results = shotgun_client.all(
25
+ fields: @fields,
26
+ sort: @orders,
27
+ filter: @conditions,
28
+ page: page_calc[:page],
29
+ page_size: page_calc[:page_size]
30
+ )
31
+ results.pop(page_calc[:end_trim])
32
+ results.shift(page_calc[:start_trim])
33
+
34
+ results.map{ |result| @klass.new(result.attributes.to_h.merge(id: result.id)) }
35
+ end
36
+ alias_method :to_a, :all
37
+
38
+ def first(number = 1)
39
+ results = limit(number).all
40
+ if @limit == 1
41
+ results.first
42
+ else
43
+ results
44
+ end
45
+ end
46
+
47
+ def limit(number)
48
+ @limit = number
49
+ self
50
+ end
51
+
52
+ def offset(number)
53
+ @offset = number
54
+ self
55
+ end
56
+
57
+ def where(conditions)
58
+ @conditions = (@conditions || {}).merge(conditions)
59
+ self
60
+ end
61
+
62
+ def find_by(conditions)
63
+ where(conditions).first
64
+ end
65
+
66
+ def orders(new_orders)
67
+ @orders = new_orders
68
+ self
69
+ end
70
+
71
+ def select(*fields)
72
+ @fields = fields.flatten
73
+ self
74
+ end
75
+
76
+ def pluck(*fields)
77
+ fields.flatten!
78
+ result = select(fields).map{ |e| fields.map{ |field| e.public_send(field) } }
79
+ fields.size == 1 ? result.flatten : result
80
+ end
81
+
82
+ private
83
+
84
+ def format_page_from_limit_and_offset(limit, offset)
85
+ min = (offset + 1).to_f
86
+ max = (offset + limit).to_f
87
+ limit.upto(limit + offset) do |size|
88
+ next unless (min / size).ceil == (max / size).ceil
89
+
90
+ page = (min / size).ceil
91
+ start_trim = min - (page - 1) * size - 1
92
+ end_trim = page * size - max
93
+ return {
94
+ page_size: [size, 1000].min,
95
+ page: page,
96
+ start_trim: start_trim.to_i,
97
+ end_trim: end_trim.to_i,
98
+ }
99
+ end
100
+ end
101
+
102
+ def shotgun_client
103
+ Client.shotgun.entities(@type)
104
+ end
105
+ end
106
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveShotgun
4
- VERSION = "0.0.2.1"
4
+ VERSION = "0.0.3"
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.2.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis <Zaratan> Pasin
@@ -271,6 +271,7 @@ files:
271
271
  - lib/active_shotgun/model/read.rb
272
272
  - lib/active_shotgun/model/validations.rb
273
273
  - lib/active_shotgun/model/write.rb
274
+ - lib/active_shotgun/query.rb
274
275
  - lib/active_shotgun/version.rb
275
276
  homepage: https://github.com/zaratan/active_shotgun
276
277
  licenses: