Sha256: 5060d08acf5f4a8ff4915600a9fb208e6288856c7e52ae9142fec752a6e00f30

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module Scrivito

#
# This class represents an attribute definition.
#
# @api public
#
class AttributeDefinition
  #
  # @!attribute [r] name
  #   @api public
  #   @return [String] the name of the attribute.
  #
  # @!attribute [r] type
  #   @api public
  #   @return [String] the type of the attribute.
  #
  attr_reader :name, :type

  def initialize(name, type, options = {})
    @name, @type, @options = name.to_s, type.to_s, options.with_indifferent_access
    assert_valid_options
  end

  #
  # Allowed values for an attribute.
  #
  # @api public
  # @return [Array<String>] allowed values if +type+ is +enum+ or +multienum+ or an empty array
  #   otherwise. If no values have been specified, an empty array is returned.
  #
  def values
    if type == 'enum' || type == 'multienum'
      @options[:values] || []
    else
      []
    end
  end

  def widgetlist?
    type == 'widgetlist'
  end

  private

  def assert_valid_options
    @options.assert_valid_keys(:values, 'values')
    if values = @options[:values]
      values.each(&method(:assert_valid_value))
    end
  end

  def assert_valid_value(value)
    unless value.is_a?(String)
      raise ScrivitoError,
        %{#{value.class} is not allowed as value for #{type} attribute "#{name}".}
    end

    if value == ''
      raise ScrivitoError, %{Empty string is not allowed as value for #{type} attribute "#{name}".}
    end
  end
end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scrivito_sdk-1.1.1 lib/scrivito/attribute_definition.rb
scrivito_sdk-1.1.0 lib/scrivito/attribute_definition.rb
scrivito_sdk-1.1.0.rc3 lib/scrivito/attribute_definition.rb
scrivito_sdk-1.1.0.rc2 lib/scrivito/attribute_definition.rb
scrivito_sdk-1.1.0.rc1 lib/scrivito/attribute_definition.rb