Sha256: ce18dad2b5674e9c36fa28260714e072b4767c1030efae2247172bf0d4a7505e

Contents?: true

Size: 793 Bytes

Versions: 5

Compression:

Stored size: 793 Bytes

Contents

# frozen_string_literal: true

module Factrey
  class Ref
    # Provides shorthand methods for creating {Ref}s and {Defer}s.
    module ShorthandMethods
      # @return [Ref, Builder, Defer]
      # @example
      #   include Factrey::Ref::ShorthandMethods
      #
      #   # `ref(symbol)` returns a `Ref` instance
      #   ref(:foo)
      #   # `ref` returns a `Ref::Builder` instance; thus, we can write
      #   ref.foo
      #   # `ref { ... }` returns a `Ref::Defer` instance
      #   ref { |foo, bar| foo + bar }
      def ref(name = nil, &)
        if name
          raise ArgumentError, "both name and block given" if block_given?

          Ref.new(name)
        elsif block_given?
          Defer.new(&)
        else
          Builder.new
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
factrey-0.5.0 lib/factrey/ref/shorthand_methods.rb
factrey-0.4.0 lib/factrey/ref/shorthand_methods.rb
factrey-0.3.0 lib/factrey/ref/shorthand_methods.rb
factrey-0.2.0 lib/factrey/ref/shorthand_methods.rb
factrey-0.1.0 lib/factrey/ref/shorthand_methods.rb