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

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.



28
29
30
31
32
33
34
35
# File 'lib/fig/package.rb', line 28

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.



25
26
27
# File 'lib/fig/package.rb', line 25

def backtrace
  @backtrace
end

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#statementsObject (readonly)

Returns the value of attribute statements.



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

def statements
  @statements
end

#unparsed_textObject

Returns the value of attribute unparsed_text.



26
27
28
# File 'lib/fig/package.rb', line 26

def unparsed_text
  @unparsed_text
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



55
56
57
58
59
60
# File 'lib/fig/package.rb', line 55

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

  return compare_components(version, other.version)
end

#==(other) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/fig/package.rb', line 121

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



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

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



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

def add_applied_config_name(name)
  @applied_config_names << name
end

#applied_config_namesObject



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

def applied_config_names()
  return @applied_config_names.clone
end

#archive_urlsObject



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

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

#config_namesObject



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

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

#configsObject



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

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

#package_dependencies(config_name, backtrace) ⇒ Object

Returns an array of PackageDescriptors



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fig/package.rb', line 95

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



90
91
92
# File 'lib/fig/package.rb', line 90

def primary_config_name()
  return @applied_config_names.first
end

#resource_urlsObject



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

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

#retrievesObject



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

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

#to_sObject



129
130
131
132
133
# File 'lib/fig/package.rb', line 129

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

#to_s_with_config(config_name) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fig/package.rb', line 135

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



151
152
153
# File 'lib/fig/package.rb', line 151

def to_s_with_primary_config()
  return to_s_with_config(primary_config_name)
end

#walk_statements(&block) ⇒ Object

Block will receive a Statement.



112
113
114
115
116
117
118
119
# File 'lib/fig/package.rb', line 112

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

  return
end