Sha256: f943b44054cb2a8f103c4b843d47a7a840b44f27d26637c042b978d8b7c2a386

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

# Refinements
# =======================================================================

using NRSER


# Definitions
# =======================================================================

# Static method namespace for dealing with labels.
# 
module Locd::Label
  
  def self.url_for label, tld:, port:
    "http://#{ label }:#{ port }"
  end
  
  
  # Simple transformation of "glob-style" label patterns to {Regexp}.
  # 
  # **_Right now only handles the `*` wildcard!_**
  # 
  # @param [String] string
  #   Glob-style pattern to search for in {Locd::Agent#label} strings.
  # 
  # @param [Boolean] full:
  #   When `false`, the returned Regexp will match any part of label
  #   strings.
  #   
  #   When `true`, the generated Regexp will only match if `pattern` matches
  #   entire label strings.
  # 
  # @param [Boolean] ignore_case:
  #   Makes the Regexp case-insensitive.
  # 
  # 
  def self.regexp_for_glob string, full: false, ignore_case: false
    string.
      # Uncommon option: `-1` causes empty segments to be included, which
      # fixes the issue of `*` at start or end
      split( '*', -1 ).
      map( &Regexp.method( :escape ) ).
      join( '.*' ).
      thru { |regexp_string|
        # Add `\A` and `\z` caps if `exact` is set
        regexp_string = "\\A#{ regexp_string }\\z" if full
        
        Regexp.new regexp_string, ignore_case
      }
  end
  
  
  def self.compare label_a, label_b
    [label_a, label_b].
      map { |label| label.split( '.' ).reverse }.
      thru { |a, b| a <=> b }
  end
    
end # module Locd::Label

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locd-0.1.5 lib/locd/label.rb
locd-0.1.4 lib/locd/label.rb