Sha256: 6e66d9cb5aaaac32c1fc6652274a313222b55be1a8e5e04630c13906f52145aa

Contents?: true

Size: 785 Bytes

Versions: 4

Compression:

Stored size: 785 Bytes

Contents

# frozen_string_literal: true

module Speculation
  module NamespacedSymbols
    # @param [#to_s] namespace
    # @param [#to_s] name
    # @return [Symbol] concatenation of `namespace` and `name`
    # @example
    #     ns(Foo::Bar, :foo)
    #     # => :"Foo::Bar/baz"
    def ns(name_or_namespace, name = nil)
      if name
        namespace = name_or_namespace
      else
        name = name_or_namespace
        namespace = is_a?(Module) ? self.name : self.class.name
      end

      NamespacedSymbols.symbol(namespace, name)
    end

    def self.symbol(ns, name)
      :"#{ns}/#{name}"
    end

    def self.name(sym)
      sym.to_s.split("/").last
    end

    def self.namespace(sym)
      parts = sym.to_s.split("/")
      parts.first if parts.count == 2
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
speculation-0.4.0 lib/speculation/namespaced_symbols.rb
speculation-0.3.1 lib/speculation/namespaced_symbols.rb
speculation-0.3.0 lib/speculation/namespaced_symbols.rb
speculation-0.2.0 lib/speculation/namespaced_symbols.rb