Class: Muster::Results
- Inherits:
-
ActiveSupport::HashWithIndifferentAccess
- Object
- ActiveSupport::HashWithIndifferentAccess
- Muster::Results
- 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.
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
Raw data specified during initialization.
-
#filters ⇒ Hash
readonly
Filters specified using #add_filter.
Instance Method Summary collapse
-
#add_filter(key, *options) ⇒ void
Add a filter to be applied to the data in #filtered results.
-
#filter(key, *options) ⇒ void
Filters and returns the raw data values for the specifid key and options.
-
#filtered ⇒ Muster::Results
Returns the raw data with all of the filters applied.
-
#initialize(data, options = {}) ⇒ Results
constructor
Create a new results instance.
Constructor Details
#initialize(data, options = {}) ⇒ Results
Create a new results instance
54 55 56 57 58 59 |
# File 'lib/muster/results.rb', line 54 def initialize( data, ={} ) 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
#data ⇒ Hash (readonly)
Returns raw data specified during initialization.
38 39 40 |
# File 'lib/muster/results.rb', line 38 def data @data end |
#filters ⇒ Hash (readonly)
Returns filters specified using #add_filter.
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.
86 87 88 |
# File 'lib/muster/results.rb', line 86 def add_filter( key, * ) self.filters[key] = 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.
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/muster/results.rb', line 142 def filter( key, * ) if .present? && .first.instance_of?(Hash) = .first.with_indifferent_access if .has_key?(:only) return filter_only_values( key, [:only] ) elsif .has_key?(:except) return filter_excluded_values( key, [:except] ) end else return self.fetch(key, *) end end |
#filtered ⇒ Muster::Results
Returns the raw data with all of the filters applied
If no filters were added, this method simply returns self.
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, )| results[key] = self.filter( key, * ) results end return self.class.new(filtered_results) end |