Class: Translatomatic::Option

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/option.rb

Overview

Stores details about command line and object constructor options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Translatomatic::Option

Create a new option

Parameters:

  • attributes (Hash<Symbol,Object>) (defaults to: {})

    Attributes as above



38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#command_line_onlyboolean (readonly)

Returns True if this option can only be set on the command line.

Returns:

  • (boolean)

    True if this option can only be set on the command line



29
30
31
# File 'lib/translatomatic/option.rb', line 29

def command_line_only
  @command_line_only
end

#defaultObject (readonly)

Returns The default value for this option.

Returns:

  • (Object)

    The default value for this option



26
27
28
# File 'lib/translatomatic/option.rb', line 26

def default
  @default
end

#descriptionString (readonly)

Returns Description of the option.

Returns:

  • (String)

    Description of the option



15
16
17
# File 'lib/translatomatic/option.rb', line 15

def description
  @description
end

#env_nameString (readonly)

Returns If set, the name of the environment variable that can be used to set this option in the environment.

Returns:

  • (String)

    If set, the name of the environment variable that can be used to set this option in the environment.



12
13
14
# File 'lib/translatomatic/option.rb', line 12

def env_name
  @env_name
end

#hiddenboolean (readonly)

Returns If true, the option does not appear on the command line but it can be used in configuration settings.

Returns:

  • (boolean)

    If true, the option does not appear on the command line but it can be used in configuration settings



19
20
21
# File 'lib/translatomatic/option.rb', line 19

def hidden
  @hidden
end

#nameString (readonly)

Returns Name of the option.

Returns:

  • (String)

    Name of the option



5
6
7
# File 'lib/translatomatic/option.rb', line 5

def name
  @name
end

#requiredboolean (readonly)

Returns True if this option is required.

Returns:

  • (boolean)

    True if this option is required



8
9
10
# File 'lib/translatomatic/option.rb', line 8

def required
  @required
end

#typeSymbol (readonly)

Returns Type of option, one of: :string, :hash, :array, :numeric, or :boolean.

Returns:

  • (Symbol)

    Type of option, one of: :string, :hash, :array, :numeric, or :boolean



23
24
25
# File 'lib/translatomatic/option.rb', line 23

def type
  @type
end

#user_location_onlyboolean (readonly)

Returns True if this option can only be set in the user configuration file.

Returns:

  • (boolean)

    True if this option can only be set in the user configuration file



33
34
35
# File 'lib/translatomatic/option.rb', line 33

def user_location_only
  @user_location_only
end

Class Method Details

.options_from_object(object) ⇒ Array<Translatomatic::Option>

Retrieve all options from an object or list of objects.

Parameters:

  • object (#options, Array<#options>)

    Options source

Returns:



73
74
75
76
77
78
79
80
81
82
83
# 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

Instance Method Details

#to_thorHash

Return sconfiguration for a thor command line option

Returns:

  • (Hash)

    Thor configuration



52
53
54
55
56
57
58
59
60
61
62
# 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_nameString

Translate the type of this option

Returns:

  • (String)

    The translated type



66
67
68
# File 'lib/translatomatic/option.rb', line 66

def type_name
  t("config.types.#{type}")
end