class NumbersTranslator::Engine

Constants

FIRST_NUMBER
FIRST_POSITION
SECOND_NUMBER
SECOND_POSITION
THIRD_NUMBER

Public Class Methods

new(data, data_storage, lang) click to toggle source
# File lib/numbers_translator/engine.rb, line 13
def initialize(data, data_storage, lang)
  @data = data.get_data
  @data_storage = data_storage
  @separator = (lang == 'uk') ? ' and ' : ' '
  @lang = lang
  @result_string = ''
end

Public Instance Methods

run() click to toggle source
# File lib/numbers_translator/engine.rb, line 21
def run

  @data[:numeric_groups].each do |key|
    data = ''

    if key.length > 1

      data = for_fist_element(key, data) if key.length == 3

      if key[THIRD_NUMBER]
        if_count_numbers_three(key, data)
      else
        if_count_numbers_not_three(key, data)
      end
    else
      for_fist_element(key, data);
    end

    if key.split('').map {|e| e.to_i}.sum != 0
      @result_string += ' ' + @data_storage[:breakdown][@data[:count_numeric_groups].to_s]
    end

    @data[:count_numeric_groups] -= 1

  end
  postprocessing
end

Protected Instance Methods

concatenation_to_string(params) click to toggle source
# File lib/numbers_translator/engine.rb, line 72
def concatenation_to_string(params)
  str = ''

  if params[:data].is_a? Hash

    if params[:position]
      str = (params[:position] == FIRST_POSITION) ?
                "-#{params[:data][params[:position].to_s]}" :
                "#{@separator}#{params[:data][params[:position].to_s]}"

      str.gsub!('-', @separator) if params[:before_zero]
    else
      str = " #{params[:data][params[:key].length.to_s]}"
    end

  else
    str = params[:not_and] ? params[:data] : @separator + params[:data] unless params[:data].empty?
  end
  @result_string += str
  ''
end
for_fist_element(key, data) click to toggle source
# File lib/numbers_translator/engine.rb, line 179
def for_fist_element(key, data)
  get_from_storage_and_concat(
      key_in_storage: FIRST_NUMBER,
      key: key,
      data: data
  )
end
get_from_storage_and_concat(params) click to toggle source
# File lib/numbers_translator/engine.rb, line 51
def get_from_storage_and_concat(params)

  if params[:for_search_in_the_storage].is_a? Array

    data = @data_storage[:comparison][
        params[:key][params[:for_search_in_the_storage][FIRST_NUMBER]] +
            params[:key][params[:for_search_in_the_storage][SECOND_NUMBER]]
    ]

    params[:data] = data
    concatenation_to_string(params)
  else
    data = @data_storage[:comparison][params[:key][params[:key_in_storage]]]

    params[:data] = data
    data = concatenation_to_string(params)
  end

  data
end
get_params() click to toggle source
# File lib/numbers_translator/engine.rb, line 196
def get_params
  {
      key_in_storage: nil,
      key: nil,
      data: '',
      position: nil,
      not_and: nil,
      before_zero: nil,
      for_search_in_the_storage: nil
  }
end
if_count_numbers_not_three(key, data) click to toggle source
# File lib/numbers_translator/engine.rb, line 136
def if_count_numbers_not_three(key, data)

  if @data_storage[:comparison].has_key?(
      key[FIRST_NUMBER] +
          key[SECOND_NUMBER]
  )
    data = get_from_storage_and_concat(
        key: key,
        data: data,
        position: nil,
        not_and: true,
        for_search_in_the_storage: [FIRST_NUMBER, SECOND_NUMBER]
    )
  else
    if key.length == 2

      data = get_from_storage_and_concat(
          key_in_storage: FIRST_NUMBER,
          key: key,
          data: data,
          position: SECOND_POSITION,
          not_and: true
      )

      data = get_from_storage_and_concat(
          key_in_storage: SECOND_NUMBER,
          key: key,
          data: data,
          position: FIRST_POSITION
      )
    else

      data = get_from_storage_and_concat(
          key_in_storage: SECOND_NUMBER,
          key: key,
          data: data
      )
    end
  end

  data
end
if_count_numbers_three(key, data) click to toggle source
# File lib/numbers_translator/engine.rb, line 94
def if_count_numbers_three(key, data)

  if @data_storage[:comparison].has_key?(key[SECOND_NUMBER] +
                                             key[THIRD_NUMBER]
  )
    data = get_from_storage_and_concat(
        key: key,
        data: data,
        for_search_in_the_storage: [SECOND_NUMBER, THIRD_NUMBER]
    )
  end

  if data.empty?

    get_from_storage_and_concat(
        key_in_storage: SECOND_NUMBER,
        key: key,
        data: data,
        position: SECOND_POSITION
    )

    data = @data_storage[:comparison][key[THIRD_NUMBER]]

    data = key[SECOND_NUMBER] != '0' ?

               concatenation_to_string(
                   key: key,
                   data: data,
                   position: FIRST_POSITION
               ) :

               concatenation_to_string(
                   key: key,
                   data: data,
                   position: FIRST_POSITION,
                   before_zero: true
               )
  end

  data
end
merge_params(params) click to toggle source
# File lib/numbers_translator/engine.rb, line 208
def merge_params(params)
  get_params.merge(params)
end
postprocessing() click to toggle source
# File lib/numbers_translator/engine.rb, line 187
def postprocessing

  if @result_string.start_with?(@separator) && @lang == 'uk'
    @result_string = @result_string.slice(@separator.length..@result_string.length)
  end

  @result_string.strip
end