Class: Methodic::Options
- Inherits:
-
Hash
- Object
- Hash
- Methodic::Options
- Defined in:
- lib/methodic.rb
Overview
please do not instantiate with Options::new use Methodic::get_options
class Options an Options utility class
Instance Attribute Summary collapse
- #classes ⇒ Object
- #conditions ⇒ Object
- #defaults ⇒ Object
- #formats ⇒ Object
- #known ⇒ Object
- #mandatories ⇒ Object
-
#toto ⇒ Object
Returns the value of attribute toto.
Instance Method Summary collapse
-
#initialize(_options = {}, _validate_known_options = false) {|_self| ... } ⇒ Options
constructor
initializer for [Options].
-
#merge_with_defaults ⇒ self|Options
(also: #merge)
default values merge method merge @defaults with self.
-
#options ⇒ Array
read only accessor on the [Hash] slef keys.
-
#specify_class_of(values) ⇒ hash
(also: #specify_classes_of)
pretty accessor for specifying classes of options.
-
#specify_condition_for(values) ⇒ hash
(also: #specify_conditions_for)
pretty accessor for specifying conditions for options.
-
#specify_default_value(values) ⇒ hash
(also: #specify_defaults_values)
pretty accessor for specifying the default(s) value(s) for options.
-
#specify_format_of(values) ⇒ hash
(also: #specify_formats_of)
pretty accessor for specifying the format of options.
-
#specify_known_option(*values) ⇒ Array
(also: #specify_known_options)
pretty accessor for specifying known options.
-
#specify_presence_of(*values) ⇒ Array
(also: #specify_presences_of)
pretty accessor for specifying mandatories options.
-
#validate ⇒ true|false
(also: #validate!)
Validation method for options, start the validation for classes, options, formats, presences.
Constructor Details
#initialize(_options = {}, _validate_known_options = false) {|_self| ... } ⇒ Options
please do not use standalone, build from module method Methodic::get_options
_options keys must be symbols
initializer for [Options]
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( = {}, = false) raise ArgumentError::new('Argument _options must be a Hash') unless .class == Hash or .class == Methodic::Options or .class == DRb::DRbObject # ;) reintrance and cascading raise ArgumentError::new('keys must be Symbol') unless .keys.select{|i| i.class == Symbol }.size == .keys.size if .class == DRb::DRbObject then self.clear .each do |key,value| self[key] = value end else self.replace end @conditions = Hash::new @defaults = Hash::new @formats = Hash::new @classes = Hash::new @known = List::new @mandatories = List::new @validate_known_options = yield self if block_given? end |
Instance Attribute Details
#classes ⇒ Object
104 105 106 |
# File 'lib/methodic.rb', line 104 def classes @classes end |
#conditions ⇒ Object
145 146 147 |
# File 'lib/methodic.rb', line 145 def conditions @conditions end |
#defaults ⇒ Object
124 125 126 |
# File 'lib/methodic.rb', line 124 def defaults @defaults end |
#formats ⇒ Object
134 135 136 |
# File 'lib/methodic.rb', line 134 def formats @formats end |
#known ⇒ Object
94 95 96 |
# File 'lib/methodic.rb', line 94 def known @known end |
#mandatories ⇒ Object
114 115 116 |
# File 'lib/methodic.rb', line 114 def mandatories @mandatories end |
#toto ⇒ Object
Returns the value of attribute toto.
135 136 137 |
# File 'lib/methodic.rb', line 135 def toto @toto end |
Instance Method Details
#merge_with_defaults ⇒ self|Options Also known as: merge
default values merge method merge @defaults with self
293 294 295 296 |
# File 'lib/methodic.rb', line 293 def merge_with_defaults self.replace( @defaults.merge self) return self end |
#options ⇒ Array
read only accessor on the [Hash] slef keys
187 188 189 |
# File 'lib/methodic.rb', line 187 def return self.keys end |
#specify_class_of(values) ⇒ hash Also known as: specify_classes_of
classes must be precised in Ruby not a string like Fixnum, Hash, Array, String
pretty accessor for specifying classes of options
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
Conditions must be precised in Ruby as a Proc Object returning a boolean
Convention : Proc MUST return true or false ONLY ( false trigged the exception raising )
pretty accessor for specifying conditions for options
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
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
formats must be Regular Expression
pretty accessor for specifying the format of options
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
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
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 |
#validate ⇒ true|false Also known as: validate!
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
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 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 |