Class: Translatomatic::Option
- Inherits:
-
Object
- Object
- Translatomatic::Option
- Includes:
- Util
- Defined in:
- lib/translatomatic/option.rb
Overview
Stores details about command line and object constructor options
Instance Attribute Summary collapse
-
#command_line_only ⇒ boolean
readonly
True if this option can only be set on the command line.
-
#default ⇒ Object
readonly
The default value for this option.
-
#description ⇒ String
readonly
Description of the option.
-
#env_name ⇒ String
readonly
If set, the name of the environment variable that can be used to set this option in the environment.
-
#hidden ⇒ boolean
readonly
If true, the option does not appear on the command line but it can be used in configuration settings.
-
#name ⇒ String
readonly
Name of the option.
-
#required ⇒ boolean
readonly
True if this option is required.
-
#type ⇒ Symbol
readonly
Type of option, one of: :string, :hash, :array, :numeric, or :boolean.
-
#user_location_only ⇒ boolean
readonly
True if this option can only be set in the user configuration file.
Class Method Summary collapse
-
.options_from_object(object) ⇒ Array<Translatomatic::Option>
Retrieve all options from an object or list of objects.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Translatomatic::Option
constructor
Create a new option.
-
#to_thor ⇒ Hash
Return sconfiguration for a thor command line option.
-
#type_name ⇒ String
Translate the type of this option.
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_only ⇒ boolean (readonly)
Returns 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 |
#default ⇒ Object (readonly)
Returns The default value for this option.
26 27 28 |
# File 'lib/translatomatic/option.rb', line 26 def default @default end |
#description ⇒ String (readonly)
Returns Description of the option.
15 16 17 |
# File 'lib/translatomatic/option.rb', line 15 def description @description end |
#env_name ⇒ String (readonly)
Returns 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 |
#hidden ⇒ boolean (readonly)
Returns 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 |
#name ⇒ String (readonly)
Returns Name of the option.
5 6 7 |
# File 'lib/translatomatic/option.rb', line 5 def name @name end |
#required ⇒ boolean (readonly)
Returns True if this option is required.
8 9 10 |
# File 'lib/translatomatic/option.rb', line 8 def required @required end |
#type ⇒ Symbol (readonly)
Returns 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_only ⇒ boolean (readonly)
Returns 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.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/translatomatic/option.rb', line 73 def self.(object) if object.is_a?(Translatomatic::Option) [object] elsif object.respond_to?(:options) (object.) elsif object.is_a?(Array) object.collect { |i| (i) }.flatten else [] end end |
Instance Method Details
#to_thor ⇒ Hash
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_name ⇒ String
Translate the type of this option
66 67 68 |
# File 'lib/translatomatic/option.rb', line 66 def type_name t("config.types.#{type}") end |