Sha256: 55f504fb583b4b530856b0ca8171edb3a4d885c89fc7a140c75106b54c53e8bf

Contents?: true

Size: 1.74 KB

Versions: 11

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

class Grumlin::Shortcuts::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_delegator :self, :__, :g

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

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

    sc = step_class

    shortcut_methods_module.define_method(name) do |*args, **params|
      next sc.new(name, args: args, params: params, previous_step: self, pool: Grumlin.default_pool)
    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 __
    traversal_start_class.new(pool: Grumlin.default_pool)
  end

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

  def step_class
    @step_class ||= shortcut_aware_class(Grumlin::Step)
  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
    step_class.include(m)
    traversal_start_class.include(m)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
grumlin-1.0.3.beta1 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.2 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.1 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc7 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc6 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc5 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc4 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc3 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc2 lib/grumlin/shortcuts/storage.rb
grumlin-1.0.0.rc1 lib/grumlin/shortcuts/storage.rb