Sha256: ed003dcaf2d776ba1194853a39535769c7b1a56f23edb3a853d3b94f8393880e

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

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

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

    def shortcut(name, shortcut = nil, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
      name = name.to_sym
      # TODO: blocklist of names to avoid conflicts with standard methods?
      if Grumlin::Action::REGULAR_STEPS.include?(name)
        raise ArgumentError,
              "cannot use names of standard gremlin steps"
      end

      if (shortcut.nil? && block.nil?) || (shortcut && block)
        raise ArgumentError, "either shortcut or block must be passed"
      end

      shortcut ||= Shortcut.new(name, &block)

      raise ArgumentError, "shortcut '#{name}' already exists" if shortcuts.key?(name) && shortcuts[name] != shortcut

      shortcuts[name] = shortcut
    end

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
grumlin-0.19.7 lib/grumlin/shortcuts.rb
grumlin-0.19.6 lib/grumlin/shortcuts.rb
grumlin-0.19.5 lib/grumlin/shortcuts.rb
grumlin-0.19.4 lib/grumlin/shortcuts.rb
grumlin-0.19.3 lib/grumlin/shortcuts.rb
grumlin-0.19.2 lib/grumlin/shortcuts.rb
grumlin-0.19.1 lib/grumlin/shortcuts.rb
grumlin-0.19.0 lib/grumlin/shortcuts.rb
grumlin-0.18.1 lib/grumlin/shortcuts.rb
grumlin-0.18.0 lib/grumlin/shortcuts.rb
grumlin-0.17.0 lib/grumlin/shortcuts.rb
grumlin-0.16.1 lib/grumlin/shortcuts.rb
grumlin-0.16.0 lib/grumlin/shortcuts.rb