module StrongArms
Constants
- VERSION
Public Instance Methods
extract_handler_values_and_parse(handler, args)
click to toggle source
# File lib/strong_arms.rb, line 91 def extract_handler_values_and_parse(handler, args) name = handler[:name] options = handler[:options] type = handler[:type] allow_nil = options[:allow_nil] value_at_name = args[name] if required_input_value_missing?(options, value_at_name) raise missing_value_for_required_input_exception(name) end return {} if value_is_absent?(value_at_name, allow_nil: allow_nil) send("parse_#{type}", name: name, value: value_at_name, options: options) end
flex(args)
click to toggle source
# File lib/strong_arms.rb, line 53 def flex(args) useful_args = action_controller_args?(args) ? accessible_hash(args) : args exposed_args = expose_data_key_if_present(useful_args) raise empty_arguments_exception if exposed_args.blank? if unhandled_keys_present?(exposed_args) raise unhandled_keys_exception(exposed_args) end reduce_handlers(handlers_values, exposed_args) end
handlers()
click to toggle source
# File lib/strong_arms.rb, line 65 def handlers @handlers ||= {} end
ignore(*args)
click to toggle source
# File lib/strong_arms.rb, line 19 def ignore(*args) @keys_to_ignore = args end
keys_to_ignore()
click to toggle source
# File lib/strong_arms.rb, line 69 def keys_to_ignore @keys_to_ignore ||= [] end
many_nested(association, options = {})
click to toggle source
# File lib/strong_arms.rb, line 43 def many_nested(association, options = {}) format = options.fetch(:format, true) model = options[:model] merged_handler_options = { has_many: true }.merge(model: model) handler_options = model ? merged_handler_options : { has_many: true } modified_key = format ? nested_attributes_key(association) : association set_handler(modified_key, handler_options, type: :association) end
nested_attributes_key(association)
click to toggle source
# File lib/strong_arms.rb, line 73 def nested_attributes_key(association) "#{association}_attributes".to_sym end
one_nested(association, options = {})
click to toggle source
# File lib/strong_arms.rb, line 33 def one_nested(association, options = {}) format = options.fetch(:format, true) model = options[:model] merged_handler_options = { has_many: false }.merge(model: model) handler_options = model ? merged_handler_options : { has_many: false } modified_key = format ? nested_attributes_key(association) : association set_handler(modified_key, handler_options, type: :association) end
parse_association(name:, value:, options:)
click to toggle source
# File lib/strong_arms.rb, line 111 def parse_association(name:, value:, options:) strong_arm = find_strong_arm(name, options) wrapped_values = [value].flatten has_many = options[:has_many] strained_values = wrapped_values.map do |wrapped_value| strong_arm.flex(wrapped_value) end if has_many { name => strained_values } else { name => strained_values.pop } end end
parse_input(name:, value:, options:)
click to toggle source
# File lib/strong_arms.rb, line 107 def parse_input(name:, value:, options:) { name => value } end
permit(attribute, **options)
click to toggle source
# File lib/strong_arms.rb, line 23 def permit(attribute, **options) attributes = [attribute].flatten if multiple_attributes?(attributes) raise multiple_attributes_exception end set_handler(attribute, options, type: :input) end
reduce_handlers(handlers, args)
click to toggle source
# File lib/strong_arms.rb, line 82 def reduce_handlers(handlers, args) return {} if handlers.empty? handlers.reduce({}) do |new_hash, handler| parsed_handler = extract_handler_values_and_parse(handler, args) new_hash.merge(parsed_handler) end end
set_handler(name, options, type:)
click to toggle source
# File lib/strong_arms.rb, line 77 def set_handler(name, options, type:) handlers[name] = build_handler(name, options, type: type) end