Sha256: 7b443313976c84c94a89f518ad1deccb622b773a39af4e2aae5643175b53174c

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require "singleton"
module Damn
  module Legacy
    class Store
      attr_reader :store, :stack

      include Singleton
      def initialize
        init
      end

      def add_meth(from, to)
        val = add(from, to)
        stack.unshift([from.to_s, val])
        val
      end

      def add(from, to)
        return add_array_to_store(from, to, "#") if to.is_a?(Array)
        return add_hash_to_store(from, to, "#") if to.is_a?(Hash)
        return add_sym_to_store(from, to, "#") if to.is_a?(Symbol)

        add_default_to_store(from, to, "#")
      end

      def clean
        init
      end

      private

      def init
        @store = Hash.new { |h, k| h[k] = [] }
        @stack = []
      end

      def add_array_to_store(key, value, glue)
        value.map do |m|
          next add_sym_to_store(key, m, glue) if m.is_a?(Symbol)
          next add_array_to_store(key, m, glue) if m.is_a?(Array)
          next add_hash_to_store(key, m, glue) if m.is_a?(Hash)

          add_default_to_store(key, m, glue)
        end.flatten
      end

      def add_hash_to_store(key, value, glue)
        value.map do |k, v|
          next_key = "#{key}#{glue}#{k}"
          store[key.to_s] << next_key
          next add_sym_to_store(next_key, v, glue) if v.is_a?(Symbol)
          next add_hash_to_store(next_key, v, glue) if v.is_a?(Hash)
          next add_array_to_store(next_key, v, glue) if v.is_a?(Array)

          add_default_to_store(next_key, v, glue)
        end.flatten
      end

      def add_sym_to_store(key, value, glue)
        new_value = "#{key}#{glue}#{value}"
        store[key.to_s] << new_value
        [new_value]
      end

      def add_default_to_store(key, value, _glue)
        new_value = value.to_s
        store[key.to_s] << new_value
        store[new_value]
        [new_value]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
damn-legacy-0.0.2 lib/damn/legacy/store.rb