Module: ClassX::Validate
- Defined in:
- lib/classx/validate.rb
Overview
for validatation Hash parameter declaretively.
require 'classx/validate'
class YourClass
def run params
validated_prams = Class::Validate.validate params do
has :x
has :y, :default => "hoge", :kind_of => Hash
end
# do something with params
end
end
YourClass.new.run(:x => 10) # raise ClassX::AttrRequiredError
Class Method Summary collapse
Class Method Details
.validate(hash, options = {}, &block) ⇒ Object
:doc:
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/classx/validate.rb', line 23 def validate hash, ={}, &block #:doc: # FIXME: it's experimental feature for caching validate class. # it can use class variable because it use caller[0] as key. if [:cache_key] != false [:cache_key] = caller[0] end if ( [:cache_key] ) @@__validate_cached ||= {} klass = @@__validate_cached[[:cache_key]] else klass = nil end unless klass klass = Class.new klass.class_eval do include ::ClassX include ::ClassX::Bracketable end klass.class_eval(&block) if [:cache_key] @@__validate_cached[[:cache_key]] = klass end end klass.new hash end |