class Hoodoo::Services::Request::ListParameters
Encapsulation of all parameters related only to modifying a list of results. Other parameters may modify lists too, but they also modify other representations (e.g. single-resource ‘show’).
Attributes
List filter key/value pairs as a hash, all keys/values strings; {} if there’s no filter data in the request URI query string.
List page size, for index views; an integer; always defined.
List offset, for index views; an integer; always defined.
List search key/value pairs as a hash, all keys/values strings; {} if there’s no search data in the request URI query string.
A Hash of String keys and values, where each key is a field for sorting and each value is the direction to sort that field.
Public Class Methods
Set up defaults in this instance.
# File lib/hoodoo/services/services/request.rb, line 59 def initialize self.offset = 0 self.limit = 50 self.sort_data = { 'created_at' => 'desc' } self.search_data = {} self.filter_data = {} end
Public Instance Methods
Load list parameters from a given Hash, of the form set by to_h
. Overwrites any corresponding internal attributes and takes full deep copies of sort, search and filter values.
# File lib/hoodoo/services/services/request.rb, line 112 def from_h!( hash ) self.offset = hash[ 'offset' ] if hash.has_key?( 'offset' ) self.limit = hash[ 'limit' ] if hash.has_key?( 'limit' ) self.search_data = Hoodoo::Utilities.deep_dup( hash[ 'search' ] ) if hash[ 'search' ].is_a?( Hash ) self.filter_data = Hoodoo::Utilities.deep_dup( hash[ 'filter' ] ) if hash[ 'filter' ].is_a?( Hash ) sorts = hash[ 'sort' ] directions = hash[ 'direction' ] # Ensure the values are Arrays not just simple e.g. Strings, # so that we can zip them up into a Hash for the 'sort_data' # attribute value. Merge the result onto the existing values. # sorts = [ sorts ] unless sorts.is_a?( Array ) directions = [ directions ] unless directions.is_a?( Array ) sorts.compact! directions.compact! sorts.concat( self.sort_data.keys[ sorts.count .. -1 ] || [] ) directions.concat( self.sort_data.values[ directions.count .. -1 ] || [] ) # The middleware enforces a URI query string match of the # count of sort and direction specifiers, so we do the same. # if sorts.length != directions.length raise 'Hoodoo::Services::Request::ListParameters#from_h!: Sort and direction array lengths must match' end self.sort_data = Hash[ sorts.zip( directions ) ] end
Represent the list data as a Hash, for uses such as persistence or loading into another session instance. The returned Hash is a full deep copy of any internal data; changing it will not alter the ListParameters
object state.
Top-level keys in the Hash are Strings corresponding to fields supported by the query Hash in Hoodoo::Client::Endpoint#list
, intentionally compatible so that pass-through / proxy scenarios from resource implementation to another resource are assisted:
-
offset
-
limit
-
sort
(keys from the Hash under attributesort_data
) -
direction
(values from the Hash undersort_data
) -
search
(deep-duplicated value of attributesearch_data
) -
filter
(deep-duplicated value of attributefilter_data
)
Sort, direction, search and filter data, if not empty, also have String keys / values. A single sort-direction key-value pair will be flattened to a simple value, while multiple sort-direction pairs are given as Arrays.
See also from_h!
.
# File lib/hoodoo/services/services/request.rb, line 91 def to_h sorts = self.sort_data.keys.map( & :to_s ) directions = self.sort_data.values.map( & :to_s ) sorts = sorts.first if sorts.count == 1 directions = directions.first if directions.count == 1 { 'offset' => self.offset, 'limit' => self.limit, 'sort' => sorts, 'direction' => directions, 'search' => Hoodoo::Utilities.deep_dup( self.search_data ), 'filter' => Hoodoo::Utilities.deep_dup( self.filter_data ) } end