module ActiveJson::Filter
Constants
- VALID_OPERATORS
Public Instance Methods
new(args)
click to toggle source
# File lib/active_json/filter.rb, line 7 def new(args) attributes = process_args(args) values = attributes.map(&parse_values) filter_lambda(values) end
Private Instance Methods
chain()
click to toggle source
# File lib/active_json/filter.rb, line 73 def chain -> (value) { value['.'] } end
filter_lambda(filter)
click to toggle source
# File lib/active_json/filter.rb, line 15 def filter_lambda(filter) -> (data) do extracted_data, *operation = filter.clone.map! do |attribute| attribute.is_a?(Array) ? reduce_data(attribute, data) : attribute end extracted_data&.send(*operation) unless operation.include?(nil) end end
float()
click to toggle source
# File lib/active_json/filter.rb, line 61 def float -> (value) { value.to_f.to_s == value } end
integer()
click to toggle source
# File lib/active_json/filter.rb, line 65 def integer -> (value) { value.to_i.to_s == value } end
operator()
click to toggle source
# File lib/active_json/filter.rb, line 69 def operator -> (value) { VALID_OPERATORS.include?(value) } end
parse_chain(value)
click to toggle source
# File lib/active_json/filter.rb, line 77 def parse_chain(value) value.gsub('.', ' [] ') .split .prepend(:[]) .map(&:to_sym) .each_slice(2) .to_a end
parse_values()
click to toggle source
# File lib/active_json/filter.rb, line 44 def parse_values -> (value) do case value when string then value[1..-2] when float then value.to_f when integer then value.to_i when operator then value.to_sym when chain then parse_chain(value) else [[:[], value.to_sym]] end end end
process_args(args)
click to toggle source
——— HELPERS ———
# File lib/active_json/filter.rb, line 26 def process_args(args) string_quote = args['"'] || args["'"] if string_quote split_with_strings(args, string_quote) else args.split end end
reduce_data(pairs, data)
click to toggle source
# File lib/active_json/filter.rb, line 86 def reduce_data(pairs, data) pairs.reduce(data) { |d, pair| d.send(*pair) } end
split_with_strings(args, quote)
click to toggle source
# File lib/active_json/filter.rb, line 35 def split_with_strings(args, quote) string_start = args.index(quote) string_end = args.rindex(quote) string = args[string_start..string_end] args.split.each.with_index(-1).with_object([]) do |(arg, i), obj| string[arg] && string[obj[i]] ? obj[i] << " #{arg}" : obj << arg end end
string()
click to toggle source
# File lib/active_json/filter.rb, line 57 def string -> (value) { %w[' "].any? { |q| value.start_with?(q) && value.end_with?(q) } } end