class Methodic::Options
class Options
an Options
utility class @note please do not instantiate with Options::new
use Methodic::get_options
@example Complete usage
require 'rubygems' require 'methodic' [...] # in a method def amethod ( _options = {}) myOptions = Methodic::get_options(_options,true) do |m| m.specify_known_options [:country,:name,:surname,:age] m.specify_default_value :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 [...]
Attributes
@example writing
myOptions = Methodic::get_options(options) myOptions.classes = {:name => String } myOptions.classes[:surname] = String
@example reading
p myOptions.classes => { :name => String, :surname => String }
@attr [Hash] classes a hash table of some options associated with their corresponding types or classes
@attr [Hash] conditions a hash table of some conditions with their corresponding @example writing
myOptions = Methodic::get_options(options) myOptions.conditions = {:name => aProcObject } myOptions.conditions[:surname] = aProcObject
@example reading
p myOptions.defaults => { :name => /\w+/, :surname => /\w+/ }
@attr [Hash] defaults a hash table of some options with defaults values @example writing
myOptions = Methodic::get_options(options) myOptions.defaults = {:name => 'John' } myOptions.defaults[:surname] = 'Doe'
@example reading
p myOptions.defaults => { :name => 'John', :surname => 'Doe' }
@attr [Hash] formats a hash table of some options with their corresponding @example writing
myOptions = Methodic::get_options(options) myOptions.formats = {:name => /\w+/ } myOptions.formats[:surname] = /\w+/
@example reading
p myOptions.defaults => { :name => /\w+/, :surname => /\w+/ }
@example writing
myOptions = Methodic::get_options(options) myOptions.known = [:name] myOptions.known.push :surname
@example reading
p myOptions.known => [ :name, :surname ]
@attr [Hash] the list of all options assumed for a method
@attr [Array] mandatories the full list of mandatories options @example writing
myOptions = Methodic::get_options(options) myOptions.mandatories = [:name] myOptions.mandatories.push :surname
@example reading
p myOptions.mandatories => [ :name, :surname ]
Public Class Methods
initializer for [Options] @note please do not use standalone, build from module method Methodic::get_options
@param [Hash] _options the options hash (define for the method you would prototype) @note _options keys must be symbols @example prototype for informational only
Methodic::Options::new({:toto => 'titi', :tutu => 'tata'})
@return [Options] self
# File lib/methodic.rb 154 def initialize(_options = {},_validate_known_options = false) 155 156 raise ArgumentError::new('Argument _options must be a Hash') unless _options.class == Hash or _options.class == Methodic::Options or _options.class == DRb::DRbObject 157 # ;) reintrance and cascading 158 raise ArgumentError::new('keys must be Symbol') unless _options.keys.select{|i| i.class == Symbol }.size == _options.keys.size 159 if _options.class == DRb::DRbObject then 160 self.clear 161 _options.each do |key,value| 162 self[key] = value 163 end 164 else 165 self.replace _options 166 end 167 168 @conditions = Hash::new 169 @defaults = Hash::new 170 @formats = Hash::new 171 @classes = Hash::new 172 @known = List::new 173 @mandatories = List::new 174 @validate_known_options = _validate_known_options 175 yield self if block_given? 176 end
Public Instance Methods
default values merge method merge @defaults with self @return [self|Options] @example usage
myOptions = Methodic::get_options(:name = 'Walker') myOptions.specify_default_value_of :surname => 'John' p myOptions # =>{:surname=>"John", :name=>"Doe"} myOptions.merge p myOptions # =>{:surname=>"John", :name=>"Walker"}
# File lib/methodic.rb 293 def merge_with_defaults 294 self.replace( @defaults.merge self) 295 return self 296 end
read only accessor on the [Hash] slef keys @return [Array] self.keys the keys of The Options
object itself @example usage
options = {:name => 'Doe', :surname => 'John'} p myOptions = Methodic::get_options(options) => { :name => String, :surname => String } p myOptions.options => [:name, :surname]
# File lib/methodic.rb 187 def options 188 return self.keys 189 end
pretty accessor for specifying classes of options @param [Hash] values a value definition, keys are symbols @return [hash] @classes merged with values @note classes must be precised in Ruby not a string like Fixnum, Hash, Array, String @example usage
myOptions = Methodic::get_options(_options) myOptions.specify_class_of :name => String myOptions.specify_classes_of :name => String, :surname => String
# File lib/methodic.rb 213 def specify_class_of(values) 214 @classes.merge! values 215 return @classes 216 end
pretty accessor for specifying conditions for options @param [Hash] values a Conditions Proc definition, keys are symbols @return [hash] @conditions merged with values @note Conditions must be precised in Ruby as a Proc Object returning a boolean @note Convention : Proc MUST return true or false ONLY ( false trigged the exception raising ) @example usage
myOptions = Methodic::get_options(_options) myOptions.specify_condition_for :name => aProcObject myOptions.specify_conditions_for :name => aProcObject, :surname => aProcObject
# File lib/methodic.rb 228 def specify_condition_for(values) 229 @conditions.merge! values 230 return @conditions 231 end
pretty accessor for specifying the default(s) value(s) for options @param [Hash] values a value definition, keys are symbols @return [hash] @defaults merged with values @example usage
myOptions = Methodic::get_options(_options) myOptions.specify_default_value :name => 'Doe' myOptions.specify_defaults_values :name => 'Doe', :surname => 'John'
# File lib/methodic.rb 199 def specify_default_value(values) 200 @defaults.merge! values 201 return @defaults 202 end
pretty accessor for specifying the format of options @param [Hash] values a value definition, keys are symbols @return [hash] @formats merged with values @note formats must be Regular Expression @example usage
myOptions = Methodic::get_options(_options) myOptions.specify_format_of :name => /\w+/ myOptions.specify_formats_of :name => /\w+/, :surname => /\w+/
# File lib/methodic.rb 275 def specify_format_of(values) 276 @formats.merge! values 277 return @formats 278 end
pretty accessor for specifying known options @param [Array] values a Array of symbols or a unique symbol @return [Array] @known merged with values @example usage
myOptions = Methodic::get_options(_options) myOptions.specify_known_option :name myOptions.specify_known_options [ :name, :surname ]
# File lib/methodic.rb 259 def specify_known_option(*values) 260 @known << values 261 @known.flatten! 262 @known.uniq! 263 return @known 264 end
pretty accessor for specifying mandatories options @param [Array] values a Array of symbols or a unique symbol @return [Array] @mandatories merged with values @example usage
myOptions = Methodic::get_options(_options) myOptions.specify_presence_of :name myOptions.specify_presences_of [ :name, :surname ]
# File lib/methodic.rb 243 def specify_presence_of(*values) 244 @mandatories << values 245 @mandatories.flatten! 246 @mandatories.uniq! 247 return @mandatories 248 end
Validation method for options, start the validation for classes, options, formats, presences @return [true|false] if validations failed or succeed @raise ArgumentError for each kind of validations, that could failed @note order for validation and Exception raising : Options
inclusion >> Classes matching >> Mandatories Options
presences >> Formats matching
# File lib/methodic.rb 302 def validate 303 table = [] 304 raise ArgumentError::new("Option : known list of options empty.") and return false if @known.empty? and @validate_known_options 305 table.push validate_known_options if @validate_known_options 306 table.push validate_classes unless @classes.empty? 307 table.push validate_presences unless @mandatories.empty? 308 table.push validate_formats unless @formats.empty? 309 table.push validate_conditions unless @conditions.empty? 310 return true unless table.include?(false) 311 end
Private Instance Methods
private method for classes validation step
# File lib/methodic.rb 362 def validate_classes 363 @classes.each do |option,value| 364 365 raise ArgumentError::new("Option : #{option} type mismatch must be a #{value}") and return false unless self[option].class == value 366 end 367 return true 368 end
private method for conditions validation step
# File lib/methodic.rb 353 def validate_conditions 354 @conditions.each do |option,cond| 355 raise ArgumentError::new("Option : #{option} condition failed") and return false unless cond.call(self[option]) == true 356 end 357 return true 358 end
private method for the formats validation step
# File lib/methodic.rb 317 def validate_formats 318 self.each do |option,value| 319 if @formats.key? option then 320 raise ArgumentError::new("Option : #{option} don't match /#{@formats[option]}/") and return false unless value =~ @formats[option] 321 end 322 end 323 324 return true 325 end
private method for known options validation step
# File lib/methodic.rb 328 def validate_known_options 329 @mandatories.each do |mdt| 330 raise ArgumentError::new("Mandatories options :#{mdt} is not a known options") and return false unless @known.include?(mdt) 331 end 332 @classes.each do |aclass,value| 333 raise ArgumentError::new("Classes definition :#{aclass} => #{value} is not a known options") and return false unless @known.include?(aclass) 334 end 335 @formats.each do |format,value| 336 raise ArgumentError::new("Formats definition :#{format} => #{value} is not a known options") and return false unless @known.include?(format) 337 end 338 @defaults.each do |default,value| 339 raise ArgumentError::new("Defaults value definition :#{default} => #{value} is not a known options") and return false unless @known.include?(default) 340 end 341 return true 342 end
private method for mandatories presence validation step
# File lib/methodic.rb 345 def validate_presences 346 @mandatories.each do |mdt| 347 raise ArgumentError::new("Missing option : #{mdt}") and return false unless self.include?(mdt) 348 end 349 return true 350 end