Class: Translatomatic::Option

Inherits:
Object
  • Object
show all
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(data = {}) ⇒ Translatomatic::Option

Create a new option

Parameters:

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

    Attributes as above



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/translatomatic/option.rb', line 37

def initialize(data = {})
  @name = data[:name]
  @required = data[:required]
  @description = data[:desc]
  @use_env = data[:use_env]
  @hidden = data[:hidden]
  @type = data[:type] || :string
  @default = data[:default]
  @aliases = data[:aliases]
  @enum = data[:enum]
  @user_context_only = data[:user_context_only]
  @command_line_only = data[:command_line_only]
  @env_name = data[:env_name] || (@use_env ? @name.to_s.upcase : nil)
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_context_onlyboolean (readonly)

Returns True if this option can only be set in user context.

Returns:

  • (boolean)

    True if this option can only be set in user context



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

def user_context_only
  @user_context_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:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/translatomatic/option.rb', line 69

def self.options_from_object(object)
  options = []
  if object.respond_to?(:options)
    options += options_from_object(object.options)
  elsif object.kind_of?(Array)
    object.each do |item|
      if item.kind_of?(Translatomatic::Option)
        options << item
      elsif item.respond_to?(:options)
        options += options_from_object(item.options)
      end
    end
  end
  options
end

Instance Method Details

#to_thorObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/translatomatic/option.rb', line 52

def to_thor
  # use internal ',' splitting for array types on command line
  type = @type == :array ? :string : @type

  { name: @name,
    required: @required,
    type: type,
    desc: @description,
    default: @default,
    aliases: @aliases,
    enum: @enum ? @enum.collect { |i| i.to_s } : nil
  }
end