Sha256: 741e68b1984bda9ae4d388a802f6c6b0ce5c4f16bdc4fe62f93ff066ac68dff9

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Fig; end

# Parsed representation of a package (name/version:config).
class Fig::PackageDescriptor
  include Comparable

  attr_reader :name, :version, :config

  def self.format(name, version, config, use_default_config = false)
    string = name || ''

    if version
      string += '/'
      string += version
    end

    if config
      string += ':'
      string += config
    elsif use_default_config
      string += ':default'
    end

    return string
  end

  def self.parse(raw_string)
    # Additional checks in validate_component() will take care of the looseness
    # of the regexes.  These just need to ensure that the entire string gets
    # assigned to one component or another.

    self.new(
      raw_string =~ %r< \A         ( [^:/]+ )    >x ? $1 : nil,
      raw_string =~ %r< \A [^/]* / ( [^:]+  )    >x ? $1 : nil,
      raw_string =~ %r< \A [^:]* : ( .+     ) \z >x ? $1 : nil
    )
  end

  def initialize(name, version, config)
    @name     = name
    @version  = version
    @config   = config
  end

  def validate_component(value, name)
    return if value.nil?

    return if value =~ / \A [a-zA-Z0-9_.-]+ \z /x

    raise %Q<Invalid #{name} for package descriptor: "#{value}".>
  end

  # Specifically not named :to_s because it doesn't act like that should.
  def to_string(use_default_config = false)
    return Fig::PackageDescriptor.format(
      @name, @version, @config, use_default_config
    )
  end

  def <=>(other)
    return to_string() <=> other.to_string()
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fig-0.1.59 lib/fig/packagedescriptor.rb