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”.

Constant Summary collapse

UNBRACKETED_COMPONENT_PATTERN =
/ (?! [.]{1,2} $) [a-zA-Z0-9_.-]+ /x
COMPONENT_PATTERN =
/ \A #{UNBRACKETED_COMPONENT_PATTERN} \z /x

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
:file_path                      => if this is for a file outside of the
                                   repository and not synthetic, the
                                   source of the package
:description                    => meta-information, if this is for a
                                   synthetic package
: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().


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fig/package_descriptor.rb', line 78

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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/fig/package_descriptor.rb', line 16

def config
  @config
end

#descriptionObject (readonly)

Returns the value of attribute description.



19
20
21
# File 'lib/fig/package_descriptor.rb', line 19

def description
  @description
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



18
19
20
# File 'lib/fig/package_descriptor.rb', line 18

def file_path
  @file_path
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/fig/package_descriptor.rb', line 14

def name
  @name
end

#original_stringObject (readonly)

Returns the value of attribute original_string.



17
18
19
# File 'lib/fig/package_descriptor.rb', line 17

def original_string
  @original_string
end

#versionObject (readonly)

Returns the value of attribute version.



15
16
17
# File 'lib/fig/package_descriptor.rb', line 15

def version
  @version
end

Class Method Details

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fig/package_descriptor.rb', line 21

def self.format(
  name, version, config, use_default_config = false, description = nil
)
  string = name
  if ! string
    string = description ? "<#{description}>" : ''
  end

  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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fig/package_descriptor.rb', line 44

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



103
104
105
# File 'lib/fig/package_descriptor.rb', line 103

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

#to_string(use_default_config = false, use_file_or_description = false) ⇒ Object

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



93
94
95
96
97
98
99
100
101
# File 'lib/fig/package_descriptor.rb', line 93

def to_string(use_default_config = false, use_file_or_description = false)
  return Fig::PackageDescriptor.format(
    @name,
    @version,
    @config,
    use_default_config,
    use_file_or_description ? @file_path ? @file_path : @description : nil
  )
end