Skip to content

Commit

Permalink
F #2505: Add nested filters (#3083)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiojvg authored and Tino Vázquez committed Mar 19, 2019
1 parent 9941ac2 commit 0258711
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/cli/cli_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ module CLIHelper
:description => "Filter data. An array is specified with\n"<<
" "*31<<"column=value pairs."
}
OPERATOR = {
:name => "operator",
:large => "--operator operator",
:format => String,
:description => "Logical operator used on filters: AND, OR."<<
" Default: AND."
}

#
#HEADER = {
# :name => "header",
Expand Down Expand Up @@ -82,7 +90,7 @@ module CLIHelper
}

#OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY]
OPTIONS = [LIST, LISTCONF, DELAY, FILTER, CSV_OPT, NO_PAGER]
OPTIONS = [LIST, LISTCONF, DELAY, FILTER, OPERATOR, CSV_OPT, NO_PAGER]

# Sets bold font
def CLIHelper.scr_bold
Expand Down Expand Up @@ -330,7 +338,7 @@ def data_array(data, options)
}
if options
filter_data!(res_data, options[:filter]) if options[:filter]
filter_data!(res_data, options) if options[:filter]
sort_data!(res_data, options[:order]) if options[:order]
end
Expand Down Expand Up @@ -393,10 +401,16 @@ def header_str
}.compact.join(' ')
end
def filter_data!(data, filter)
def filter_data!(data, options)
# TBD: add more operators
# operators=/(==|=|!=|<|<=|>|>=)/
operators=/(=|!=)/
filter = options[:filter]
if options.key?(:operator)
log_operator = options[:operator].upcase
else
log_operator = "AND"
end
stems=filter.map do |s|
m=s.match(/^(.*?)#{operators}(.*?)$/)
Expand Down Expand Up @@ -427,12 +441,19 @@ def filter_data!(data, filter)
stems.each do |s|
if d[s[:index]].public_send(s[:operator] == "=" ? "==" : s[:operator], s[:right])
pass=false
break
if log_operator == "OR"
pass = true
break
end
else
pass = false
if log_operator == "AND"
break
end
end
end
!pass
pass
end
end
Expand Down

0 comments on commit 0258711

Please sign in to comment.