Sha256: 9c64222ea793c1f423048a93414e13b1612d20efd1da1767a1af66664655bfbe

Contents?: true

Size: 939 Bytes

Versions: 22

Compression:

Stored size: 939 Bytes

Contents

module Earthquake
  module IdVar
    attr_accessor :id_var

    def id2var(id)
      id_var.id2var(id)
    end

    def var2id(var)
      id_var.var2id(var)
    end

    class Gen
      def initialize(vars = ('aa'..'zz').to_a, prefix = '$')
        if not vars.kind_of?(Array)
          raise ArgumentError, 'vars should be an Array'
        elsif vars.empty?
          raise ArgumentError, 'vars should not be empty'
        end
        @vars = vars.map { |var| prefix + var }
        @table = {}
        @rtable = {}
        @prefix = prefix
      end

      def var2id(var)
        @table[var]
      end

      def id2var(id)
        @rtable[id] || self.next(id)
      end

      def next(id)
        var = @vars.shift
        @vars.push var
        @rtable.delete(@table[var])
        @table[var] = id
        @rtable[id] = var
        var
      end
    end
  end

  extend IdVar

  init do
    self.id_var ||= IdVar::Gen.new
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
earthquake-0.6.11 lib/earthquake/id_var.rb
earthquake-0.6.10 lib/earthquake/id_var.rb
earthquake-0.6.9 lib/earthquake/id_var.rb
earthquake-0.6.8 lib/earthquake/id_var.rb
earthquake-0.6.7 lib/earthquake/id_var.rb
earthquake-0.6.6 lib/earthquake/id_var.rb
earthquake-0.6.5 lib/earthquake/id_var.rb
earthquake-0.6.4 lib/earthquake/id_var.rb
earthquake-0.6.3 lib/earthquake/id_var.rb
earthquake-0.6.2 lib/earthquake/id_var.rb
earthquake-0.6.1 lib/earthquake/id_var.rb
earthquake-0.6.0 lib/earthquake/id_var.rb
earthquake-0.5.9 lib/earthquake/id_var.rb
earthquake-0.5.8 lib/earthquake/id_var.rb
earthquake-0.5.7 lib/earthquake/id_var.rb
earthquake-0.5.6 lib/earthquake/id_var.rb
earthquake-0.5.5 lib/earthquake/id_var.rb
earthquake-0.5.4 lib/earthquake/id_var.rb
earthquake-0.5.3 lib/earthquake/id_var.rb
earthquake-0.5.2 lib/earthquake/id_var.rb