Sha256: 4adcce054f702ea9f5fe3ee4645159a77adf01641ff217793742454a67ec3cb4

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

# encoding=utf-8
require 'digest'
# require_relative 'poly'

$pd = false unless defined?($pd)

class Hash
  # block name in commands and documents
  def pub_name(**kwargs)
    full = fetch(:nickname, nil) || fetch(:oname, nil)
    full&.to_s&.pub_name(**kwargs).tap { |ret| pp [__LINE__, 'Hash.pub_name() ->', ret] if $pd }
  end
end

class String
  FN_ID_LEN = 4
  FN_MAX_LEN = 64
  FN_PATTERN = %r{[^!#%\+\-0-9=@A-Z_a-z()\[\]{}]}.freeze # characters than can be used in a file name without quotes or escaping
  # except '.', ',', '~' reserved for tokenization
  # / !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
  FN_REPLACEMENT = '_'

  # block name in commands and documents
  def pub_name(
    id_len: FN_ID_LEN, max_len: FN_MAX_LEN,
    pattern: FN_PATTERN, replacement: FN_REPLACEMENT
  )
    trimmed = if self[max_len]
                rand(((10**(id_len - 1)) + 1)..(10**id_len)).to_s
                dig = Digest::MD5.hexdigest(self)[0, id_len]
                self[0..max_len - id_len] + dig
              else
                self
              end

    trimmed.gsub(pattern, replacement).tap { |ret| pp [__LINE__, 'String.pub_name() ->', ret] if $pd }
  end
end

# require 'ostruct'

# class BlkS < OpenStruct
#   # Method to fetch the value associated with the attribute :nickname or :oname
#   def pub_name
#     self.nickname || self.oname
#   end
# end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
markdown_exec-2.3.0 lib/namer.rb
markdown_exec-2.2.0 lib/namer.rb