Sha256: c6654d9988daa277ea4e829473f16879c7d046ea20c50335aee2322a807b68fb

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module I18n::Tasks
  module Command
    module Options
      module EnumOpt
        delegate :enum_opt, to: :class

        DEFAULT_ENUM_OPT_ERROR = proc { |bad, good|
          I18n.t('i18n_tasks.cmd.enum_opt.invalid_one', invalid: bad, valid: good * ', ')
        }

        def parse_enum_opt(value, valid, &error_msg)
          valid = enum_opt(valid) if Symbol === valid
          return valid.first unless value.present?
          if enum_opt_valid?(valid, value)
            value
          else
            error_msg ||= DEFAULT_ENUM_OPT_ERROR
            raise CommandError.new error_msg.call(value, valid)
          end
        end

        def enum_opt_valid?(valid, value)
          valid = enum_opt(valid) if Symbol === valid
          valid.include?(value)
        end

        DEFAULT_ENUM_LIST_ERROR = proc { |bad, good|
          I18n.t('i18n_tasks.cmd.enum_opt.invalid_list', invalid: bad * ', ', valid: good * ', ')
        }

        def parse_enum_list_opt(values, valid, &error_msg)
          values  = explode_list_opt(values)
          invalid = values - valid.map(&:to_s)
          if invalid.empty?
            values
          else
            error_msg ||= DEFAULT_ENUM_LIST_ERROR
            raise CommandError.new error_msg.call(invalid, valid)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-tasks-0.7.0 lib/i18n/tasks/command/options/enum_opt.rb