Sha256: 94aea8f2f6ecd1c503e403aeda9ef360c22de809cf43a12c5e7d8867898f3312

Contents?: true

Size: 1.41 KB

Versions: 12

Compression:

Stored size: 1.41 KB

Contents

module I18n::Tasks
  module Command
    module Options
      module EnumOpt
        DEFAULT_ENUM_OPT_ERROR = proc { |bad, good|
          I18n.t('i18n_tasks.cmd.enum_opt.invalid', 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_list_opt.invalid', 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?
            if values.empty?
              valid
            else
              values
            end
          else
            error_msg ||= DEFAULT_ENUM_LIST_ERROR
            raise CommandError.new error_msg.call(invalid, valid)
          end
        end

        def enum_opt(*args)
          self.class.enum_opt(*args)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
i18n-tasks-0.7.13 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.12 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.11 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.10 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.9 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.8 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.7 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.6 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.5 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.4 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.3 lib/i18n/tasks/command/options/enum_opt.rb
i18n-tasks-0.7.2 lib/i18n/tasks/command/options/enum_opt.rb