Class: Hoodoo::Services::Interface::ToListDSL
- Inherits:
-
Object
- Object
- Hoodoo::Services::Interface::ToListDSL
- Defined in:
- lib/hoodoo/services/services/interface.rb
Overview
Implementation of the DSL that’s written inside a block passed to Hoodoo::Services::Interface#to_list. This is an internal implementation class. Instantiate with a Hoodoo::Services::Interface::ToList instance, the data in which is updated as the DSL methods run.
Instance Method Summary collapse
-
#default(sort_key) ⇒ Object
Used in conjunction with #sort.
-
#filter(*filter) ⇒ Object
As #search, but for filtering.
-
#initialize(hoodoo_interface_to_list_instance, &block) ⇒ ToListDSL
constructor
Initialize an instance and run the DSL methods.
-
#limit(limit) ⇒ Object
Specify the page size (limit) for lists.
-
#search(*search) ⇒ Object
Specify supported search keys in an array.
-
#sort(sort) ⇒ Object
Specify extra sort keys and orders that add with whatever platform common defaults are already in place.
Constructor Details
#initialize(hoodoo_interface_to_list_instance, &block) ⇒ ToListDSL
Initialize an instance and run the DSL methods.
hoodoo_interface_to_list_instance
-
Instance of Hoodoo::Services::Interface::ToList to update with data from DSL method calls.
- &block
-
Block of code that makes calls to the DSL herein.
On exit, the DSL is run and the Hoodoo::Services::Interface::ToList has been updated.
135 136 137 138 139 140 141 142 143 |
# File 'lib/hoodoo/services/services/interface.rb', line 135 def initialize( hoodoo_interface_to_list_instance, &block ) @tl = hoodoo_interface_to_list_instance # Shorthand! unless @tl.instance_of?( Hoodoo::Services::Interface::ToList ) raise "Hoodoo::Services::Interface::ToListDSL\#initialize requires a Hoodoo::Services::Interface::ToList instance - got '#{ @tl.class }'" end self.instance_eval( &block ) end |
Instance Method Details
#default(sort_key) ⇒ Object
Used in conjunction with #sort. Specifies that a sort key should be the default sort order for the interface.
Example - add sort key ‘code
’ with directions :asc
and :desc
, plus sort key :member
which only supports direction :asc
. Say that ‘code
’ is to be the default sort order.
sort default( :code ) => [ :asc, :desc ],
:member => [ :asc ]
207 208 209 210 211 212 213 214 |
# File 'lib/hoodoo/services/services/interface.rb', line 207 def default( sort_key ) unless sort_key.is_a?( ::String ) || sort_key.is_a?( ::Symbol ) raise "Hoodoo::Services::Interface::ToListDSL\#default requires a String or Symbol - got '#{ sort_key.class }'" end @tl.send( :default_sort_key=, sort_key.to_s ) return sort_key end |
#filter(*filter) ⇒ Object
As #search, but for filtering.
filter
-
Array of permitted filter keys, as symbols or strings. The order of array entries is arbitrary.
242 243 244 |
# File 'lib/hoodoo/services/services/interface.rb', line 242 def filter( *filter ) @tl.send( :filter=, filter.map { | item | item.to_s } ) end |
#limit(limit) ⇒ Object
Specify the page size (limit) for lists.
limit
-
Page size (integer).
Example:
limit 100
153 154 155 156 157 158 159 |
# File 'lib/hoodoo/services/services/interface.rb', line 153 def limit( limit ) unless limit.is_a?( ::Integer ) raise "Hoodoo::Services::Interface::ToListDSL\#limit requires an Integer - got '#{ limit.class }'" end @tl.send( :limit=, limit ) end |
#search(*search) ⇒ Object
Specify supported search keys in an array. The middleware will make sure the interface implementation is only called with search keys in that list. If a client attempts a search on an unsupported key, their request will be rejected by the middleware.
If a service wants to do its own search validation, it should not list call here. Note also that only the keys are specified and validated; value escaping and validation, if necessary, is up to the service implementation.
search
-
Array of permitted search keys, as symbols or strings. The order of array entries is arbitrary.
Example - allow searches specifying first_name
and last_name
keys:
search :first_name, :last_name
233 234 235 |
# File 'lib/hoodoo/services/services/interface.rb', line 233 def search( *search ) @tl.send( :search=, search.map { | item | item.to_s } ) end |
#sort(sort) ⇒ Object
Specify extra sort keys and orders that add with whatever platform common defaults are already in place.
sort
-
Hash of sort keys, with values that are an array of supported sort directions. The first array entry is used as the default direction if no direction is specified in the client caller’s query string. Use strings or symbols.
To specify that a sort key should be the new default for the interface in question, wrap it in a call to the #default DSL method.
Example - add sort key ‘code
’ with directions :asc
and :desc
, plus sort key :member
which only supports direction :asc
.
sort :code => [ :asc, :desc ],
:member => [ :asc ]
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/hoodoo/services/services/interface.rb', line 179 def sort( sort ) unless sort.is_a?( ::Hash ) raise "Hoodoo::Services::Interface::ToListDSL\#sort requires a Hash - got '#{ sort.class }'" end # Convert Hash keys to Strings and Arrays to Sets of Strings too. sort = sort.inject( {} ) do | memo, ( k, v ) | memo[ k.to_s ] = Set.new( v.map do | entry | entry.to_s end ) memo end merged = @tl.sort().merge( sort ) @tl.send( :sort=, merged ) end |