Class: Objective::Filters::StringFilter

Inherits:
Objective::Filter show all
Defined in:
lib/objective/filters/string_filter.rb

Instance Attribute Summary

Attributes inherited from Objective::Filter

#key, #sub_filters

Instance Method Summary collapse

Methods inherited from Objective::Filter

#default, #default?, #dup, #feed, #feed_empty, #feed_invalid, #feed_nil, #feed_none, #feed_result, filter_name, #handle_errors, inherited, #initialize, #options, #sub_filters_hash, #type_specific_options_hash

Constructor Details

This class inherits a constructor from Objective::Filter

Instance Method Details

#coercable?(raw) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/objective/filters/string_filter.rb', line 13

def coercable?(raw)
  options.coercable_classes.map { |klass| raw.is_a?(klass) }.any?
end

#coerce(raw) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/objective/filters/string_filter.rb', line 5

def coerce(raw)
  return raw unless raw.is_a?(String) || coercable?(raw)
  tmp = raw.is_a?(BigDecimal) ? raw.to_s(options.decimal_format) : raw.to_s
  tmp = tmp.gsub(/[^[:print:]\t\r\n]+/, ' ') unless options.allow_control_characters
  tmp = tmp.strip if options.strip
  tmp
end

#coerce_error(coerced) ⇒ Object



17
18
19
# File 'lib/objective/filters/string_filter.rb', line 17

def coerce_error(coerced)
  return :string unless coerced.is_a?(String)
end

#validate(coerced) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/objective/filters/string_filter.rb', line 21

def validate(coerced)
  return :empty if coerced.empty?
  return :min if options.min && coerced.length < options.min
  return :max if options.max && coerced.length > options.max
  return :in if options.in && !options.in.include?(coerced)
  return :matches if options.matches && (options.matches !~ coerced)
end