class Translatomatic::Option
Stores details about command line and object constructor options
Constants
- CONSTRUCTOR_OPTIONS
- VALID_TYPES
Attributes
@return [boolean] True if this option can only be set on the command line
@return [Object] The default value for this option
@return [String] Description of the option
@return [String] If set, the name of the environment variable
that can be used to set this option in the environment.
@return [String] Name of the option
@return [boolean] True if this option is required
@return [Symbol] Type of option, one of:
:string, :hash, :array, :numeric, or :boolean
@return [boolean] True if this option can only be set in the
user configuration file
Public Class Methods
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
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
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
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
# File lib/translatomatic/option.rb, line 94 def constructor_option?(key) CONSTRUCTOR_OPTIONS.include?(key) end
# 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