class ExplicitParameters::Parameters
Public Class Methods
accepts(name, type = nil, options = {}, &block)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 35 def accepts(name, type = nil, options = {}, &block) if block_given? subtype = define(name, &block) if type == Array type = Array[subtype] elsif type == nil type = subtype else raise ArgumentError, "`type` argument can only be `nil` or `Array` when a block is provided" end end attribute(name, type, options.slice(:default, :required)) validations = options.except(:default) validations[:coercion] = true validates(name, validations) end
define(name = nil, &block)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 27 def define(name = nil, &block) name_class(Class.new(self, &block), name) end
new(attributes = {})
click to toggle source
Calls superclass method
# File lib/explicit_parameters/parameters.rb, line 67 def initialize(attributes = {}) attributes = attributes.to_unsafe_h if IS_RAILS5 && attributes.respond_to?(:to_unsafe_h) @original_attributes = attributes.stringify_keys super(attributes) end
optional_attributes()
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 52 def optional_attributes @optional_attributes ||= [] end
parse!(params)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 23 def parse!(params) new(params).validate! end
requires(name, type = nil, options = {}, &block)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 31 def requires(name, type = nil, options = {}, &block) accepts(name, type, options.merge(required: true), &block) end
Private Class Methods
name_class(klass, name)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 58 def name_class(klass, name) if name.present? name = name.to_s.camelize klass.singleton_class.send(:define_method, :name) { name } end klass end
Public Instance Methods
to_hash()
click to toggle source
Calls superclass method
# File lib/explicit_parameters/parameters.rb, line 94 def to_hash super.except(*missing_attributes) end
validate!()
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 73 def validate! raise InvalidParameters.new({errors: errors}.to_json) unless valid? self end
validate_attribute_coercion!(attribute_name, value)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 86 def validate_attribute_coercion!(attribute_name, value) return unless @original_attributes.key?(attribute_name.to_s) attribute = attribute_set[attribute_name] return if value.nil? && !attribute.required? return if attribute.value_coerced?(value) errors.add attribute_name, "#{@original_attributes[attribute_name].inspect} is not a valid #{attribute.type.name.demodulize}" end
validate_attribute_provided!(attribute_name, value)
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 78 def validate_attribute_provided!(attribute_name, value) if !@original_attributes.key?(attribute_name.to_s) errors.add attribute_name, 'is required' elsif attribute_set[attribute_name].type.primitive == Array && value == [].freeze errors.add attribute_name, 'is required' end end
Private Instance Methods
missing_attributes()
click to toggle source
# File lib/explicit_parameters/parameters.rb, line 103 def missing_attributes @missing_attributes ||= (attribute_set.map(&:name).map(&:to_s) - @original_attributes.keys).map(&:to_sym) end