Sha256: 0f355c3ce7a13fb46662edf518fb10611bfc3ec5a69004e8495bdf70e5256a04

Contents?: true

Size: 984 Bytes

Versions: 5

Compression:

Stored size: 984 Bytes

Contents

# frozen_string_literal: true

module Grumlin
  module Shortcuts
    module InstanceMethods
      def with_shortcuts(obj)
        ShortcutProxy.new(obj, self.class.shortcuts, parent: self)
      end
    end

    def self.extended(base)
      base.include(InstanceMethods)
      base.include(Grumlin::Expressions)
    end

    def inherited(subclass)
      super
      subclass.shortcuts_from(self)
    end

    def shortcut(name, &block)
      name = name.to_sym
      # TODO: blocklist of names to avoid conflicts with standard methods?
      raise ArgumentError, "cannot use names of standard gremlin steps" if Grumlin.supported_steps.include?(name)

      raise ArgumentError, "shortcut '#{name}' already exists" if shortcuts.key?(name)

      shortcuts[name] = block
    end

    def shortcuts_from(other_shortcuts)
      other_shortcuts.shortcuts.each do |name, block|
        shortcut(name, &block)
      end
    end

    def shortcuts
      @shortcuts ||= {}
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
grumlin-0.14.3 lib/grumlin/shortcuts.rb
grumlin-0.14.2 lib/grumlin/shortcuts.rb
grumlin-0.14.1 lib/grumlin/shortcuts.rb
grumlin-0.14.0 lib/grumlin/shortcuts.rb
grumlin-0.13.1 lib/grumlin/shortcuts.rb