Sha256: 0f58b63e557af57e06cb008e36c03451bee72f70463f4807f4d20227a75734b1

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true
module UiComponents
  module CellAttributes
    extend ActiveSupport::Concern

    def initialize(*args)
      super
      options.except(:controller).each do |k, v|
        public_send(:"#{k}=", v)
      end
      validate_mandatory_attributes
    end

    def attributes
      self.class.attributes.keys.map do |k|
        [k, public_send(k)]
      end.to_h
    end

    private

    def validate_mandatory_attributes
      missing_attributes = self.class.attributes
        .select { |a| a[:mandatory] && public_send(a[:name]).nil? }
      return if missing_attributes.empty?
      raise MandatoryAttributeNotSet,
        'Following mandatory attribute(s) have not been provided: ' +
        missing_attributes.map { |m| m[:name] }.join(', ')
    end

    class MandatoryAttributeNotSet < StandardError; end

    class_methods do
      def attribute(name, options = {})
        options = ActionController::Parameters.new(options)
        attributes << {
          name: name,
          mandatory: options[:mandatory] == true,
          description: options.require(:description)
        }
        attr_accessor name
      end

      def attributes
        @attributes ||= []
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ad2games-ui_components-2.0.3 lib/ui_components/cell_attributes.rb
ad2games-ui_components-2.0.2 lib/ui_components/cell_attributes.rb
ad2games-ui_components-2.0.1 lib/ui_components/cell_attributes.rb
ad2games-ui_components-2.0.0 lib/ui_components/cell_attributes.rb