class Pyper::Pipes::Model::AttributeValidation
Attributes
allowed[R]
Array of attributes that are allowed to be set. If empty, all attributes are allowed.
required[R]
Array of attributes that are required to be present (non-nil).
restricted[R]
Hash of attributes whose value must be restricted in some way. Format :attribute => lambda { |value| #Return boolean indicating pass/fail }
Public Class Methods
new(opts={})
click to toggle source
@param opts [Hash] Options defining how attributes should be validated. @option opts [Array<Symbol>] :allowed A list of attributes that are allowed
to be set. If empty, all attributes are assumed to be allowed.
@option opts [Array<Symbol>] :required A list of attributes that are required
to be present (non-nil).
@option opts [Hash] :restricted A Hash of attributes whose value must be
restricted in some way. Format :attribute => lambda { |value| #Return boolean indicating pass/fail }
# File lib/pyper/pipes/model/attribute_validation.rb, line 26 def initialize(opts={}) @allowed = opts[:allowed] if opts[:allowed] @required = opts[:required] if opts[:required] @restricted = opts[:restricted] end
Public Instance Methods
pipe(attributes, status = {})
click to toggle source
@param attributes [Hash] The un-validated attributes @param status [Hash] The mutable status field @return [Hash] The original attributes
# File lib/pyper/pipes/model/attribute_validation.rb, line 35 def pipe(attributes, status = {}) if allowed.present? attributes.keys.each do |attr| raise Failure.new("Attribute #{attr} is not allowed.") unless allowed.include?(attr) end end if required.present? required.each do |attr| raise Failure.new("Missing required attribute #{attr}.") if attributes[attr].nil? end end if restricted.present? restricted.each do |attr, test| raise Failure.new("Invalid value for attribute #{attr}.") unless test.call(attributes[attr]) end end attributes end