Module: Decanter::Core::ClassMethods
- Defined in:
- lib/decanter/core.rb
Instance Method Summary collapse
- #decant(args) ⇒ Object
- #decanter_for_handler(handler) ⇒ Object
- #handle(handler, args) ⇒ Object
- #handle_association(handler, 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
- #input(name, parsers = nil, **options) ⇒ Object
- #parse(key, parsers, values, options) ⇒ Object
- #strict(mode) ⇒ Object
- #strict_mode ⇒ Object
-
#unhandled_keys(args) ⇒ Object
protected.
Instance Method Details
#decant(args) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/decanter/core.rb', line 52 def decant(args) return {} unless args.present? 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
150 151 152 153 154 155 156 |
# File 'lib/decanter/core.rb', line 150 def decanter_for_handler(handler) if specified_decanter = handler[:options][:decanter] Decanter::decanter_from(specified_decanter) else Decanter::decanter_for(handler[:assoc]) end end |
#handle(handler, args) ⇒ Object
97 98 99 100 101 |
# File 'lib/decanter/core.rb', line 97 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/decanter/core.rb', line 109 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_has_many(handler, values) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/decanter/core.rb', line 131 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
146 147 148 |
# File 'lib/decanter/core.rb', line 146 def handle_has_one(handler, values) { handler[:key] => decanter_for_handler(handler).decant(values) } end |
#handle_input(handler, args) ⇒ Object
103 104 105 106 107 |
# File 'lib/decanter/core.rb', line 103 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
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/decanter/core.rb', line 83 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
172 173 174 |
# File 'lib/decanter/core.rb', line 172 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 |
#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 |
#parse(key, parsers, values, options) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/decanter/core.rb', line 158 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 |
#strict(mode) ⇒ Object
47 48 49 50 |
# File 'lib/decanter/core.rb', line 47 def strict(mode) raise( ArgumentError.new("#{self.name}: Unknown strict value #{mode}")) unless [:with_exception, true, false].include? mode @strict_mode = mode end |
#strict_mode ⇒ Object
176 177 178 |
# File 'lib/decanter/core.rb', line 176 def strict_mode @strict_mode.nil? ? Decanter.configuration.strict : @strict_mode end |
#unhandled_keys(args) ⇒ Object
protected
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/decanter/core.rb', line 61 def unhandled_keys(args) unhandled_keys = args.keys.map(&:to_sym) - handlers.keys.flatten.uniq - 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 ArgumentError.new("#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.") else args.select { |key| unhandled_keys.include? key } end else {} end end |