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
- #default_keys ⇒ Object
- #default_value_inputs ⇒ 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
Adjusting has_many to explicitly define keyword arguments.
-
#has_one(assoc, **options) ⇒ Object
Adjusting has_one similarly.
- #ignore(*args) ⇒ Object
- #input(name, parsers = nil, **options) ⇒ Object
- #keys_to_ignore ⇒ Object
- #log_unhandled_keys(mode) ⇒ Object
- #log_unhandled_keys_mode ⇒ Object
- #parse(key, parsers, value, 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
99 100 101 |
# File 'lib/decanter/core.rb', line 99 def any_inputs_required? required_inputs.any? end |
#decant(args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/decanter/core.rb', line 69 def decant(args) return handle_empty_args if args.blank? return empty_required_input_error unless required_input_keys_present?(args) # Convert all params passed to a decanter to a hash with indifferent access to mitigate accessor ambiguity accessible_args = to_indifferent_hash(args) {}.merge(default_keys) .merge(unhandled_keys(accessible_args)) .merge(handled_keys(accessible_args)) end |
#decanter_for_handler(handler) ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/decanter/core.rb', line 218 def decanter_for_handler(handler) if specified_decanter = handler[:options][:decanter] Decanter.decanter_from(specified_decanter) else Decanter.decanter_for(handler[:assoc]) end end |
#default_keys ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/decanter/core.rb', line 80 def default_keys # return keys with provided default value when key is not defined within incoming args default_result = default_value_inputs .map { |input| [input[:key], input[:options][DEFAULT_VALUE_KEY]] } .to_h # parse handled default values, including keys # with defaults not already managed by handled_keys default_result.merge(handled_keys(default_result)) end |
#default_value_inputs ⇒ Object
91 92 93 |
# File 'lib/decanter/core.rb', line 91 def default_value_inputs handlers.values.select { |input| input[:options].key?(DEFAULT_VALUE_KEY) } end |
#empty_args_error ⇒ Object
124 125 126 |
# File 'lib/decanter/core.rb', line 124 def empty_args_error raise(ArgumentError, 'Decanter has required inputs but no values were passed') end |
#empty_required_input_error ⇒ Object
119 120 121 122 |
# File 'lib/decanter/core.rb', line 119 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
164 165 166 167 168 |
# File 'lib/decanter/core.rb', line 164 def handle(handler, args) values = args.values_at(*handler[:name]) values = values.length == 1 ? values.first : values send("handle_#{handler[:type]}", handler, values) end |
#handle_association(handler, args) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/decanter/core.rb', line 176 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]) } send("handle_#{_handler[:type]}", _handler, args[_handler[:name]]) else raise ArgumentError, "Handler #{handler[:name]} matches multiple keys: #{assoc_handler_names}." end end |
#handle_empty_args ⇒ Object
95 96 97 |
# File 'lib/decanter/core.rb', line 95 def handle_empty_args any_inputs_required? ? empty_args_error : {} end |
#handle_has_many(handler, values) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/decanter/core.rb', line 198 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 { handler[:key] => parsed_values } else { handler[:key] => values.compact.map { |value| decanter.decant(value) } } end end |
#handle_has_one(handler, values) ⇒ Object
214 215 216 |
# File 'lib/decanter/core.rb', line 214 def handle_has_one(handler, values) { handler[:key] => decanter_for_handler(handler).decant(values) } end |
#handle_input(handler, args) ⇒ Object
170 171 172 173 174 |
# File 'lib/decanter/core.rb', line 170 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
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/decanter/core.rb', line 151 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
234 235 236 |
# File 'lib/decanter/core.rb', line 234 def handlers @handlers ||= {} end |
#has_many(assoc, **options) ⇒ Object
Adjusting has_many to explicitly define keyword arguments
31 32 33 34 35 36 37 38 39 |
# File 'lib/decanter/core.rb', line 31 def has_many(assoc, **) handlers[assoc] = { assoc:, key: .fetch(:key, assoc), name: assoc, options:, type: :has_many } end |
#has_one(assoc, **options) ⇒ Object
Adjusting has_one similarly
42 43 44 45 46 47 48 49 50 |
# File 'lib/decanter/core.rb', line 42 def has_one(assoc, **) handlers[assoc] = { assoc:, key: .fetch(:key, assoc), name: assoc, options:, type: :has_one } end |
#ignore(*args) ⇒ Object
52 53 54 |
# File 'lib/decanter/core.rb', line 52 def ignore(*args) keys_to_ignore.push(*args).map!(&:to_sym) end |
#input(name, parsers = nil, **options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/decanter/core.rb', line 13 def input(name, parsers = nil, **) # Convert all input names to symbols to correctly calculate handled vs. unhandled keys input_names = [name].flatten.map(&:to_sym) if input_names.length > 1 && parsers.blank? raise ArgumentError, "#{self.name} no parser specified for input with multiple values." end handlers[input_names] = { key: .fetch(:key, input_names.first), name: input_names, options:, parsers:, type: :input } end |
#keys_to_ignore ⇒ Object
238 239 240 |
# File 'lib/decanter/core.rb', line 238 def keys_to_ignore @keys_to_ignore ||= [] end |
#log_unhandled_keys(mode) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/decanter/core.rb', line 62 def log_unhandled_keys(mode) raise(ArgumentError, "#{name}: Unknown log_unhandled_keys value #{mode}") unless [true, false].include? mode @log_unhandled_keys_mode = mode end |
#log_unhandled_keys_mode ⇒ Object
246 247 248 249 250 |
# File 'lib/decanter/core.rb', line 246 def log_unhandled_keys_mode return !!Decanter.configuration.log_unhandled_keys if @log_unhandled_keys_mode.nil? !!@log_unhandled_keys_mode end |
#parse(key, parsers, value, options) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/decanter/core.rb', line 226 def parse(key, parsers, value, ) return { key => value } unless parsers raise ArgumentError, "No value for required argument: #{key}" if [:required] && value_missing?(value) parser_classes = Parser.parsers_for(parsers) Parser.compose_parsers(parser_classes).parse(key, value, ) end |
#required_input_keys_present?(args = {}) ⇒ Boolean
110 111 112 113 114 115 116 117 |
# File 'lib/decanter/core.rb', line 110 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) && !args[input].nil? end end |
#required_inputs ⇒ Object
103 104 105 106 107 108 |
# File 'lib/decanter/core.rb', line 103 def required_inputs handlers.map do |h| = h.last[:options] h.first.first if && [:required] end end |
#strict(mode) ⇒ Object
56 57 58 59 60 |
# File 'lib/decanter/core.rb', line 56 def strict(mode) raise(ArgumentError, "#{name}: Unknown strict value #{mode}") unless [:ignore, true, false].include? mode @strict_mode = mode end |
#strict_mode ⇒ Object
242 243 244 |
# File 'lib/decanter/core.rb', line 242 def strict_mode @strict_mode.nil? ? Decanter.configuration.strict : @strict_mode end |
#unhandled_keys(args) ⇒ Object
protected
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/decanter/core.rb', line 130 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 } return {} unless unhandled_keys.any? case strict_mode when :ignore p "#{name} ignoring unhandled keys: #{unhandled_keys.join(', ')}." if log_unhandled_keys_mode {} when true raise(UnhandledKeysError, "#{name} received unhandled keys: #{unhandled_keys.join(', ')}.") else args.select { |key| unhandled_keys.include? key.to_sym } end end |