Class: FmRest::Spyke::Relation
- Inherits:
-
Spyke::Relation
- Object
- Spyke::Relation
- FmRest::Spyke::Relation
- Defined in:
- lib/fmrest/spyke/relation.rb
Constant Summary collapse
- SORT_PARAM_MATCHER =
/(.*?)(!|__desc(?:end)?)?\Z/.freeze
Instance Attribute Summary collapse
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#offset_value ⇒ Object
Returns the value of attribute offset_value.
-
#portal_params ⇒ Object
Returns the value of attribute portal_params.
-
#query_params ⇒ Object
Returns the value of attribute query_params.
-
#sort_params ⇒ Object
Returns the value of attribute sort_params.
Instance Method Summary collapse
-
#find_one ⇒ FmRest::Spyke::Base
Finds a single instance of the model by forcing limit = 1.
-
#has_query? ⇒ Boolean
Whether a query was set on this relation.
-
#initialize(*_args) ⇒ Relation
constructor
A new instance of Relation.
-
#limit(value) ⇒ FmRest::Spyke::Relation
A new relation with the limit applied.
-
#offset(value) ⇒ FmRest::Spyke::Relation
A new relation with the offset applied.
- #omit(params) ⇒ Object
- #portal(*args) ⇒ Object (also: #includes)
- #query(*params) ⇒ Object
-
#sort(*args) ⇒ FmRest::Spyke::Relation
(also: #order)
Allows sort params given in either hash format (using FM Data API's format), or as a symbol, in which case the of the attribute must match a known mapped attribute, optionally suffixed with
!
or__desc
to signify it should use descending order.
Constructor Details
#initialize(*_args) ⇒ Relation
Returns a new instance of Relation.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/fmrest/spyke/relation.rb', line 17 def initialize(*_args) super @limit_value = klass.default_limit if klass.default_sort.present? @sort_params = Array.wrap(klass.default_sort).map { |s| normalize_sort_param(s) } end @query_params = [] @portal_params = [] end |
Instance Attribute Details
#limit_value ⇒ Object
Returns the value of attribute limit_value.
14 15 16 |
# File 'lib/fmrest/spyke/relation.rb', line 14 def limit_value @limit_value end |
#offset_value ⇒ Object
Returns the value of attribute offset_value.
14 15 16 |
# File 'lib/fmrest/spyke/relation.rb', line 14 def offset_value @offset_value end |
#portal_params ⇒ Object
Returns the value of attribute portal_params.
14 15 16 |
# File 'lib/fmrest/spyke/relation.rb', line 14 def portal_params @portal_params end |
#query_params ⇒ Object
Returns the value of attribute query_params.
14 15 16 |
# File 'lib/fmrest/spyke/relation.rb', line 14 def query_params @query_params end |
#sort_params ⇒ Object
Returns the value of attribute sort_params.
14 15 16 |
# File 'lib/fmrest/spyke/relation.rb', line 14 def sort_params @sort_params end |
Instance Method Details
#find_one ⇒ FmRest::Spyke::Base
Finds a single instance of the model by forcing limit = 1
91 92 93 94 95 96 |
# File 'lib/fmrest/spyke/relation.rb', line 91 def find_one return super if params[klass.primary_key].present? @find_one ||= klass.new_collection_from_result(limit(1).fetch).first rescue ::Spyke::ConnectionError => error fallback_or_reraise(error, default: nil) end |
#has_query? ⇒ Boolean
Returns whether a query was set on this relation.
84 85 86 |
# File 'lib/fmrest/spyke/relation.rb', line 84 def has_query? query_params.present? end |
#limit(value) ⇒ FmRest::Spyke::Relation
Returns a new relation with the limit applied.
32 33 34 |
# File 'lib/fmrest/spyke/relation.rb', line 32 def limit(value) with_clone { |r| r.limit_value = value } end |
#offset(value) ⇒ FmRest::Spyke::Relation
Returns a new relation with the offset applied.
39 40 41 |
# File 'lib/fmrest/spyke/relation.rb', line 39 def offset(value) with_clone { |r| r.offset_value = value } end |
#omit(params) ⇒ Object
79 80 81 |
# File 'lib/fmrest/spyke/relation.rb', line 79 def omit(params) query(params.merge(omit: true)) end |
#portal(*args) ⇒ Object Also known as: includes
65 66 67 68 69 70 |
# File 'lib/fmrest/spyke/relation.rb', line 65 def portal(*args) with_clone do |r| r.portal_params += args.flatten.map { |p| normalize_portal_param(p) } r.portal_params.uniq! end end |
#query(*params) ⇒ Object
73 74 75 76 77 |
# File 'lib/fmrest/spyke/relation.rb', line 73 def query(*params) with_clone do |r| r.query_params += params.flatten.map { |p| normalize_query_params(p) } end end |
#sort(*args) ⇒ FmRest::Spyke::Relation Also known as: order
Allows sort params given in either hash format (using FM Data API's
format), or as a symbol, in which case the of the attribute must match
a known mapped attribute, optionally suffixed with !
or __desc
to
signify it should use descending order.
58 59 60 61 62 |
# File 'lib/fmrest/spyke/relation.rb', line 58 def sort(*args) with_clone do |r| r.sort_params = args.flatten.map { |s| normalize_sort_param(s) } end end |