Class: Muster::Strategies::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/muster/strategies/rack.rb

Overview

Query string parsing strategy based on Rack::Utils#parse_query

Examples:


strategy = Muster::Strategies::Rack.new
results  = strategy.parse('name=value&choices=1&choices=2')  #=>  { 'name' => 'value', 'choices' => ['1', '2'] }

Direct Known Subclasses

ActiveRecord, Hash, Pagination

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Rack

Create a new Rack parsing strategy

Examples:


strategy = Muster::Strategies::Rack.new(:fields => [:name, :state])
strategy = Muster::Strategies::Rack.new(:field  => :name)

Parameters:

  • options (Hash) (defaults to: {})

    the options available for this method

Options Hash (options):

  • :fields (optional, Array<Symbol>)

    when specified, only parse the specified fields You may also use :field if you only intend to pass one field



36
37
38
39
40
41
# File 'lib/muster/strategies/rack.rb', line 36

def initialize( options={} )
  @options = options.with_indifferent_access

  @fields  = Array.wrap(@options[:field] || @options[:fields])
  @fields.map!{ |field| field.to_sym }
end

Instance Attribute Details

#fieldsHash (readonly)

Returns list of fields to parse, ignoring all others.

Returns:

  • (Hash)

    list of fields to parse, ignoring all others



24
25
26
# File 'lib/muster/strategies/rack.rb', line 24

def fields
  @fields
end

#optionsHash (readonly)

Returns options specified during initialization.

Returns:

  • (Hash)

    options specified during initialization



20
21
22
# File 'lib/muster/strategies/rack.rb', line 20

def options
  @options
end

Instance Method Details

#fields_to_parse(query_string) ⇒ Hash (protected)

Returns the list of fields to be parsed

If the :fields option was specified, only those fields will be returned. Otherwise, all fields will be returned.

Parameters:

  • query_string (String)

    the query string to parse

Returns:



78
79
80
81
82
83
84
85
86
# File 'lib/muster/strategies/rack.rb', line 78

def fields_to_parse( query_string )
  fields = self.parse_query_string(query_string)

  if self.fields.present?
    fields = fields.select{ |key, value| self.fields.include?(key.to_sym) }
  end

  return fields.with_indifferent_access
end

#parse(query_string) ⇒ Muster::Results

Processes a query string and returns a hash of its fields/values

Examples:


results = strategy.parse('name=value&choices=1&choices=1')  #=>  { 'name' => 'value', 'choices' => ['1', '2'] }

Parameters:

  • query_string (String)

    the query string to parse

Returns:



52
53
54
# File 'lib/muster/strategies/rack.rb', line 52

def parse( query_string )
  Muster::Results.new( self.fields_to_parse(query_string) )
end

#parse_query_string(query_string) ⇒ Hash (protected)

Converts the query string into a hash for processing

Examples:


fields = self.parse_query_string('name=value&choices=1&choices=1')  #=>  { 'name' => 'value', 'choices' => ['1', '2'] }

Parameters:

  • query_string (String)

    the query string being parsed

Returns:



67
68
69
# File 'lib/muster/strategies/rack.rb', line 67

def parse_query_string( query_string )
  ::Rack::Utils.parse_query(query_string).with_indifferent_access
end