Class: Mutations::HashFilter
- Inherits:
-
InputFilter
- Object
- InputFilter
- Mutations::HashFilter
- Defined in:
- lib/mutations/hash_filter.rb
Instance Attribute Summary collapse
-
#optional_inputs ⇒ Object
Returns the value of attribute optional_inputs.
-
#required_inputs ⇒ Object
Returns the value of attribute required_inputs.
Attributes inherited from InputFilter
Class Method Summary collapse
Instance Method Summary collapse
- #array(name, options = {}, &block) ⇒ Object
- #dup ⇒ Object
- #filter(data) ⇒ Object
- #hash(name, options = {}, &block) ⇒ Object
-
#initialize(opts = {}, &block) ⇒ HashFilter
constructor
A new instance of HashFilter.
- #model(name, options = {}) ⇒ Object
- #optional(&block) ⇒ Object
- #optional_keys ⇒ Object
- #required(&block) ⇒ Object
- #required_keys ⇒ Object
Methods inherited from InputFilter
#default, default_options, #discard_empty?, #discard_invalid?, #discard_nils?, #has_default?
Constructor Details
#initialize(opts = {}, &block) ⇒ HashFilter
Returns a new instance of HashFilter.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mutations/hash_filter.rb', line 19 def initialize(opts = {}, &block) super(opts) @optional_inputs = {} @required_inputs = {} @current_inputs = @required_inputs if block_given? instance_eval &block end end |
Instance Attribute Details
#optional_inputs ⇒ Object
Returns the value of attribute optional_inputs.
16 17 18 |
# File 'lib/mutations/hash_filter.rb', line 16 def optional_inputs @optional_inputs end |
#required_inputs ⇒ Object
Returns the value of attribute required_inputs.
17 18 19 |
# File 'lib/mutations/hash_filter.rb', line 17 def required_inputs @required_inputs end |
Class Method Details
.register_additional_filter(type_class, type_name) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/mutations/hash_filter.rb', line 3 def self.register_additional_filter(type_class, type_name) define_method(type_name) do | *args, &block | name = args[0] = args[1] || {} @current_inputs[name.to_sym] = type_class.new(, &block) end end |
Instance Method Details
#array(name, options = {}, &block) ⇒ Object
68 69 70 71 |
# File 'lib/mutations/hash_filter.rb', line 68 def array(name, = {}, &block) name_sym = name.to_sym @current_inputs[name.to_sym] = ArrayFilter.new(name_sym, , &block) end |
#dup ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mutations/hash_filter.rb', line 31 def dup dupped = HashFilter.new @optional_inputs.each_pair do |k, v| dupped.optional_inputs[k] = v end @required_inputs.each_pair do |k, v| dupped.required_inputs[k] = v end dupped end |
#filter(data) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mutations/hash_filter.rb', line 73 def filter(data) # Handle nil case if data.nil? return [nil, nil] if [:nils] return [nil, :nils] end # Ensure it's a hash return [data, :hash] unless data.is_a?(Hash) # We always want a hash with indiffernet access unless data.is_a?(HashWithIndifferentAccess) data = data.with_indifferent_access end errors = ErrorHash.new filtered_data = HashWithIndifferentAccess.new wildcard_filterer = nil [[@required_inputs, true], [@optional_inputs, false]].each do |(inputs, is_required)| inputs.each_pair do |key, filterer| # If we are doing wildcards, then record so and move on if key == :* wildcard_filterer = filterer next end data_element = data[key] if data.has_key?(key) sub_data, sub_error = filterer.filter(data_element) if sub_error.nil? filtered_data[key] = sub_data elsif !is_required && filterer.discard_invalid? data.delete(key) elsif !is_required && sub_error == :empty && filterer.discard_empty? data.delete(key) elsif !is_required && sub_error == :nils && filterer.discard_nils? data.delete(key) else sub_error = ErrorAtom.new(key, sub_error) if sub_error.is_a?(Symbol) errors[key] = sub_error end end if !data.has_key?(key) if filterer.has_default? filtered_data[key] = filterer.default elsif is_required errors[key] = ErrorAtom.new(key, :required) end end end end if wildcard_filterer filtered_keys = data.keys - filtered_data.keys filtered_keys.each do |key| data_element = data[key] sub_data, sub_error = wildcard_filterer.filter(data_element) if sub_error.nil? filtered_data[key] = sub_data elsif wildcard_filterer.discard_invalid? data.delete(key) elsif sub_error == :empty && wildcard_filterer.discard_empty? data.delete(key) elsif sub_error == :nils && wildcard_filterer.discard_nils? data.delete(key) else sub_error = ErrorAtom.new(key, sub_error) if sub_error.is_a?(Symbol) errors[key] = sub_error end end end if errors.any? [data, errors] else [filtered_data, nil] # We win, it's valid! end end |
#hash(name, options = {}, &block) ⇒ Object
60 61 62 |
# File 'lib/mutations/hash_filter.rb', line 60 def hash(name, = {}, &block) @current_inputs[name.to_sym] = HashFilter.new(, &block) end |
#model(name, options = {}) ⇒ Object
64 65 66 |
# File 'lib/mutations/hash_filter.rb', line 64 def model(name, = {}) @current_inputs[name.to_sym] = ModelFilter.new(name.to_sym, ) end |
#optional(&block) ⇒ Object
47 48 49 50 |
# File 'lib/mutations/hash_filter.rb', line 47 def optional(&block) @current_inputs = @optional_inputs instance_eval &block end |
#optional_keys ⇒ Object
56 57 58 |
# File 'lib/mutations/hash_filter.rb', line 56 def optional_keys @optional_inputs.keys end |
#required(&block) ⇒ Object
42 43 44 45 |
# File 'lib/mutations/hash_filter.rb', line 42 def required(&block) @current_inputs = @required_inputs instance_eval &block end |
#required_keys ⇒ Object
52 53 54 |
# File 'lib/mutations/hash_filter.rb', line 52 def required_keys @required_inputs.keys end |