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
52 53 54 55 56 57 |
# File 'lib/muster/results.rb', line 52 def initialize(data, = {}) # rubocop:disable Lint/UnusedMethodArgument 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)
rubocop:disable Lint/NestedMethodDefinition, Metrics/MethodLength
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/muster/results.rb', line 155 def method_missing(meth, *args, &block) if key?(meth) value = self[meth] if value.is_a?(Hash) value.instance_eval do def method_missing(meth, *args, &block) if key?(meth) return fetch(meth) end super end end end return value end # rubocop:enable super end |
Instance Attribute Details
#data ⇒ Hash (readonly)
Returns raw data specified during initialization.
36 37 38 |
# File 'lib/muster/results.rb', line 36 def data @data end |
#filters ⇒ Hash (readonly)
Returns filters specified using #add_filter.
40 41 42 |
# File 'lib/muster/results.rb', line 40 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.
84 85 86 |
# File 'lib/muster/results.rb', line 84 def add_filter(key, *) 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.
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/muster/results.rb', line 138 def filter(key, *) if .present? && .first.instance_of?(Hash) = .first.with_indifferent_access if .key?(:only) return filter_only_values(key, [:only]) elsif .key?(:except) return filter_excluded_values(key, [:except]) end else return 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.
99 100 101 102 103 104 105 106 107 |
# File 'lib/muster/results.rb', line 99 def filtered return self if filters.empty? filtered_results = filters.each_with_object({}) do |(key, ), results| results[key] = filter(key, *) end self.class.new(filtered_results) end |