Sha256: 9a748a1e074ac25906491ad765c8b78594d1bb732aaa2a96ba46508a8d68e226

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

module Configit
  # The definition of an attribute in a config.
  class AttributeDefinition
    attr_reader :name
    attr_reader :desc
    attr_reader :default
    attr_reader :type
    
    # See Configit::Base.attribute
    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

    def required?
      @required
    end
  
    # Returns an error string if the value is not valid per this AttributeDefinition
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
configit-0.1.3 lib/configit/attribute_definition.rb
configit-0.1.2 lib/configit/attribute_definition.rb