Class: ActiveConfiguration::Option
- Inherits:
-
Object
- Object
- ActiveConfiguration::Option
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/active_configuration/option.rb
Overview
Holds the configuration details of a single option. An instance of this object is created when the #option block is used within a #configure block.
Instance Attribute Summary collapse
-
#allow_multiple ⇒ Object
(also: #allow_multiple?)
Returns the value of attribute allow_multiple.
-
#allowed_format ⇒ Object
Returns the value of attribute allowed_format.
-
#allowed_modifiers ⇒ Object
Returns the value of attribute allowed_modifiers.
-
#allowed_values ⇒ Object
Returns the value of attribute allowed_values.
-
#default_value ⇒ Object
Returns the value of attribute default_value.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
-
#default(value) ⇒ Object
Sets the default value for this option.
-
#format(value) ⇒ Object
Sets a specific format that the value of this option must conform to.
-
#initialize(key) ⇒ Option
constructor
Initializes the default values for all deatils of this options.
-
#modifiers(*values) ⇒ Object
Restricts the allows modifiers of this option to a given list of modifers.
-
#multiple(value) ⇒ Object
Whether or not this option can have multiple settings set against it.
-
#restrict(*values) ⇒ Object
Restricts the allowed values of this option to a given list of values.
-
#validate! ⇒ Object
Validates how the specified configuration options are used with one another.
Constructor Details
#initialize(key) ⇒ Option
Initializes the default values for all deatils of this options. This includes no default value, no restricted values set, no modifiers, and a ‘string’ format.
28 29 30 31 32 33 34 35 |
# File 'lib/active_configuration/option.rb', line 28 def initialize(key) @key = key @default_value = nil @allowed_format = 'string' @allowed_values = nil @allowed_modifiers = nil @allow_multiple = false end |
Instance Attribute Details
#allow_multiple ⇒ Object Also known as: allow_multiple?
Returns the value of attribute allow_multiple.
19 20 21 |
# File 'lib/active_configuration/option.rb', line 19 def allow_multiple @allow_multiple end |
#allowed_format ⇒ Object
Returns the value of attribute allowed_format.
19 20 21 |
# File 'lib/active_configuration/option.rb', line 19 def allowed_format @allowed_format end |
#allowed_modifiers ⇒ Object
Returns the value of attribute allowed_modifiers.
19 20 21 |
# File 'lib/active_configuration/option.rb', line 19 def allowed_modifiers @allowed_modifiers end |
#allowed_values ⇒ Object
Returns the value of attribute allowed_values.
19 20 21 |
# File 'lib/active_configuration/option.rb', line 19 def allowed_values @allowed_values end |
#default_value ⇒ Object
Returns the value of attribute default_value.
19 20 21 |
# File 'lib/active_configuration/option.rb', line 19 def default_value @default_value end |
#key ⇒ Object
Returns the value of attribute key.
19 20 21 |
# File 'lib/active_configuration/option.rb', line 19 def key @key end |
Instance Method Details
#default(value) ⇒ Object
Sets the default value for this option. This cannot be used in conjunction with the multiple options. Additionally, if a set of allowed values is set with the #restrict method, this default value must appear in that list of allowed values.
43 44 45 46 47 |
# File 'lib/active_configuration/option.rb', line 43 def default(value) run_callbacks :validate do @default_value = (value.is_a?(Symbol) ? value.to_s : value) end end |
#format(value) ⇒ Object
Sets a specific format that the value of this option must conform to. Allowed formats include: ‘string’, ‘fixnum’, ‘float’, ‘boolean’, ‘email’, ‘url’ or a /regular expression/.
54 55 56 57 58 |
# File 'lib/active_configuration/option.rb', line 54 def format(value) run_callbacks :validate do @allowed_format = (value.is_a?(Symbol) ? value.to_s : value) end end |
#modifiers(*values) ⇒ Object
Restricts the allows modifiers of this option to a given list of modifers.
Example:
modifiers 'eq', 'lt', 'gt', 'lte', 'gte'
80 81 82 83 84 |
# File 'lib/active_configuration/option.rb', line 80 def modifiers(*values) run_callbacks :validate do @allowed_modifiers = values.collect{|value| (value.is_a?(Symbol) ? value.to_s : value)} end end |
#multiple(value) ⇒ Object
Whether or not this option can have multiple settings set against it.
90 91 92 93 94 |
# File 'lib/active_configuration/option.rb', line 90 def multiple(value) run_callbacks :validate do @allow_multiple = value end end |
#restrict(*values) ⇒ Object
Restricts the allowed values of this option to a given list of values.
Example:
restrict 'alphabetical', 'manual'
67 68 69 70 71 |
# File 'lib/active_configuration/option.rb', line 67 def restrict(*values) run_callbacks :validate do @allowed_values = values.collect{|value| (value.is_a?(Symbol) ? value.to_s : value)} end end |
#validate! ⇒ Object
Validates how the specified configuration options are used with one another. If an invalid configuration is detected, such as using both the #default method and setting #multiple to true, an exception of ActiveConfiguration::Error is raised.
Note: This method is automatically called after each of the configuration methods are run.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_configuration/option.rb', line 103 def validate! # If both a default value and a list of allowed values are given, # the default value must appear in the list of allowed values. if !@default_value.nil? and !@allowed_values.nil? and !@allowed_values.include?(@default_value) raise ActiveConfiguration::Error, "The default value '#{@default_value}' isn't present in the list of allowed values." end # If multiple is set, it must be set to either true or false. if ![TrueClass, FalseClass].include?(@allow_multiple.class) raise ActiveConfiguration::Error, 'The multiple option requires a boolean.' end # If a default value is given, multiple must be false. if !@default_value.nil? and @allow_multiple raise ActiveConfiguration::Error, 'The default value cannot be set in combination with the multiple option.' end # If a format is specified, it must be an allowed format. This # includes 'string', 'fixnum', 'float', 'boolean', 'email', 'url' # or a /regular exprssion. if !@allowed_format.nil? and !['string', 'fixnum', 'float', 'boolean', 'email', 'url'].include?(@allowed_format) and !@allowed_format.is_a?(Regexp) raise ActiveConfiguration::Error, "The format #{@allowed_format} is not supported." end end |