Class: Configit::AttributeDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/configit/attribute_definition.rb

Overview

The definition of an attribute in a config.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, desc, options = {}) ⇒ AttributeDefinition

See Configit::Base.attribute

Raises:



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, options={})
  # Clone them so we can delete from the hash
  options = options.clone

  raise ArgumentError, "Name must be a symbol" if not Symbol === name

  @name = name
  @desc = desc
  @required = options.delete(:required) || false
  @type = options.delete(:type) || :string
  @default = options.delete(:default)

  if options.any?
    raise ArgumentError, "Invalid options #{options.keys.join(',')}"
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/configit/attribute_definition.rb', line 6

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



5
6
7
# File 'lib/configit/attribute_definition.rb', line 5

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/configit/attribute_definition.rb', line 4

def name
  @name
end

#typeObject (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

Returns:

  • (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