module Eco module Data class Mapper # it expects [[v1a, v1b], [v2a, v2b] ...] def initialize (array_of_arrays = [], internal: :last) @source = array_of_arrays if @source # internal should be always last in @source @source = @source.map { |a| a.reverse } unless internal == :last # first declarations take priority @by_external = @source.reverse.to_h @by_internal = @source.reverse.map do |pair| pair.reverse end.to_h end end def list(type = :internal) return [] if !@source @source.map do |pair| type == :internal ? pair.last : pair.first end.uniq end def internal?(value) return true if !@source @by_internal.key?(value) end def external?(value) return true if !@source @by_external.key?(value) end def include?(value) return true if !@source internal?(value) || external?(value) end def to_internal(value) return value if !@source @by_external[value] end def to_external(value) return value if !@source @by_internal[value] end end end end