Class: Mutations::ArrayFilter

Inherits:
InputFilter show all
Defined in:
lib/mutations/array_filter.rb

Instance Attribute Summary

Attributes inherited from InputFilter

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InputFilter

#default, default_options, #discard_empty?, #discard_invalid?, #discard_nils?, #has_default?

Constructor Details

#initialize(name, opts = {}, &block) ⇒ ArrayFilter

Returns a new instance of ArrayFilter.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mutations/array_filter.rb', line 16

def initialize(name, opts = {}, &block)
  super(opts)

  @name = name
  @element_filter = nil

  if block_given?
    instance_eval &block
  end

  raise ArgumentError.new("Can't supply both a class and a filter") if @element_filter && self.options[:class]
end

Class Method Details

.register_additional_filter(type_class, type_name) ⇒ Object



3
4
5
6
7
8
# File 'lib/mutations/array_filter.rb', line 3

def self.register_additional_filter(type_class, type_name)
  define_method(type_name) do | *args |
    options = args[0] || {}
    @element_filter = type_class.new(options)
  end
end

Instance Method Details

#array(options = {}, &block) ⇒ Object



37
38
39
# File 'lib/mutations/array_filter.rb', line 37

def array(options = {}, &block)
  @element_filter = ArrayFilter.new(nil, options, &block)
end

#filter(data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mutations/array_filter.rb', line 41

def filter(data)
  # Handle nil case
  if data.nil?
    return [nil, nil] if options[:nils]
    return [nil, :nils]
  end

  if !data.is_a?(Array) && options[:arrayize]
    return [[], nil] if data == ""
    data = Array(data)
  end

  if data.is_a?(Array)
    errors = ErrorArray.new
    filtered_data = []
    found_error = false
    data.each_with_index do |el, i|
      el_filtered, el_error = filter_element(el)
      el_error = ErrorAtom.new(@name, el_error, :index => i) if el_error.is_a?(Symbol)
      errors << el_error
      if el_error
        found_error = true
      else
        filtered_data << el_filtered
      end
    end

    if found_error && !(@element_filter && @element_filter.discard_invalid?)
      [data, errors]
    else
      [filtered_data, nil]
    end
  else
    return [data, :array]
  end
end

#filter_element(data) ⇒ Object

Returns [filtered, errors]



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mutations/array_filter.rb', line 79

def filter_element(data)
  if @element_filter
    data, el_errors = @element_filter.filter(data)
    return [data, el_errors] if el_errors
  elsif options[:class]
    class_const = options[:class]
    class_const = class_const.constantize if class_const.is_a?(String)

    if !data.is_a?(class_const)
      return [data, :class]
    end
  end

  [data, nil]
end

#hash(options = {}, &block) ⇒ Object



29
30
31
# File 'lib/mutations/array_filter.rb', line 29

def hash(options = {}, &block)
  @element_filter = HashFilter.new(options, &block)
end

#model(name, options = {}) ⇒ Object



33
34
35
# File 'lib/mutations/array_filter.rb', line 33

def model(name, options = {})
  @element_filter = ModelFilter.new(name.to_sym, options)
end