Class: Fig::Package

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

Overview

The parsed representation of a configuration file for a specific version. Contains the statement objects.

Unique identifier for this object: name and version. A different version of the same package will be a separate instance of this class.

Constant Summary collapse

UNPUBLISHED =
'<unpublished>'
DEFAULT_CONFIG =
'default'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, directory, statements) ⇒ Package

Returns a new instance of Package.



25
26
27
28
29
30
31
32
# File 'lib/fig/package.rb', line 25

def initialize(name, version, directory, statements)
  @name = name
  @version = version
  @directory = directory
  @statements = statements
  @applied_config_names = []
  @backtrace = nil
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



23
24
25
# File 'lib/fig/package.rb', line 23

def backtrace
  @backtrace
end

#directoryObject (readonly)

Returns the value of attribute directory.



22
23
24
# File 'lib/fig/package.rb', line 22

def directory
  @directory
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/fig/package.rb', line 22

def name
  @name
end

#statementsObject (readonly)

Returns the value of attribute statements.



22
23
24
# File 'lib/fig/package.rb', line 22

def statements
  @statements
end

#versionObject (readonly)

Returns the value of attribute version.



22
23
24
# File 'lib/fig/package.rb', line 22

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object



52
53
54
55
56
57
# File 'lib/fig/package.rb', line 52

def <=>(other)
  compared = compare_components(name, other.name)
  return compared if compared != 0

  return compare_components(version, other.version)
end

#==(other) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/fig/package.rb', line 118

def ==(other)
  return false if other.nil?

  return @name == other.name &&
         @version == other.version &&
         @statements.to_yaml == other.statements.to_yaml
end

#[](config_name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fig/package.rb', line 34

def [](config_name)
  @statements.each do |stmt|
    return stmt if stmt.is_a?(Fig::Statement::Configuration) && stmt.name == config_name
  end

  descriptor = Fig::PackageDescriptor.new(@name, @version, config_name)
  config_description = nil
  if @name.nil? and @version.nil?
    config_description = config_name
  else
    config_description = descriptor.to_string(:use_default_config)
  end

  message = %Q<There is no "#{config_description}" config.>

  raise Fig::NoSuchPackageConfigError.new(message, descriptor)
end

#add_applied_config_name(name) ⇒ Object



83
84
85
# File 'lib/fig/package.rb', line 83

def add_applied_config_name(name)
  @applied_config_names << name
end

#applied_config_namesObject



79
80
81
# File 'lib/fig/package.rb', line 79

def applied_config_names()
  return @applied_config_names.clone
end

#archive_urlsObject



71
72
73
# File 'lib/fig/package.rb', line 71

def archive_urls
  return @statements.select{|s| s.is_a?(Fig::Statement::Archive)}.map{|s| s.url}
end

#config_namesObject



63
64
65
# File 'lib/fig/package.rb', line 63

def config_names
  return configs.collect { |statement| statement.name }
end

#configsObject



59
60
61
# File 'lib/fig/package.rb', line 59

def configs
  return @statements.select { |statement| statement.is_a?(Fig::Statement::Configuration) }
end

#package_dependencies(config_name, backtrace) ⇒ Object

Returns an array of PackageDescriptors



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fig/package.rb', line 92

def package_dependencies(config_name, backtrace)
  descriptors = []

  self[config_name || DEFAULT_CONFIG].walk_statements do
    |statement|

    if statement.is_a?(Fig::Statement::Include)
      descriptors << statement.resolved_dependency_descriptor(self, backtrace)
    elsif statement.is_a?(Fig::Statement::Override)
      backtrace.add_override(statement)
    end
  end

  return descriptors
end

#primary_config_nameObject



87
88
89
# File 'lib/fig/package.rb', line 87

def primary_config_name()
  return @applied_config_names.first
end

#resource_urlsObject



75
76
77
# File 'lib/fig/package.rb', line 75

def resource_urls
  return @statements.select{|s| s.is_a?(Fig::Statement::Resource)}.map{|s|s.url}
end

#retrievesObject



67
68
69
# File 'lib/fig/package.rb', line 67

def retrieves
  return @statements.select { |statement| statement.is_a?(Fig::Statement::Retrieve) }
end

#to_sObject



126
127
128
129
130
# File 'lib/fig/package.rb', line 126

def to_s
  name    = @name || '<empty>'
  version = @version || '<empty>'
  return Fig::PackageDescriptor.format(name, version, nil)
end

#to_s_with_config(config_name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fig/package.rb', line 132

def to_s_with_config(config_name)
  string = nil

  if name.nil?
    string = UNPUBLISHED
  else
    string = to_s
  end

  if not config_name.nil? and config_name != DEFAULT_CONFIG
    string += ":#{config_name}"
  end

  return string
end

#to_s_with_primary_configObject



148
149
150
# File 'lib/fig/package.rb', line 148

def to_s_with_primary_config()
  return to_s_with_config(primary_config_name)
end

#walk_statements(&block) ⇒ Object

Block will receive a Statement.



109
110
111
112
113
114
115
116
# File 'lib/fig/package.rb', line 109

def walk_statements(&block)
  @statements.each do |statement|
    yield statement
    statement.walk_statements &block
  end

  return
end