Sha256: 4223eb7030183c4afc8fba0442609d106fa1c2c5b79ad3dbde9d2b2f39b4a70e

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true


# 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

8 entries across 8 versions & 1 rubygems

Version Path
locd-0.1.13 lib/locd/label.rb
locd-0.1.12 lib/locd/label.rb
locd-0.1.11 lib/locd/label.rb
locd-0.1.10 lib/locd/label.rb
locd-0.1.9 lib/locd/label.rb
locd-0.1.8 lib/locd/label.rb
locd-0.1.7 lib/locd/label.rb
locd-0.1.6 lib/locd/label.rb