module Methodic class Options < Hash attr_accessor :classes attr_accessor :mandatories attr_accessor :defaults attr_accessor :formats def initialize(_options = {}) self.replace _options @defaults = Hash::new @formats = Hash::new @classes = Hash::new @mandatories = Array::new end def options return @classes.keys end def specify_default_value_of(values) @defaults.merge! values end alias :specify_defaults_values_of :specify_default_value_of def specify_class_of(values) @classes.merge! values end alias :specify_classes_of :specify_class_of def specify_presence_of(values) @mandatories << values @mandatories.flatten! end alias :specify_presences_of :specify_presence_of def specify_format_of(values) @formats.merge! values end alias :specify_formats_of :specify_format_of def specify_condition_for raise NotYetImplemented end alias :specify_conditions_for :specify_condition_for def merge_with_defaults self.replace(@defaults.merge self) end def validate table = [] table.push validate_options_and_classes unless @classes.empty? table.push validate_formats unless @formats.empty? table.push validate_presences unless @mandatories.empty? return true unless table.include?(false) end alias :validate! :validate private def validate_formats self.each do |option,value| if @formats.key? option then raise ArgumentError::new("Option : #{option} don't match /#{@formats[option]}/") and return false unless value =~ @formats[option] end end return true end def validate_presences @mandatories.each do |mdt| raise ArgumentError::new("Missing option : #{mdt}") and return false unless self.include?(mdt) end return true end def validate_options_and_classes self.each do |option,value| raise ArgumentError::new("Unknown option : #{option}") and return fasle unless @classes.include?(option) raise ArgumentError::new("Option : #{option} type mismatch must be a #{@classes[option]}") and return false unless value.class == @classes[option] end return true end end def Methodic::get_options(_options = {}) return Methodic::Options::new(_options) end end