Class: Hoodoo::Services::Request::ListParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/services/services/request.rb

Overview

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’).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListParameters

Set up defaults in this instance.



59
60
61
62
63
64
65
# 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

Instance Attribute Details

#filter_dataObject

List filter key/value pairs as a hash, all keys/values strings; {} if there’s no filter data in the request URI query string.



55
56
57
# File 'lib/hoodoo/services/services/request.rb', line 55

def filter_data
  @filter_data
end

#limitObject

List page size, for index views; an integer; always defined.



40
41
42
# File 'lib/hoodoo/services/services/request.rb', line 40

def limit
  @limit
end

#offsetObject

List offset, for index views; an integer; always defined.



36
37
38
# File 'lib/hoodoo/services/services/request.rb', line 36

def offset
  @offset
end

#search_dataObject

List search key/value pairs as a hash, all keys/values strings; {} if there’s no search data in the request URI query string.



50
51
52
# File 'lib/hoodoo/services/services/request.rb', line 50

def search_data
  @search_data
end

#sort_dataObject

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.



45
46
47
# File 'lib/hoodoo/services/services/request.rb', line 45

def sort_data
  @sort_data
end

Instance Method Details

#from_h!(hash) ⇒ Object

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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

#to_hObject

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 attribute #sort_data)

  • direction (values from the Hash under #sort_data)

  • search (deep-duplicated value of attribute #search_data)

  • filter (deep-duplicated value of attribute #filter_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!.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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