Sha256: b0413a5eb844cc0b48694c7a8f41ce58ae430b766fb13d1381dd6b088e620f87

Contents?: true

Size: 1.04 KB

Versions: 64

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/patches/class/common_constructor'
require 'eac_ruby_utils/inflector'

module EacRubyUtils
  module Listable
    class Item
      common_constructor :list, :value, :key, :translation_required, default: [true]

      def to_s
        "I: #{list.item}, V: #{value}, K: #{key}"
      end

      def constant_name
        ::EacRubyUtils::Inflector.variableize("#{list.item}_#{key}").upcase
      end

      def label
        translate('label')
      end

      def description
        translate('description')
      end

      # @return [Array] A two-item array in format `[label, value]`.
      def to_option
        [label, value]
      end

      def translation_required?
        translation_required
      end

      private

      def translate(translate_key)
        full_translate_key = "#{list.i18n_key}.#{key}.#{translate_key}"
        if !::I18n.exists?(full_translate_key) && !translation_required?
          ''
        else
          ::I18n.t(full_translate_key)
        end
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 2 rubygems

Version Path
eac_tools-0.52.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/item.rb
eac_tools-0.51.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/item.rb
eac_tools-0.50.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/item.rb
eac_ruby_utils-0.110.1 lib/eac_ruby_utils/listable/item.rb