Module: Decanter::Core::ClassMethods
- Defined in:
- lib/decanter/core.rb
Instance Method Summary collapse
- #any_inputs_required? ⇒ Boolean
- #decant(args) ⇒ Object
- #decanter_for_handler(handler) ⇒ Object
- #empty_args_error ⇒ Object
- #empty_required_input_error ⇒ Object
- #handle(handler, args) ⇒ Object
- #handle_association(handler, args) ⇒ Object
- #handle_empty_args ⇒ Object
- #handle_has_many(handler, values) ⇒ Object
- #handle_has_one(handler, values) ⇒ Object
- #handle_input(handler, args) ⇒ Object
- #handled_keys(args) ⇒ Object
- #handlers ⇒ Object
- #has_many(assoc, **options) ⇒ Object
- #has_one(assoc, **options) ⇒ Object
- #ignore(*args) ⇒ Object
- #input(name, parsers = nil, **options) ⇒ Object
- #keys_to_ignore ⇒ Object
- #parse(key, parsers, values, options) ⇒ Object
- #required_input_keys_present?(args = {}) ⇒ Boolean
- #required_inputs ⇒ Object
- #strict(mode) ⇒ Object
- #strict_mode ⇒ Object
-
#unhandled_keys(args) ⇒ Object
protected.
Instance Method Details
#any_inputs_required? ⇒ Boolean
68 69 70 |
# File 'lib/decanter/core.rb', line 68 def any_inputs_required? required_inputs.any? end |
#decant(args) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/decanter/core.rb', line 56 def decant(args) return handle_empty_args if args.blank? return empty_required_input_error unless required_input_keys_present?(args) args = args.to_unsafe_h.with_indifferent_access if args.class.name == 'ActionController::Parameters' {}.merge( unhandled_keys(args) ) .merge( handled_keys(args) ) end |
#decanter_for_handler(handler) ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/decanter/core.rb', line 187 def decanter_for_handler(handler) if specified_decanter = handler[:options][:decanter] Decanter::decanter_from(specified_decanter) else Decanter::decanter_for(handler[:assoc]) end end |
#empty_args_error ⇒ Object
91 92 93 |
# File 'lib/decanter/core.rb', line 91 def empty_args_error raise(ArgumentError, 'Decanter has required inputs but no values were passed') end |
#empty_required_input_error ⇒ Object
87 88 89 |
# File 'lib/decanter/core.rb', line 87 def empty_required_input_error raise(MissingRequiredInputValue, 'Required inputs have been declared, but no values for those inputs were passed.') end |
#handle(handler, args) ⇒ Object
134 135 136 137 138 |
# File 'lib/decanter/core.rb', line 134 def handle(handler, args) values = args.values_at(*handler[:name]) values = values.length == 1 ? values.first : values self.send("handle_#{handler[:type]}", handler, values) end |
#handle_association(handler, args) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/decanter/core.rb', line 146 def handle_association(handler, args) assoc_handlers = [ handler, handler.merge({ key: handler[:options].fetch(:key, "#{handler[:name]}_attributes").to_sym, name: "#{handler[:name]}_attributes".to_sym }) ] assoc_handler_names = assoc_handlers.map { |_handler| _handler[:name] } case args.values_at(*assoc_handler_names).compact.length when 0 {} when 1 _handler = assoc_handlers.detect { |_handler| args.has_key?(_handler[:name]) } self.send("handle_#{_handler[:type]}", _handler, args[_handler[:name]]) else raise ArgumentError.new("Handler #{handler[:name]} matches multiple keys: #{assoc_handler_names}.") end end |
#handle_empty_args ⇒ Object
64 65 66 |
# File 'lib/decanter/core.rb', line 64 def handle_empty_args any_inputs_required? ? empty_args_error : {} end |
#handle_has_many(handler, values) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/decanter/core.rb', line 168 def handle_has_many(handler, values) decanter = decanter_for_handler(handler) if values.is_a?(Hash) parsed_values = values.map do |index, input_values| next if input_values.nil? decanter.decant(input_values) end return { handler[:key] => parsed_values } else { handler[:key] => values.compact.map { |value| decanter.decant(value) } } end end |
#handle_has_one(handler, values) ⇒ Object
183 184 185 |
# File 'lib/decanter/core.rb', line 183 def handle_has_one(handler, values) { handler[:key] => decanter_for_handler(handler).decant(values) } end |
#handle_input(handler, args) ⇒ Object
140 141 142 143 144 |
# File 'lib/decanter/core.rb', line 140 def handle_input(handler, args) values = args.values_at(*handler[:name]) values = values.length == 1 ? values.first : values parse(handler[:key], handler[:parsers], values, handler[:options]) end |
#handled_keys(args) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/decanter/core.rb', line 120 def handled_keys(args) arg_keys = args.keys.map(&:to_sym) inputs, assocs = handlers.values.partition { |handler| handler[:type] == :input } {}.merge( # Inputs inputs.select { |handler| (arg_keys & handler[:name]).any? } .reduce({}) { |memo, handler| memo.merge handle_input(handler, args) } ).merge( # Associations assocs.reduce({}) { |memo, handler| memo.merge handle_association(handler, args) } ) end |
#handlers ⇒ Object
209 210 211 |
# File 'lib/decanter/core.rb', line 209 def handlers @handlers ||= {} end |
#has_many(assoc, **options) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/decanter/core.rb', line 27 def has_many(assoc, **) handlers[assoc] = { assoc: assoc, key: .fetch(:key, assoc), name: assoc, options: , type: :has_many } end |
#has_one(assoc, **options) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/decanter/core.rb', line 37 def has_one(assoc, **) handlers[assoc] = { assoc: assoc, key: .fetch(:key, assoc), name: assoc, options: , type: :has_one } end |
#ignore(*args) ⇒ Object
47 48 49 |
# File 'lib/decanter/core.rb', line 47 def ignore(*args) keys_to_ignore.push(*args) end |
#input(name, parsers = nil, **options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/decanter/core.rb', line 10 def input(name, parsers=nil, **) _name = [name].flatten if _name.length > 1 && parsers.blank? raise ArgumentError.new("#{self.name} no parser specified for input with multiple values.") end handlers[_name] = { key: .fetch(:key, _name.first), name: _name, options: , parsers: parsers, type: :input } end |
#keys_to_ignore ⇒ Object
213 214 215 |
# File 'lib/decanter/core.rb', line 213 def keys_to_ignore @keys_to_ignore ||= [] end |
#parse(key, parsers, values, options) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/decanter/core.rb', line 195 def parse(key, parsers, values, ) case when !parsers { key => values } when [:required] == true && Array.wrap(values).all? { |value| value.nil? || value == "" } raise ArgumentError.new("No value for required argument: #{key}") else Parser.parsers_for(parsers) .reduce({key => values}) do |vals_hash, parser| vals_hash.keys.reduce({}) { |acc, k| acc.merge(parser.parse(k, vals_hash[k], )) } end end end |
#required_input_keys_present?(args = {}) ⇒ Boolean
79 80 81 82 83 84 85 |
# File 'lib/decanter/core.rb', line 79 def required_input_keys_present?(args={}) return true unless any_inputs_required? compact_inputs = required_inputs.compact compact_inputs.all? do |input| args.keys.map(&:to_sym).include?(input) end end |
#required_inputs ⇒ Object
72 73 74 75 76 77 |
# File 'lib/decanter/core.rb', line 72 def required_inputs handlers.map do |h| = h.last[:options] h.first.first if && [:required] end end |
#strict(mode) ⇒ Object
51 52 53 54 |
# File 'lib/decanter/core.rb', line 51 def strict(mode) raise(ArgumentError, "#{self.name}: Unknown strict value #{mode}") unless [:with_exception, true, false].include? mode @strict_mode = mode end |
#strict_mode ⇒ Object
217 218 219 |
# File 'lib/decanter/core.rb', line 217 def strict_mode @strict_mode.nil? ? Decanter.configuration.strict : @strict_mode end |
#unhandled_keys(args) ⇒ Object
protected
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/decanter/core.rb', line 97 def unhandled_keys(args) unhandled_keys = args.keys.map(&:to_sym) - handlers.keys.flatten.uniq - keys_to_ignore - handlers.values .select { |handler| handler[:type] != :input } .map { |handler| "#{handler[:name]}_attributes".to_sym } if unhandled_keys.any? case strict_mode when true p "#{self.name} ignoring unhandled keys: #{unhandled_keys.join(', ')}." {} when :with_exception raise(UnhandledKeysError, "#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.") else args.select { |key| unhandled_keys.include? key } end else {} end end |