Class: Methodic::Options

Inherits:
Hash
  • Object
show all
Defined in:
lib/methodic.rb

Overview

Note:

please do not instantiate with Options::new use Methodic::get_options

class Options an Options utility class

Examples:

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
[...]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}, _validate_known_options = false) {|_self| ... } ⇒ Options

Note:

please do not use standalone, build from module method Methodic::get_options

Note:

_options keys must be symbols

initializer for [Options]

Examples:

prototype for informational only

Methodic::Options::new({:toto => 'titi', :tutu => 'tata'})

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/methodic.rb', line 154

def initialize(_options = {},_validate_known_options = false)

  raise ArgumentError::new('Argument _options must be a Hash') unless _options.class == Hash or _options.class == Methodic::Options or _options.class == DRb::DRbObject
  # ;) reintrance and cascading
  raise ArgumentError::new('keys must be Symbol') unless _options.keys.select{|i| i.class == Symbol }.size ==  _options.keys.size
  if _options.class == DRb::DRbObject then
    self.clear
    _options.each  do |key,value|
      self[key] = value
    end
  else
    self.replace _options
  end

  @conditions = Hash::new
  @defaults = Hash::new
  @formats = Hash::new
  @classes = Hash::new
  @known = List::new
  @mandatories = List::new
  @validate_known_options = _validate_known_options
  yield self if block_given?
end

Instance Attribute Details

#classesObject

Examples:

writing

myOptions = Methodic::get_options(options)
myOptions.classes = {:name => String }
myOptions.classes[:surname] = String

reading

p myOptions.classes
=> { :name => String, :surname => String }


104
105
106
# File 'lib/methodic.rb', line 104

def classes
  @classes
end

#conditionsObject

Examples:

writing

myOptions = Methodic::get_options(options)
myOptions.conditions = {:name => aProcObject }
myOptions.conditions[:surname] = aProcObject

reading

p myOptions.defaults
=> { :name => /\w+/, :surname => /\w+/ }


145
146
147
# File 'lib/methodic.rb', line 145

def conditions
  @conditions
end

#defaultsObject

Examples:

writing

myOptions = Methodic::get_options(options)
myOptions.defaults = {:name => 'John' }
myOptions.defaults[:surname] = 'Doe'

reading

p myOptions.defaults
=> { :name => 'John', :surname => 'Doe' }


124
125
126
# File 'lib/methodic.rb', line 124

def defaults
  @defaults
end

#formatsObject

Examples:

writing

myOptions = Methodic::get_options(options)
myOptions.formats = {:name => /\w+/ }
myOptions.formats[:surname] = /\w+/

reading

p myOptions.defaults
=> { :name => /\w+/, :surname => /\w+/ }


134
135
136
# File 'lib/methodic.rb', line 134

def formats
  @formats
end

#knownObject

Examples:

writing

myOptions = Methodic::get_options(options)
myOptions.known = [:name]
myOptions.known.push :surname

reading

p myOptions.known
=> [ :name, :surname ]


94
95
96
# File 'lib/methodic.rb', line 94

def known
  @known
end

#mandatoriesObject

Examples:

writing

myOptions = Methodic::get_options(options)
myOptions.mandatories = [:name]
myOptions.mandatories.push :surname

reading

p myOptions.mandatories
=> [ :name, :surname ]


114
115
116
# File 'lib/methodic.rb', line 114

def mandatories
  @mandatories
end

#totoObject

Returns the value of attribute toto.



135
136
137
# File 'lib/methodic.rb', line 135

def toto
  @toto
end

Instance Method Details

#merge_with_defaultsself|Options Also known as: merge

default values merge method merge @defaults with self

Examples:

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"}


293
294
295
296
# File 'lib/methodic.rb', line 293

def merge_with_defaults
  self.replace( @defaults.merge self)
  return self
end

#optionsArray

read only accessor on the [Hash] slef keys

Examples:

usage

options = {:name => 'Doe', :surname => 'John'}
p myOptions = Methodic::get_options(options)
=> { :name => String, :surname => String }
p myOptions.options
=> [:name, :surname]


187
188
189
# File 'lib/methodic.rb', line 187

def options
  return self.keys
end

#specify_class_of(values) ⇒ hash Also known as: specify_classes_of

Note:

classes must be precised in Ruby not a string like Fixnum, Hash, Array, String

pretty accessor for specifying classes of options

Examples:

usage

myOptions = Methodic::get_options(_options)
myOptions.specify_class_of :name => String
myOptions.specify_classes_of :name => String, :surname => String


213
214
215
216
# File 'lib/methodic.rb', line 213

def specify_class_of(values)
  @classes.merge! values
  return @classes
end

#specify_condition_for(values) ⇒ hash Also known as: specify_conditions_for

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 )

pretty accessor for specifying conditions for options

Examples:

usage

myOptions = Methodic::get_options(_options)
myOptions.specify_condition_for :name => aProcObject
myOptions.specify_conditions_for :name => aProcObject, :surname => aProcObject


228
229
230
231
# File 'lib/methodic.rb', line 228

def specify_condition_for(values)
  @conditions.merge! values
  return @conditions
end

#specify_default_value(values) ⇒ hash Also known as: specify_defaults_values

pretty accessor for specifying the default(s) value(s) for options

Examples:

usage

myOptions = Methodic::get_options(_options)
myOptions.specify_default_value :name => 'Doe'
myOptions.specify_defaults_values :name => 'Doe', :surname => 'John'


199
200
201
202
# File 'lib/methodic.rb', line 199

def specify_default_value(values)
   @defaults.merge! values
   return @defaults
end

#specify_format_of(values) ⇒ hash Also known as: specify_formats_of

Note:

formats must be Regular Expression

pretty accessor for specifying the format of options

Examples:

usage

myOptions = Methodic::get_options(_options)
myOptions.specify_format_of :name => /\w+/
myOptions.specify_formats_of :name => /\w+/, :surname => /\w+/


275
276
277
278
# File 'lib/methodic.rb', line 275

def specify_format_of(values)
  @formats.merge! values
  return @formats
end

#specify_known_option(*values) ⇒ Array Also known as: specify_known_options

pretty accessor for specifying known options

Examples:

usage

myOptions = Methodic::get_options(_options)
myOptions.specify_known_option :name
myOptions.specify_known_options [ :name, :surname ]


259
260
261
262
263
264
# File 'lib/methodic.rb', line 259

def specify_known_option(*values)
  @known << values
  @known.flatten!
  @known.uniq!
  return @known
end

#specify_presence_of(*values) ⇒ Array Also known as: specify_presences_of

pretty accessor for specifying mandatories options

Examples:

usage

myOptions = Methodic::get_options(_options)
myOptions.specify_presence_of :name
myOptions.specify_presences_of [ :name, :surname ]


243
244
245
246
247
248
# File 'lib/methodic.rb', line 243

def specify_presence_of(*values)
  @mandatories << values
  @mandatories.flatten!
  @mandatories.uniq!
  return @mandatories
end

#validatetrue|false Also known as: validate!

Note:

order for validation and Exception raising : Options inclusion >> Classes matching >> Mandatories Options presences >> Formats matching

Validation method for options, start the validation for classes, options, formats, presences

Raises:

  • ArgumentError for each kind of validations, that could failed



302
303
304
305
306
307
308
309
310
311
# File 'lib/methodic.rb', line 302

def validate
  table = []
  raise ArgumentError::new("Option : known list of options empty.") and return false if @known.empty? and @validate_known_options
  table.push validate_known_options if @validate_known_options
  table.push validate_classes unless @classes.empty?
  table.push validate_presences unless @mandatories.empty?
  table.push validate_formats unless @formats.empty?
  table.push validate_conditions unless @conditions.empty?
  return true unless table.include?(false)
end