Class: Muster::Results

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/muster/results.rb

Overview

Query parsed results helper class

As with most Muster classes, all hashes returned and options specified support with indifferent access. You can also access results using the dot notation for the key name.

Examples:


data = { :select => [:id, :name, :created_at] }
results = Muster::Results.new(data)

# Filter values one at a time
results.data                                     #=> { 'select' => [:id, :name, :created_at] }
results[:select]                                 #=> [:id, :name, :created_at]
results.select                                   #=> [:id, :name, :created_at]
results.filter(:select, :only => [:id, :name])   #=> [:id, :name]
results.filter(:select, :except => :created_at)  #=> [:id, :name]
results.filter(:page, 1)                         #=> 1
results.filter(:page)                            #=> KeyError: :page does not exist

# Filter values in one pass
results.add_filter(:select, :only => [:id, :name])
results.add_filter(:page, 1)
results.filtered                                 #=> { 'select' => [:id, :name], 'page' => 1 }
results.filtered[:select]                        #=> [:id, :name]
results.filtered.select                          #=> [:id, :name]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Results

Create a new results instance

Examples:


data = { :select => [:id, :name, :created_at] }
results = Muster::Results.new(data)

Parameters:

  • data (Hash)

    the raw parsed query string data

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

    the options available for this method They’e aren’t any options yet. :-)



54
55
56
57
58
59
# File 'lib/muster/results.rb', line 54

def initialize( data, options={} )
  super(data)

  @data = data
  @filters = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/muster/results.rb', line 158

def method_missing(meth, *args, &block)
  if self.has_key?(meth)
    value = self[meth]

    if value.kind_of?(Hash)
      value.instance_eval do
        def method_missing(meth, *args, &block)
          if self.has_key?(meth)
            return self.fetch(meth)
          end

          super
        end
      end
    end

    return value
  end

  super
end

Instance Attribute Details

#dataHash (readonly)

Returns raw data specified during initialization.

Returns:

  • (Hash)

    raw data specified during initialization



38
39
40
# File 'lib/muster/results.rb', line 38

def data
  @data
end

#filtersHash (readonly)

Returns filters specified using #add_filter.

Returns:



42
43
44
# File 'lib/muster/results.rb', line 42

def filters
  @filters
end

Instance Method Details

#add_filter(key, *options) ⇒ void

This method returns an undefined value.

Add a filter to be applied to the data in #filtered 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 #data 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



86
87
88
# File 'lib/muster/results.rb', line 86

def add_filter( key, *options )
  self.filters[key] = options
end

#filter(key, *options) ⇒ void

This method returns an undefined value.

Filters and returns the raw data values for the specifid key and options

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:


data = { :select => [:id, :name, :created_at] }
results = Muster::Results.new(data)
results.filter(:select)                            #=> [:id, :name, :created_at]
results.filter(:select, :only => :name)            #=> :name
results.filter(:select, :only => [:other, :name])  #=> [:name]
results.filter(:other, :default)                   #=> :default
results.filter(:other)                             #=> KeyError

Parameters:

  • key (String, Symbol)

    the key of the values in #data 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



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/muster/results.rb', line 142

def filter( key, *options )
  if options.present? && options.first.instance_of?(Hash)
    options = options.first.with_indifferent_access

    if options.has_key?(:only)
      return filter_only_values( key, options[:only] )
    elsif options.has_key?(:except)
      return filter_excluded_values( key, options[:except] )
    end
  else
    return self.fetch(key, *options)
  end
end

#filteredMuster::Results

Returns the raw data with all of the filters applied

If no filters were added, this method simply returns self.

Examples:


results.add_filter(:select, :only => [:id, :name])
results.add_dilter(:page, 1)
results.filtered   #=> { 'select' => [:id, :name], 'page' => 1 }

Returns:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/muster/results.rb', line 101

def filtered
  return self if self.filters.empty?

  filtered_results = self.filters.inject( {} ) do |results, (key, options)|
    results[key] = self.filter( key, *options )

    results
  end

  return self.class.new(filtered_results)
end