Method: Command::Setting#casting_variable

Defined in:
lib/command/setting.rb

#casting_variable(name, value) ⇒ Object

値の文字列を設定に基づいた型にキャストして、[scope, value] 形式で返す不正な設定名もしくは値の場合は例外を吐く



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/command/setting.rb', line 86

def casting_variable(name, value)
  scope = get_scope_of_variable_name(name)
  unless scope
    raise Helper::InvalidVariableName, "#{name} は不明な名前です"
  end
  variable_definition = SETTING_VARIABLES[scope][name]
  case variable_definition[:type]
  when :select
    select_values = variable_definition[:select_keys]
    unless select_values.include?(value)
      raise InvalidSelectValue, "不明な値です。#{select_values.join(", ")} の中から指定して下さい"
    end
    casted_value = value
  when :multiple
    select_values = variable_definition[:select_keys]
    value.split(",").each do |input_value|
      unless select_values.include?(input_value)
        raise InvalidSelectValue, "不明な値です。#{select_values.join(", ")} の中から指定して下さい"
      end
    end
    casted_value = value
  else
    casted_value = Helper.string_cast_to_type(value, variable_definition[:type])
  end
  [scope, casted_value]
end