Class: Configit::AttributeDefinition
- Inherits:
-
Object
- Object
- Configit::AttributeDefinition
- Defined in:
- lib/configit/attribute_definition.rb
Overview
The definition of an attribute in a config.
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(name, desc, options = {}) ⇒ AttributeDefinition
constructor
See Configit::Base.attribute.
- #required? ⇒ Boolean
-
#validate(value) ⇒ Object
Returns an error string if the value is not valid per this AttributeDefinition.
Constructor Details
#initialize(name, desc, options = {}) ⇒ AttributeDefinition
See Configit::Base.attribute
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/configit/attribute_definition.rb', line 10 def initialize(name, desc, ={}) # Clone them so we can delete from the hash = .clone raise ArgumentError, "Name must be a symbol" if not Symbol === name @name = name @desc = desc @required = .delete(:required) || false @type = .delete(:type) || :string @default = .delete(:default) if .any? raise ArgumentError, "Invalid options #{.keys.join(',')}" end end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
6 7 8 |
# File 'lib/configit/attribute_definition.rb', line 6 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
5 6 7 |
# File 'lib/configit/attribute_definition.rb', line 5 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/configit/attribute_definition.rb', line 4 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/configit/attribute_definition.rb', line 7 def type @type end |
Instance Method Details
#required? ⇒ Boolean
27 28 29 |
# File 'lib/configit/attribute_definition.rb', line 27 def required? @required end |
#validate(value) ⇒ Object
Returns an error string if the value is not valid per this AttributeDefinition
32 33 34 35 36 37 38 |
# File 'lib/configit/attribute_definition.rb', line 32 def validate(value) if required? && (value == nil || value == "") && (default == nil || default == "") return "#{name} is a required attribute" end # TODO: add type validation here nil end |