Method: FilterTable::Table#where

Defined in:
lib/utils/filter.rb

#where(conditions = {}, &block) ⇒ Object

Filter the raw data based on criteria (as method params) or by evaling a block; then construct a new Table of the same class as ourselves, wrapping the filtered data, and return it.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/utils/filter.rb', line 98

def where(conditions = {}, &block)
  return self if !conditions.is_a?(Hash)
  return self if conditions.empty? && !block_given?

  # Initialize the details of the new Table.
  new_criteria_string = criteria_string
  filtered_raw_data = raw_data

  # If we were provided params, interpret them as criteria to be evaluated
  # against the raw data. Criteria are assumed to be hash keys.
  conditions.each do |raw_field_name, desired_value|
    raise(ArgumentError, "'#{decorate_symbols(raw_field_name)}' is not a recognized criterion - expected one of #{decorate_symbols(list_fields).join(', ')}'") unless field?(raw_field_name)
    populate_lazy_field(raw_field_name, desired_value) if is_field_lazy?(raw_field_name)
    new_criteria_string += " #{raw_field_name} == #{desired_value.inspect}"
    filtered_raw_data = filter_raw_data(filtered_raw_data, raw_field_name, desired_value)
  end

  # If we were given a block, make a special Struct for each row, that has an accessor
  # for each field declared using `register_custom_property`, then instance-eval the block
  # against the struct.
  if block_given?
    # Perform the filtering.
    filtered_raw_data = filtered_raw_data.find_all { |row_as_hash| create_eval_context_for_row(row_as_hash, '').instance_eval(&block) }
    # Try to interpret the block for updating the stringification.
    src = Trace.new
    # Swallow any exceptions raised here.
    # See https://github.com/chef/inspec/issues/2929
    begin
      src.instance_eval(&block)
    rescue # rubocop: disable Lint/HandleExceptions
      # Yes, robocop, ignoring all exceptions is normally
      # a bad idea.  Here, an exception just means we don't
      # understand what was in a `where` block, so we can't
      # meaningfully sytringify it.  We still have a decent
      # default stringification.
    end
    new_criteria_string += Trace.to_ruby(src)
  end

  self.class.new(resource, filtered_raw_data, new_criteria_string)
end