Module: Sinatra::Muster::Helpers

Defined in:
lib/sinatra/muster.rb

Overview

Sinatra helpers for working with Muster query string parsing strategies

Examples:


require 'sinatra/base'
require 'sinatra/muster'

class MyApp < Sinatra::Base
  register Sinatra::Muster

  use Muster::Rack, Muster::Strategies::Hash

  #  GET /?select=id,name,password
  get '/' do
    query_filter :select, :only => ['id', 'name', 'email']

    selected = query[:select]  #=> ['id', 'name']

    Users.all(:fields => selected)
  end
end

Instance Method Summary collapse

Instance Method Details

#queryMuster::Results

Returns the parsed and filtered query options from Muster::Rack

Examples:


#  GET /?select=id,name,password
get '/' do
  query_filter :select, :only => ['id', 'name', 'email']

  selected = query[:select]  #=> ['id', 'name']

  Users.all(:fields => selected)
end

Returns:

  • (Muster::Results)


73
74
75
# File 'lib/sinatra/muster.rb', line 73

def query
  muster_query.filtered
end

#query_filter(key, *options) ⇒ void

This method returns an undefined value.

Add a filter to be applied to the data in #query results

If you pass a scalar value instead of a Hash into options, it will be treated as the default, just like Hash#fetch does.

If you pass nothing into the options argument, it will return all values if the key exists or raise a KeyError like Hash#fetch.

Examples:


results.add_filter(:select, :only => [:id, :name])
results.add_filter(:select, :except => [:id])
results.add_filter(:page, 1)

Parameters:

  • key (String, Symbol)

    the key of the values in #query to filter

  • options (optional, Hash)

    the options available for this filter

Options Hash (*options):

  • :only (optional)

    when specified, only return the matching values If you specify a single value, a single value will be returned If you specify an Array of values, an Array will be returned, even if only one value matches

  • :except (optional)

    return all values except the ones given here If the raw data value is a single value, a single value will be returned If the raw data value is an Array, and array will be returned, even if all values are excluded If nothing was excluded, the raw value is returned as-is



55
56
57
# File 'lib/sinatra/muster.rb', line 55

def query_filter( key, *options )
  muster_query.add_filter( key, *options )
end