Project

methodic

0.0
No commit activity in last 3 years
No release in over 3 years
Methodic : provide Hash table options arguments manager (specifications and validations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 13.0.1
~> 6.2.1
~> 5.0.0
~> 3.9.0
~> 0.9.24
 Project Readme

Methodic

Content

Gem Version

Description

Methodic is a macro-like utility to help test, validate, control options passed by an Hash param to a method, it could help you to merge with defaults values, It raise explained exceptions and return false if the validations steps failed.

Installation

In a valid Ruby environment :

   $ sudo zsh
   # gem ins methodic

Implementation

  • [Methodic]
  • [Methodic::Options]

Examples

Without known options control

  require 'rubygems'                                                                                                                                                                             
  require 'methodic'
  [...]                                                                                                                                                                                          
  # in a method                                                                                                                                                                                  
  def amethod ( _options = {})                                                                                                                                                                   
    myOptions = Methodic::get_options(_options) do |m|                                                                                                                                           
      m.specify_default_value_of :country => 'France'                                                                                                                                            
      m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String                                                                                               
      aCond = Proc::new {|option| case options when 'Doe' then true else false end }                                                                                                                
      m.specify_condition_for :name => aCond
      m.specify_presence_of :name                                                                                                                                                                
      m.specify_presence_of :surname                                                                                                                                                             
      m.specify_formats_of :name => /\w+/, :surname => /\w+/, :country => /\w+/                                                                                                                  
      m.merge!                                                                                                                                                                                   
    end                                                                                                                                                                                          
    # processing method                                                                                                                                                                          
  end                                                                                                                                                                                    
  [...]  

With known options control

  require 'rubygems'                                                                                                                                                                             
  require 'methodic'
  [...]                                                                                                                                                                                          
  # in a method                                                                                                                                                                                  
  def amethod ( _options = {})                                                                                                                                                                   
    myOptions = Methodic::get_options(_options,true) do |m|
      # all others definitions MUST be included in known options list (explained in Spec), so :                                                                                                             m.specify_known_options [:country,:name,:surname, :age]          
      m.specify_default_value_of :country => 'France'
      aCond = Proc::new {|option| case options when 'Doe' then true else false end }                                                                                                                
      m.specify_condition_for :name => aCond                                                                                                                                            
      m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String                                                                                               
      m.specify_presence_of :name                                                                                                                                                                
      m.specify_presence_of :surname                                                                                                                                                             
      m.specify_formats_of :name => /\w+/, :surname => /\w+/, :country => /\w+/                                                                                                                  
      m.merge!                                                                                                                                                                                   
    end                                                                                                                                                                                          
    # processing method                                                                                                                                                                          
  end                                                                                                                                                                                    
  [...]  

Remarque about conditions

  • Condition MUST :
  • be ruby code
  • be a Proc Object
  • have an argument |option| who provide the option symbol, like :an_option
  • return true or false
  • Make your condition like
   aCond = Proc::new do |option| 
           case options
             when .... then ...
             when .... then ...
             else ...
           end
   end

Copyright

Methodic (c) 2012-2013 Romain GEORGES  for Ultragreen Software