Sha256: 7ceca713630bd77c538de28b46e24ecedb210af47737a6beee27710eea6d95de

Contents?: true

Size: 644 Bytes

Versions: 1

Compression:

Stored size: 644 Bytes

Contents

# frozen_string_literal: true

# NameRudiment is a module to be included in classes whose instances may be identified by name.
module HeadMusic::Named
  attr_reader :name
  delegate :to_s, to: :name

  def initialize(name)
    @name = name.to_s
  end

  def hash_key
    HeadMusic::Utilities::HashKey.for(name)
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  # Adds .get_by_name to the including class.
  module ClassMethods
    def get_by_name(name)
      name = name.to_s
      @instances_by_name ||= {}
      key = HeadMusic::Utilities::HashKey.for(name)
      @instances_by_name[key] ||= new(name)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
head_music-0.23.4 lib/head_music/named.rb