Class: Fig::PackageDescriptor

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/fig/package_descriptor.rb

Overview

Parsed representation of a package specification, i.e. “name/version:config”.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, config, options = {}) ⇒ PackageDescriptor

Options are:

:name                           => { :required | :forbidden }
:version                        => { :required | :forbidden }
:config                         => { :required | :forbidden }
:original_string                => the unparsed form
:require_at_least_one_component => should we have at least one of
                                   name, version, and config
:validation_context             => what the descriptor is for
:source_description             => where the descriptor came from,
                                   most likely the result of invoking
                                   Fig::Statement.position_description().


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fig/package_descriptor.rb', line 58

def initialize(name, version, config, options = {})
  @name            = name
  @version         = version
  @config          = config
  @original_string = options[:original_string]

  validate_component name,    'name',    :name,    options
  validate_component version, 'version', :version, options
  validate_component config,  'config',  :config,  options
  validate_name      options
  validate_across_components options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/fig/package_descriptor.rb', line 9

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/fig/package_descriptor.rb', line 9

def name
  @name
end

#original_stringObject (readonly)

Returns the value of attribute original_string.



9
10
11
# File 'lib/fig/package_descriptor.rb', line 9

def original_string
  @original_string
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/fig/package_descriptor.rb', line 9

def version
  @version
end

Class Method Details

.format(name, version, config, use_default_config = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fig/package_descriptor.rb', line 11

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

.parse(raw_string, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fig/package_descriptor.rb', line 29

def self.parse(raw_string, options = {})
  filled_in_options = {}
  filled_in_options.merge!(options)
  filled_in_options[:original_string] = raw_string
  filled_in_options[:require_at_least_one_component] = true

  # 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.
  return self.new(
    raw_string =~ %r< \A         ( [^:/]+ )    >x ? $1 : nil,
    raw_string =~ %r< \A [^/]* / ( [^:]+  )    >x ? $1 : nil,
    raw_string =~ %r< \A [^:]* : ( .+     ) \z >x ? $1 : nil,
    filled_in_options
  )
end

Instance Method Details

#<=>(other) ⇒ Object



78
79
80
# File 'lib/fig/package_descriptor.rb', line 78

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

#to_string(use_default_config = false) ⇒ Object

Specifically not named :to_s because it doesn’t act like that should.



72
73
74
75
76
# File 'lib/fig/package_descriptor.rb', line 72

def to_string(use_default_config = false)
  return Fig::PackageDescriptor.format(
    @name, @version, @config, use_default_config
  )
end