Class: RCite::Option
- Inherits:
-
Object
- Object
- RCite::Option
- Includes:
- Comparable
- Defined in:
- lib/rcite/option.rb
Overview
Represents an option. Option objects provide useful helper methods for
the flexible validation and transformation of option values.
Instance Attribute Summary (collapse)
-
- (true, false) allow_nil
Determines whether
nilvalues are allowed. -
- (Array<Object>) bad_values
List of objects that are invalid values for this option.
-
- (Object?) default
Default value for this option.
-
- (Array<Object>) good_values
List of objects that are valid values for this option.
-
- (Symbol) name
This option's name.
-
- (Object) transformer
An Object indicating which transformation should be applied to values that are added using #set.
-
- (Proc?) validator
A
Procthat is used to determine (together with#good_values,#bad_valuesand#allow_nil) whether. -
- (Object?) values
(also: #get)
This option's current value(s).
Instance Method Summary (collapse)
-
- (-1, ...) <=>(other)
Compares this option with
other. -
- (Option) initialize(name, options = {}, &block)
constructor
Creates a new Option.
-
- (Array<Object>, ...) set(*values)
(also: #values=)
Assigns one or more values to this option.
-
- (Object) transform!(value)
Transforms
valuein-place according to the procedure specified by #transformer. -
- (true, false) validate(value)
Validates a value for this option.
Constructor Details
- (Option) initialize(name, options = {}, &block)
Creates a new Option. All instance variables can be set using the options hash.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rcite/option.rb', line 105 def initialize(name, = {}, &block) raise ArgumentError, 'name cannot be empty' unless name && name != '' @name = name.to_sym @transformer = Proc.new if block_given? @allow_nil = true @good_values, @bad_values = [], [] .each_pair do |k,v| method = (k.to_s+'=').to_sym raise ArgumentError, 'invalid option: #{k}' unless respond_to?(method) send(method, v) end @values ||= @default end |
Instance Attribute Details
- (true, false) allow_nil
Determines whether nil values are allowed.
60 61 62 |
# File 'lib/rcite/option.rb', line 60 def allow_nil @allow_nil end |
- (Array<Object>) bad_values
List of objects that are invalid values for this option.
#good_values takes precedence over this list, so if an item is present on that list as well, it is considered valid.
84 85 86 |
# File 'lib/rcite/option.rb', line 84 def bad_values @bad_values end |
- (Object?) default
Default value for this option. Needs not be valid in the sense of #validate. May be an array of arbitrary objects (including other arrays).
54 55 56 |
# File 'lib/rcite/option.rb', line 54 def default @default end |
- (Array<Object>) good_values
List of objects that are valid values for this option. This list is non-exclusive, so items that are not on it may still be valid according to the #validator.
This list takes precedence over #bad_values, so if an item is listed on both, it is considered valid.
76 77 78 |
# File 'lib/rcite/option.rb', line 76 def good_values @good_values end |
- (Symbol) name
This option's name. Should be descriptive yet short because Style creates helper methods whose names are derived from this attribute.
11 12 13 |
# File 'lib/rcite/option.rb', line 11 def name @name end |
- (Object) transformer
An Object indicating which transformation should be applied to values that are added using #set. Can be one of the following:
- A
Procobject -- for each value transformation, the Proc is called with the value as the only argument. - One of the standard classes
String,Symbol,IntegerandFloat-- this option will attempt to transform each value to an object of the given class. Transforms tonilif the value does not respond to the respective conversion method. trueorfalse-- values are converted totrueorfalse. If a value isfalse,nil,"false"or:false, it is converted tofalse; other values are converted totrue.- Any
Classobjects -- values are replaced with the return value ofclass.new(value). Transforms tonilif the given class does not respond tonew. May throw errors if the class's constructor demands other parameters.
50 51 52 |
# File 'lib/rcite/option.rb', line 50 def transformer @transformer end |
- (Proc?) validator
A Proc that is used to determine (together with
#good_values, #bad_values and #allow_nil) whether
58 59 60 |
# File 'lib/rcite/option.rb', line 58 def validator @validator end |
- (Object?) values Also known as: get
This option's current value(s). If the option has multiple values, they
are represented by an Array (which may include more Arrays for value
tupels and such).
17 18 19 |
# File 'lib/rcite/option.rb', line 17 def values @values end |
Instance Method Details
- (-1, ...) <=>(other)
Compares this option with other. The option's #name is the sole
criterion for comparison. Therefore you are advised to make sure that no
two options share the same name.
254 255 256 |
# File 'lib/rcite/option.rb', line 254 def <=>(other) @name <=> other.name end |
- (Array<Object>, ...) set(*values) Also known as: values=
Assigns one or more values to this option. Performs all validations and transformations prior to assignment. Validation is performed after transformation so you have the possibility of 'rescuing' invalid values by transforming them.
If multiple values are given, they are turned into a flattened array and
compacted before the assignment. The #allow_nil directive is only
considered violated if all elements of values are nil. This is
determined prior to transformation and validation.
If values contains exactly one element after validation and
transformation, #values is assinged a single object rather than an
Array.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rcite/option.rb', line 139 def set(*values) vals = values.flatten.compact if vals.empty? unless @default || @allow_nil raise ArgumentError, "Nil values are not allowed for option #{@name}." end return @values = @default end vals.each do |val| transform!(val) unless validate(val) raise ArgumentError, "Invalid value for option #{@name}: '#{val}'." end end @values = vals.size == 1 ? vals[0] : vals end |
- (Object) transform!(value)
Transforms value in-place according to the procedure specified by
#transformer. See the discussion there for further information.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rcite/option.rb', line 187 def transform!(value) if @transformer.is_a?(Class) return case @transformer.to_s when 'String' then value.respond_to?(:to_s) ? value.to_s : nil when 'Symbol' then value.respond_to?(:to_sym) ? value.to_sym : nil when 'Integer' then value.respond_to?(:to_i) ? value.to_i : nil when 'Float' then value.respond_to?(:to_f) ? value.to_f : nil else @transformer.respond_to?(:new) ? @transformer.new(value) : nil end end case @transformer when true, false case value when true, false then value when nil, "false", :false then false else true end when Proc @transformer.call(value) else value end end |
- (true, false) validate(value)
Validates a value for this option. Tests the following conditions (in the specified order):
- #good_values includes the value; if so
trueis returned and all other validations are skipped. - #bad_values includes the value; if so
falseis returned and all other validations are skipped. - #validator is
nil; if sotrueis returned. - If the above validations don't succeed or fail, the return value of
#validator
.call(value)is returned.
175 176 177 178 179 180 |
# File 'lib/rcite/option.rb', line 175 def validate(value) return true if @good_values.include?(value) return false if @bad_values. include?(value) return true unless @validator return @validator.call(value) end |