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



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)



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

def command_line_only
  @command_line_only
end

#defaultObject (readonly)



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

def default
  @default
end

#descriptionString (readonly)



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

def description
  @description
end

#env_nameString (readonly)



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

def env_name
  @env_name
end

#hiddenboolean (readonly)



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

def hidden
  @hidden
end

#nameString (readonly)



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

def name
  @name
end

#requiredboolean (readonly)



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

def required
  @required
end

#typeSymbol (readonly)



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

def type
  @type
end

#user_location_onlyboolean (readonly)



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.



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



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



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

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