fmrest 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0334b98968b68371d61a1f46c010937feb87bb5137b6e688c0096daf73b168a
4
- data.tar.gz: c6585502bbb1a8005ea0f8a618c47d53b40b9412778a51e8154ab79393814ce5
3
+ metadata.gz: 7eb7073bb86836108750364857226e2ce6eaac9c88348c74b0c82ecb446f5d4e
4
+ data.tar.gz: 7e5d6bee538d00fce9feea4f0666014853fe4feba67ea11ff1841809a7d6c577
5
5
  SHA512:
6
- metadata.gz: 433e6b05959fd64afbffddd2065df862449b3a3dcebe13780182b28bd6847f6263d4b2faffa3cef7d0a0f7949005ec1a1b9e8f7825129868bf49bd3833784ab5
7
- data.tar.gz: 542d824f1cbb3c87722105a24b6bf2fdecd0d522a1a6d796d660a4c806ec83b356c50f400c9d37ce472cd2c4781ea1a50ecc533c361c4d6c02e19d47d625fb47
6
+ metadata.gz: 2e59d89d5829d94566be859060c10e88c875b3298bdd15dbc61b289df64b69534f3d294528b6b3a697b1583cf149472030fcc370eee97ddd0af0d03804520bec
7
+ data.tar.gz: 057a8bca299892de5d4415b2c67bec71a15778c8e80567726e44c9413bd7b75cf7d422bf86626b24a0021b2c5ca9eb7e43cf4eb486217e176133c19d3a509554
@@ -1,5 +1,10 @@
1
1
  ## Changelog
2
2
 
3
+ ### 0.4.0
4
+
5
+ * Implement ability to set limit and offset for portals
6
+ * Implement disabling and requesting all portals
7
+
3
8
  ### 0.3.3
4
9
 
5
10
  * Fix encoding of paths for layouts with brackets in them (e.g. `"\[Very Ugly\]
data/README.md CHANGED
@@ -447,22 +447,27 @@ passing arbitrary parameters to the REST backend. fmrest-ruby however is well
447
447
  aware of its backend API, so it extends Spkye models with a bunch of useful
448
448
  querying methods.
449
449
 
450
- ```ruby
451
- class Honeybee < Spyke::Base
452
- include FmRest::Spyke
450
+ #### .limit
453
451
 
454
- attributes name: "Bee Name", age: "Bee Age"
452
+ `.limit` sets the limit for get and find request:
455
453
 
456
- has_portal :hives, portal_key: "Bee Hives"
457
- end
454
+ ```ruby
455
+ Honeybee.limit(10)
458
456
  ```
459
457
 
460
- #### .limit
458
+ NOTE: You can also set a default limit value for a model class, see
459
+ [Other notes on querying](#other-notes-on-querying).
461
460
 
462
- `.limit` sets the limit for get and find request:
461
+ You can also use `.limit` to set limits on portals:
463
462
 
464
463
  ```ruby
465
- Honeybee.limit(10)
464
+ Honeybee.limit(hives: 3, flowers: 2)
465
+ ```
466
+
467
+ To remove the limit on a portal set it to `nil`:
468
+
469
+ ```ruby
470
+ Honeybee.limit(flowers: nil)
466
471
  ```
467
472
 
468
473
  #### .offset
@@ -473,6 +478,18 @@ Honeybee.limit(10)
473
478
  Honeybee.offset(10)
474
479
  ```
475
480
 
481
+ You can also use `.offset` to set offsets on portals:
482
+
483
+ ```ruby
484
+ Honeybee.offset(hives: 3, flowers: 2)
485
+ ```
486
+
487
+ To remove the offset on a portal set it to `nil`:
488
+
489
+ ```ruby
490
+ Honeybee.offset(flowers: nil)
491
+ ```
492
+
476
493
  #### .sort
477
494
 
478
495
  `.sort` (or `.order`) sets sorting options for get and find requests:
@@ -490,16 +507,44 @@ Honeybee.sort(:name, :age!)
490
507
  Honeybee.sort(:name, :age__desc)
491
508
  ```
492
509
 
510
+ NOTE: You can also set default sort values for a model class, see
511
+ [Other notes on querying](#other-notes-on-querying).
512
+
493
513
  #### .portal
494
514
 
495
- `.portal` (or `.includes`) sets the portals to fetch for get and find requests
496
- (this recognizes portals defined with `has_portal`):
515
+ `.portal` (aliased as `.includes` and `.portals`) sets which portals to fetch
516
+ (if any) for get and find requests (this recognizes portals defined with
517
+ `has_portal`):
497
518
 
498
519
  ```ruby
499
- Honeybee.portal(:hives)
520
+ Honeybee.portal(:hives) # include just the :hives portal
500
521
  Honeybee.includes(:hives) # alias method
522
+ Honeybee.portals(:hives, :flowers) # alias for pluralization fundamentalists
523
+ ```
524
+
525
+ Chaining calls to `.portal` will add portals to the existing included list:
526
+
527
+ ```ruby
528
+ Honeybee.portal(:flowers).portal(:hives) # include both portals
529
+ ```
530
+
531
+ If you want to disable portals for the scope call `.portal(false)`:
532
+
533
+ ```ruby
534
+ Honeybee.portal(false) # disable portals for this scope
535
+ ```
536
+
537
+ If you want to include all portals call `.portal(true)`:
538
+
539
+ ```ruby
540
+ Honeybee.portal(true) # include all portals
501
541
  ```
502
542
 
543
+ For convenience you can also use `.with_all_portals` and `.without_portals`,
544
+ which behave just as calling `.portal(true)` and `portal(false)` respectively.
545
+
546
+ NOTE: By default all portals are included.
547
+
503
548
  #### .query
504
549
 
505
550
  `.query` sets query conditions for a find request (and supports attributes as
@@ -553,8 +598,10 @@ Honeybee.limit(10).offset(20).sort(:name, :age!).portal(:hives).query(name: "Hut
553
598
  You can also set default values for limit and sort on the class:
554
599
 
555
600
  ```ruby
556
- Honeybee.default_limit = 1000
557
- Honeybee.default_sort = [:name, :age!]
601
+ class Honeybee < FmRest::Spyke::Base
602
+ self.default_limit = 1000
603
+ self.default_sort = [:name, :age!]
604
+ end
558
605
  ```
559
606
 
560
607
  Calling any `Enumerable` method on the resulting scope object will trigger a
@@ -672,7 +719,7 @@ end
672
719
  - [ ] Better/simpler-to-use core Ruby API
673
720
  - [ ] Better API documentation and README
674
721
  - [ ] Oauth support
675
- - [ ] Support for portal limit and offset
722
+ - [x] Support for portal limit and offset
676
723
  - [x] More options for token storage
677
724
  - [x] Support for container fields
678
725
  - [x] Optional logging
@@ -17,7 +17,9 @@ module FmRest
17
17
 
18
18
  class_methods do
19
19
  # Methods delegated to FmRest::Spyke::Relation
20
- delegate :limit, :offset, :sort, :query, :portal, to: :all
20
+ delegate :limit, :offset, :sort, :order, :query, :omit, :portal,
21
+ :portals, :includes, :with_all_portals, :without_portals,
22
+ to: :all
21
23
 
22
24
  def all
23
25
  # Use FmRest's Relation insdead of Spyke's vanilla one
@@ -66,9 +68,15 @@ module FmRest
66
68
  prefixed ? scope.sort_params.to_json : scope.sort_params
67
69
  end
68
70
 
69
- if scope.portal_params.present?
71
+ unless scope.included_portals.nil?
70
72
  where_options["portal"] =
71
- prefixed ? scope.portal_params.to_json : scope.portal_params
73
+ prefixed ? scope.included_portals.to_json : scope.included_portals
74
+ end
75
+
76
+ if scope.portal_params.present?
77
+ scope.portal_params.each do |portal_param, value|
78
+ where_options["#{prefix}#{portal_param}"] = value
79
+ end
72
80
  end
73
81
 
74
82
  scope.where(where_options)
@@ -12,7 +12,7 @@ module FmRest
12
12
 
13
13
 
14
14
  attr_accessor :limit_value, :offset_value, :sort_params, :query_params,
15
- :portal_params
15
+ :included_portals, :portal_params
16
16
 
17
17
  def initialize(*_args)
18
18
  super
@@ -24,20 +24,43 @@ module FmRest
24
24
  end
25
25
 
26
26
  @query_params = []
27
- @portal_params = []
27
+
28
+ @included_portals = nil
29
+ @portal_params = {}
28
30
  end
29
31
 
30
- # @param value [Integer] the limit value
31
- # @return [FmRest::Spyke::Relation] a new relation with the limit applied
32
- def limit(value)
33
- with_clone { |r| r.limit_value = value }
32
+ # @param value_or_hash [Integer, Hash] the limit value for this layout,
33
+ # or a hash with limits for the layout's portals
34
+ # @example
35
+ # Person.limit(10) # Set layout limit
36
+ # Person.limit(children: 10) # Set portal limit
37
+ # @return [FmRest::Spyke::Relation] a new relation with the limits
38
+ # applied
39
+ def limit(value_or_hash)
40
+ with_clone do |r|
41
+ if value_or_hash.respond_to?(:each)
42
+ r.set_portal_params(value_or_hash, :limit)
43
+ else
44
+ r.limit_value = value_or_hash
45
+ end
46
+ end
34
47
  end
35
48
 
36
- # @param value [Integer] the offset value
37
- # @return [FmRest::Spyke::Relation] a new relation with the offset
49
+ # @param value_or_hash [Integer, Hash] the offset value for this layout,
50
+ # or a hash with offsets for the layout's portals
51
+ # @example
52
+ # Person.offset(10) # Set layout offset
53
+ # Person.offset(children: 10) # Set portal offset
54
+ # @return [FmRest::Spyke::Relation] a new relation with the offsets
38
55
  # applied
39
- def offset(value)
40
- with_clone { |r| r.offset_value = value }
56
+ def offset(value_or_hash)
57
+ with_clone do |r|
58
+ if value_or_hash.respond_to?(:each)
59
+ r.set_portal_params(value_or_hash, :offset)
60
+ else
61
+ r.offset_value = value_or_hash
62
+ end
63
+ end
41
64
  end
42
65
 
43
66
  # Allows sort params given in either hash format (using FM Data API's
@@ -47,7 +70,7 @@ module FmRest
47
70
  #
48
71
  # @param args [Array<Symbol, Hash>] the names of attributes to sort by with
49
72
  # optional `!` or `__desc` suffix, or a hash of options as expected by
50
- # the FM Data API
73
+ # the FM Data API
51
74
  # @example
52
75
  # Person.sort(:first_name, :age!)
53
76
  # Person.sort(:first_name, :age__desc)
@@ -62,13 +85,45 @@ module FmRest
62
85
  end
63
86
  alias order sort
64
87
 
88
+ # Sets the portals to include with each record in the response.
89
+ #
90
+ # @param args [Array<Symbol, String>, true, false] the names of portals to
91
+ # include, or `false` to request no portals
92
+ # @example
93
+ # Person.portal(:relatives, :pets)
94
+ # Person.portal(false) # Disables portals
95
+ # Person.portal(true) # Enables portals (includes all)
96
+ # @return [FmRest::Spyke::Relation] a new relation with the portal
97
+ # options applied
65
98
  def portal(*args)
99
+ raise ArgumentError, "Call `portal' with at least one argument" if args.empty?
100
+
66
101
  with_clone do |r|
67
- r.portal_params += args.flatten.map { |p| normalize_portal_param(p) }
68
- r.portal_params.uniq!
102
+ if args.length == 1 && args.first.eql?(true) || args.first.eql?(false)
103
+ r.included_portals = args.first ? nil : []
104
+ else
105
+ r.included_portals ||= []
106
+ r.included_portals += args.flatten.map { |p| normalize_portal_param(p) }
107
+ r.included_portals.uniq!
108
+ end
69
109
  end
70
110
  end
71
111
  alias includes portal
112
+ alias portals portal
113
+
114
+ # Same as calling `portal(true)`
115
+ #
116
+ # @return (see #portal)
117
+ def with_all_portals
118
+ portal(true)
119
+ end
120
+
121
+ # Same as calling `portal(false)`
122
+ #
123
+ # @return (see #portal)
124
+ def without_portals
125
+ portal(false)
126
+ end
72
127
 
73
128
  def query(*params)
74
129
  with_clone do |r|
@@ -95,6 +150,28 @@ module FmRest
95
150
  fallback_or_reraise(error, default: nil)
96
151
  end
97
152
 
153
+ protected
154
+
155
+ def set_portal_params(params_hash, param)
156
+ # Copy portal_params so we're not modifying the same hash as the parent
157
+ # scope
158
+ self.portal_params = portal_params.dup
159
+
160
+ params_hash.each do |portal_name, value|
161
+ # TODO: Use a hash like { portal_name: { param: value } } instead so
162
+ # we can intelligently avoid including portal params for excluded
163
+ # portals
164
+ key = "#{param}.#{normalize_portal_param(portal_name)}"
165
+
166
+ # Delete key if value is falsy
167
+ if !value && portal_params.has_key?(key)
168
+ portal_params.delete(key)
169
+ else
170
+ self.portal_params[key] = value
171
+ end
172
+ end
173
+ end
174
+
98
175
  private
99
176
 
100
177
  def normalize_sort_param(param)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FmRest
4
- VERSION = "0.3.3"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fmrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Carbajal
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-02 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday