Provide attr_required with presence validation
MIT License
= attr_required
This gem provide attr_required and attr_optional like attr_accessor.
REQUIRED and OPTIONAL are common terminology in RFCs, and used for protocol parameters. This gem helps RFC library developers to define which parameters (attributes in Ruby world) are REQUIRED and which are OPTIONAL. It might be also helpful for other developers.
I've developed this gem to use for rack-oauth2, a Rack-based OAuth 2.0 library. https://github.com/nov/rack-oauth2
== Installation
gem install attr_required
== Usage
require 'attr_required' require 'attr_optional'
class A include AttrRequired, AttrOptional attr_required :required_a attr_optional :optional_a end
class B < A attr_required :required_b attr_optional :optional_b end
A.required_attributes #=> [:required_a] B.required_attributes #=> [:required_a, :required_b] A.optional_attributes #=> [:optional_a] B.optional_attributes #=> [:optional_a, :optional_b]
A.attr_required?(:required_a) #=> true B.attr_optional?(:optional_b) #=> true
@a = A.new @b = B.new
@a.required_attributes #=> [:required_a] @b.required_attributes #=> [:required_a, :required_b] @a.optional_attributes #=> [:optional_a] @b.optional_attributes #=> [:optional_a, :optional_b]
@a.attr_required?(:required_a) #=> true @a.attr_optional?(:optiona_a) #=> true
@a.attr_missing? #=> true @a.attr_missing #=> [:required_a] @a.attr_missing! #=> raise AttrRequired::AttrMissing @a.required_a = "foo" @a.attr_missing? #=> false @a.attr_missing #=> [] @a.attr_missing! #=> do nothing
Check spec/attr_(required|optional).rb for more details.
== Note on Patches/Pull Requests
== Copyright
Copyright (c) 2010 nov matake. See LICENSE for details.