Sha256: 8745b240c066db2e6d1b2b503d7bd5b259e32b7d5afb4308a299120f41d58741

Contents?: true

Size: 1.99 KB

Versions: 9

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Grumlin
  module Shortcuts
    class Storage
      extend Forwardable

      class << self
        def [](other)
          new(other)
        end

        def empty
          @empty ||= new
        end
      end

      def initialize(storage = {})
        @storage = storage
        storage.each do |n, s|
          add(n, s)
        end
      end

      def_delegator :@storage, :[]
      def_delegator :@storage, :include?, :known?
      def_delegator :@storage, :keys, :names

      def ==(other)
        @storage == other.storage
      end

      def add(name, shortcut)
        @storage[name] = shortcut

        ac = action_class

        shortcut_methods_module.define_method(name) do |*args, **params|
          next ac.new(name, args: args, params: params, previous_step: self)
        end
        extend_traversal_classes(shortcut) unless shortcut.lazy?
      end

      def add_from(other)
        other.storage.each do |name, shortcut|
          add(name, shortcut)
        end
      end

      def g
        __
      end

      def __
        @__ ||= traversal_start_class.new
      end

      def traversal_start_class
        @traversal_start_class ||= shortcut_aware_class(TraversalStart)
      end

      def action_class
        @action_class ||= shortcut_aware_class(Action)
      end

      protected

      attr_reader :storage

      private

      def shortcut_methods_module
        @shortcut_methods_module ||= begin
          shorts = self
          Module.new do
            define_method :shortcuts do
              shorts
            end
          end
        end
      end

      def shortcut_aware_class(base)
        methods = shortcut_methods_module
        Class.new(base) do
          include methods
        end
      end

      def extend_traversal_classes(shortcut)
        m = Module.new do
          define_method(shortcut.name, &shortcut.block)
        end
        action_class.include(m)
        traversal_start_class.include(m)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
grumlin-0.22.4 lib/grumlin/shortcuts/storage.rb
grumlin-0.22.3 lib/grumlin/shortcuts/storage.rb
grumlin-0.22.2 lib/grumlin/shortcuts/storage.rb
grumlin-0.22.1 lib/grumlin/shortcuts/storage.rb
grumlin-0.22.0 lib/grumlin/shortcuts/storage.rb
grumlin-0.21.1 lib/grumlin/shortcuts/storage.rb
grumlin-0.21.0 lib/grumlin/shortcuts/storage.rb
grumlin-0.20.2 lib/grumlin/shortcuts/storage.rb
grumlin-0.20.1 lib/grumlin/shortcuts/storage.rb