Sha256: 220b992a1cf79037ccb8ee52fc135a971e82f3ab4c7a0405bba601c9635906b2

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

require_relative 'comp_bitz'

module LanguageCards
  class Comparator
    def initialize mapping_key, mapping, collection
      ##
      # @title should be a hash like {"Romaji"=>"Katakana"}
      # @mapping should be an array like [:k, :v]
      raise I18n.t('Error.MappingNotFound') unless mapping.has_key? mapping_key
      begin
        @mapped_as, @mapping = mapping[mapping_key].reduce 
      rescue LocalJumpError
        raise I18n.t('Errors.InvalidMapping')
      end

      @collection = collection
    end

    def mapped_as
      case @mapping.first
      when :k
        @mapped_as.keys.first
      else
        @mapped_as.values.first
      end
    end

    def given key, value
      CompBitz.new(
        display: choose_display(key, value),
        collection: @collection,
        expected: choose_expected(key, value),
        value: value,
        mapping: @mapping.first
      )
    end

    def match? input, comp_bitz
      case comp_bitz.mapping 
      when :k
        comp_bitz.value == comp_bitz.collection[input]
      when :v
        comp_bitz.value == input
      end
    end

    private
    def choose_display key, value
      case @mapping.last
      when :k
        key
      when :v
        value
      end
    end

    def choose_expected key, value
      case @mapping.first
      when :k
        key
      when :v
        value
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
language_cards-0.1.3 lib/language_cards/comparator.rb
language_cards-0.1.2 lib/language_cards/comparator.rb
language_cards-0.1.1 lib/language_cards/comparator.rb
language_cards-0.1.0 lib/language_cards/comparator.rb