Class: FmRest::Spyke::Relation

Inherits:
Spyke::Relation
  • Object
show all
Defined in:
lib/fmrest/spyke/relation.rb

Constant Summary collapse

SORT_PARAM_MATCHER =
/(.*?)(!|__desc(?:end)?)?\Z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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_valueObject

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_valueObject

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_paramsObject

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_paramsObject

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_paramsObject

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_oneFmRest::Spyke::Base

Finds a single instance of the model by forcing limit = 1

Returns:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • value (Integer)

    the limit value

Returns:



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.

Parameters:

  • value (Integer)

    the offset value

Returns:



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.

Examples:

Person.sort(:first_name, :age!)
Person.sort(:first_name, :age__desc)
Person.sort(:first_name, :age__descend)
Person.sort({ fieldName: "FirstName" }, { fieldName: "Age", sortOrder: "descend" })

Parameters:

  • args (Array<Symbol, Hash>)

    the names of attributes to sort by with optional ! or __desc suffix, or a hash of options as expected by the FM Data API

Returns:



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