Sha256: 67839a00fbade57b1f149a9ca3e9257f92b666d14f39bc59b742e87670f1529c

Contents?: true

Size: 1.29 KB

Versions: 46

Compression:

Stored size: 1.29 KB

Contents

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

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
eco-helpers-1.3.3 lib/eco/data/mapper.rb
eco-helpers-1.3.2 lib/eco/data/mapper.rb
eco-helpers-1.3.1 lib/eco/data/mapper.rb
eco-helpers-1.2.2 lib/eco/data/mapper.rb
eco-helpers-1.2.1 lib/eco/data/mapper.rb
eco-helpers-1.1.8 lib/eco/data/mapper.rb
eco-helpers-1.1.7 lib/eco/data/mapper.rb
eco-helpers-1.1.6 lib/eco/data/mapper.rb
eco-helpers-1.1.5 lib/eco/data/mapper.rb
eco-helpers-1.1.4 lib/eco/data/mapper.rb
eco-helpers-1.1.3 lib/eco/data/mapper.rb
eco-helpers-1.1.2 lib/eco/data/mapper.rb
eco-helpers-1.1.1 lib/eco/data/mapper.rb
eco-helpers-1.0.14 lib/eco/data/mapper.rb
eco-helpers-1.0.13 lib/eco/data/mapper.rb
eco-helpers-1.0.12 lib/eco/data/mapper.rb
eco-helpers-1.0.11 lib/eco/data/mapper.rb
eco-helpers-1.0.10 lib/eco/data/mapper.rb
eco-helpers-1.0.9 lib/eco/data/mapper.rb
eco-helpers-1.0.8 lib/eco/data/mapper.rb