Sha256: 2ec65f504c778c2fe1f8015813682b1f174274496c99983268cb868174df3b64

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

class Scarpe
  class InvalidAttributeValueError < StandardError; end

  class <%= argument %> < Scarpe::Widget
     display_properties <%= display_properties %>  # Write your display proporties here

    def initialize(left, top)
      @left = convert_to_integer(left, "left")
      @top = convert_to_integer(top, "top")

      super()
      create_display_widget
    end

    private

    def convert_to_integer(value, attribute_name, default = 0)
      begin
        value = Integer(value)
        raise InvalidAttributeValueError, "Negative num '#{value}' not allowed for attribute '#{attribute_name}'" if value < 0

        value
      rescue ArgumentError
        error_message = "Invalid value '#{value}' provided for attribute '#{attribute_name}'. The value should be a number."
        raise InvalidAttributeValueError, error_message
      end
    end

    def convert_to_float(value, attribute_name, default = 0.0)
      begin
        value = Float(value)
        raise InvalidAttributeValueError, "Negative num '#{value}' not allowed for attribute '#{attribute_name}'" if value < 0

        value
      rescue ArgumentError
        error_message = "Invalid value '#{value}' provided for attribute '#{attribute_name}'. The value should be a number."
        raise InvalidAttributeValueError, error_message
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scarpe-0.2.1 templates/class_template_with_shapes.erb
scarpe-0.2.0 templates/class_template_with_shapes.erb