mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
extend filter table to handle soft variable lookup
This commit is contained in:
parent
fb91b788a6
commit
fc718267c4
2 changed files with 63 additions and 10 deletions
|
@ -95,15 +95,36 @@ module FilterTable
|
|||
|
||||
private
|
||||
|
||||
def matches_float(x, y)
|
||||
return false if x.nil?
|
||||
return false if !x.is_a?(Float) && (x =~ /\A[-+]?(\d+\.?\d*|\.\d+)\z/).nil?
|
||||
x.to_f == y
|
||||
end
|
||||
|
||||
def matches_int(x, y)
|
||||
return false if x.nil?
|
||||
return false if !x.is_a?(Integer) && (x =~ /\A[-+]?\d+\z/).nil?
|
||||
x.to_i == y
|
||||
end
|
||||
|
||||
def matches_regex(x, y)
|
||||
return x == y if x.is_a?(Regexp)
|
||||
!x.to_s.match(y).nil?
|
||||
end
|
||||
|
||||
def matches(x, y)
|
||||
x === y # rubocop:disable Style/CaseEquality
|
||||
end
|
||||
|
||||
def filter_lines(table, field, condition)
|
||||
m = method(:matches)
|
||||
m = method(:matches_float) if condition.is_a?(Float)
|
||||
m = method(:matches_int) if condition.is_a?(Integer)
|
||||
m = method(:matches_regex) if condition.is_a?(Regexp)
|
||||
|
||||
table.find_all do |line|
|
||||
next unless line.key?(field)
|
||||
case line[field]
|
||||
when condition
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
m.call(line[field], condition)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -134,7 +155,7 @@ module FilterTable
|
|||
fields.each do |method, field_name|
|
||||
block = blocks[method]
|
||||
define_method method.to_sym do |condition = Show, &cond_block|
|
||||
return block.call(self) unless block.nil?
|
||||
return block.call(self, condition) unless block.nil?
|
||||
return where(nil).get_fields(field_name) if condition == Show && !block_given?
|
||||
where({ field_name => condition }, &cond_block)
|
||||
end
|
||||
|
|
|
@ -7,9 +7,9 @@ require 'helper'
|
|||
|
||||
describe FilterTable do
|
||||
let (:data) {[
|
||||
{ foo: 3, bar: true, baz: 'yay', num: nil },
|
||||
{ foo: 2, bar: false, baz: 'noo', num: 1 },
|
||||
{ foo: 2, bar: false, baz: 'whatever', num: 2 },
|
||||
{ foo: 3, bar: true, baz: 'yay', num: nil, snum: "0" },
|
||||
{ foo: 2, bar: false, baz: 'noo', num: 1, snum: nil },
|
||||
{ foo: 2, bar: false, baz: 'whatever', num: 2, snum: "1.00" },
|
||||
]}
|
||||
|
||||
let (:resource) {
|
||||
|
@ -103,6 +103,38 @@ describe FilterTable do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'with the string-number field' do
|
||||
before { factory.add(:snum).connect(resource, :data) }
|
||||
|
||||
it 'retrieves the number 0' do
|
||||
instance.snum(0).params.must_equal [data[0]]
|
||||
end
|
||||
|
||||
it 'retrieves the number 1' do
|
||||
instance.snum(1).params.must_equal []
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with the string-float field' do
|
||||
before { factory.add(:snum).connect(resource, :data) }
|
||||
|
||||
it 'retrieves the float 0.0' do
|
||||
instance.snum(0.0).params.must_equal [data[0]]
|
||||
end
|
||||
|
||||
it 'retrieves the float 1.0' do
|
||||
instance.snum(1.0).params.must_equal [data[2]]
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with a regex check' do
|
||||
before { factory.add(:baz).connect(resource, :data) }
|
||||
|
||||
it 'retrieves the number 0' do
|
||||
instance.baz(/ever$/).params.must_equal [data[2]]
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with the string field' do
|
||||
before { factory.add(:baz).connect(resource, :data) }
|
||||
|
||||
|
|
Loading…
Reference in a new issue