class Translatomatic::Option

Stores details about command line and object constructor options

Constants

CONSTRUCTOR_OPTIONS
VALID_TYPES

Attributes

command_line_only[R]

@return [boolean] True if this option can only be set on the command line

default[R]

@return [Object] The default value for this option

description[R]

@return [String] Description of the option

env_name[R]

@return [String] If set, the name of the environment variable

that can be used to set this option in the environment.
hidden[R]

@return [boolean] If true, the option does not appear on the command line

but it can be used in configuration settings
name[R]

@return [String] Name of the option

required[R]

@return [boolean] True if this option is required

type[R]

@return [Symbol] Type of option, one of:

:string, :hash, :array, :numeric, or :boolean
user_location_only[R]

@return [boolean] True if this option can only be set in the

user configuration file

Public Class Methods

new(attributes = {}) click to toggle source

Create a new option @param attributes [Hash<Symbol,Object>] Attributes as above @return [Translatomatic::Option] A new option instance

# File lib/translatomatic/option.rb, line 38
def initialize(attributes = {})
  attributes.each do |k, v|
    raise "unrecognised attribute #{k}" unless constructor_option?(k)
    instance_variable_set("@#{k}", v)
  end
  @description = @desc
  @type ||= :string
  raise "invalid type: #{@type}" unless VALID_TYPES.include?(@type)
  @env_name ||= @use_env && @name ? @name.to_s.upcase : nil
  @user_location_only = true if @name.to_s =~ /api_key/
end
options_from_object(object) click to toggle source

Retrieve all options from an object or list of objects. @param object [#options,Array<#options>] Options source @return [Array<Translatomatic::Option>] options

# File lib/translatomatic/option.rb, line 73
def self.options_from_object(object)
  if object.is_a?(Translatomatic::Option)
    [object]
  elsif object.respond_to?(:options)
    options_from_object(object.options)
  elsif object.is_a?(Array)
    object.collect { |i| options_from_object(i) }.flatten
  else
    []
  end
end

Public Instance Methods

to_thor() click to toggle source

Return sconfiguration for a thor command line option @return [Hash] Thor configuration

# File lib/translatomatic/option.rb, line 52
def to_thor
  {
    required: @required,
    type: thor_type,
    desc: @description,
    default: @default,
    aliases: @aliases,
    banner: @type == :boolean ? nil : type_name,
    enum: @enum ? @enum.collect(&:to_s) : nil
  }
end
type_name() click to toggle source

Translate the type of this option @return [String] The translated type

# File lib/translatomatic/option.rb, line 66
def type_name
  t("config.types.#{type}")
end

Private Instance Methods

constructor_option?(key) click to toggle source
# File lib/translatomatic/option.rb, line 94
def constructor_option?(key)
  CONSTRUCTOR_OPTIONS.include?(key)
end
thor_type() click to toggle source
# File lib/translatomatic/option.rb, line 98
def thor_type
  case @type
  when :array, :path_array, :string, :path
    # use internal ',' splitting for array types on command line
    :string
  else
    @type
  end
end