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, file_path, description, runtime_directory, include_file_base_directory, statements, synthetic) ⇒ Package

Returns a new instance of Package.


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

def initialize(
  name,
  version,
  file_path,
  description,
  runtime_directory,
  include_file_base_directory,
  statements,
  synthetic
)
  @name                         = name
  @version                      = version
  @file_path                    = file_path
  @description                  = description
  @runtime_directory            = runtime_directory
  @include_file_base_directory  = include_file_base_directory
  @statements                   = statements
  @synthetic                    = synthetic
  @applied_config_names         = []
  @backtrace                    = nil
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace


32
33
34
# File 'lib/fig/package.rb', line 32

def backtrace
  @backtrace
end

#descriptionObject (readonly)

Returns the value of attribute description


28
29
30
# File 'lib/fig/package.rb', line 28

def description
  @description
end

#file_pathObject (readonly)

Returns the value of attribute file_path


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

def file_path
  @file_path
end

#include_file_base_directoryObject (readonly)

Returns the value of attribute include_file_base_directory


30
31
32
# File 'lib/fig/package.rb', line 30

def include_file_base_directory
  @include_file_base_directory
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#runtime_directoryObject (readonly)

Returns the value of attribute runtime_directory


29
30
31
# File 'lib/fig/package.rb', line 29

def runtime_directory
  @runtime_directory
end

#statementsObject (readonly)

Returns the value of attribute statements


31
32
33
# File 'lib/fig/package.rb', line 31

def statements
  @statements
end

#unparsed_textObject

Returns the value of attribute unparsed_text


33
34
35
# File 'lib/fig/package.rb', line 33

def unparsed_text
  @unparsed_text
end

#versionObject (readonly)

Returns the value of attribute version


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

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object


103
104
105
106
107
108
# File 'lib/fig/package.rb', line 103

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

  return compare_components(version, other.version)
end

#[](config_name) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fig/package.rb', line 75

def [](config_name)
  @statements.each do
    |statement|

    return statement if
          statement.is_a?(Fig::Statement::Configuration) \
      &&  statement.name == config_name
  end

  descriptor = Fig::PackageDescriptor.new(
    @name,
    @version,
    config_name,
    :file_path   => @file_path,
    :description => @description
  )
  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, self)
end

#add_applied_config_name(name) ⇒ Object


138
139
140
# File 'lib/fig/package.rb', line 138

def add_applied_config_name(name)
  @applied_config_names << name
end

#applied_config_namesObject


134
135
136
# File 'lib/fig/package.rb', line 134

def applied_config_names()
  return @applied_config_names.clone
end

#archive_locationsObject


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

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

#base?Boolean

Is this the base package?

Returns:

  • (Boolean)

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

def base?()
  return @base
end

#config_namesObject


114
115
116
# File 'lib/fig/package.rb', line 114

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

#configsObject


110
111
112
# File 'lib/fig/package.rb', line 110

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

#name_or_file_or_descriptionObject


179
180
181
182
183
184
185
186
187
# File 'lib/fig/package.rb', line 179

def name_or_file_or_description
  return @name if @name

  if @file_path
    return "[#{@file_path}]"
  end

  return @description
end

#package_dependencies(config_name, backtrace) ⇒ Object

Returns an array of PackageDescriptors


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fig/package.rb', line 147

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::IncludeFile)
      full_path = statement.full_path_relative_to self

      descriptors << Fig::PackageDescriptor.new(
        nil, nil, nil, :file_path => full_path
      )
    elsif statement.is_a?(Fig::Statement::Override)
      backtrace.add_override(statement)
    end
  end

  return descriptors
end

#primary_config_nameObject


142
143
144
# File 'lib/fig/package.rb', line 142

def primary_config_name()
  return @applied_config_names.first
end

#resource_locationsObject


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

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

#retrievesObject


118
119
120
# File 'lib/fig/package.rb', line 118

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

#set_base(yea_or_nay) ⇒ Object


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

def set_base(yea_or_nay)
  @base = yea_or_nay

  return
end

#synthetic?Boolean

Was this package (supposedly) created from something other than usual parsing? (Note that some tests artificially create “non-synthetic” instances.)

Returns:

  • (Boolean)

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

def synthetic?
  return @synthetic
end

#to_descriptive_string_with_config(config_name) ⇒ Object

Useful for debugging; should not be used for regular output.


203
204
205
206
207
# File 'lib/fig/package.rb', line 203

def to_descriptive_string_with_config(config_name)
  return Fig::PackageDescriptor.format(
    name, version, config_name, :use_default_config, description
  )
end

#to_sObject


189
190
191
192
193
# File 'lib/fig/package.rb', line 189

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

#to_s_with_config(config_name) ⇒ Object


195
196
197
198
199
200
# File 'lib/fig/package.rb', line 195

def to_s_with_config(config_name)
  displayed_config = config_name == DEFAULT_CONFIG ? nil : config_name
  return Fig::PackageDescriptor.format(
    name_or_file_or_description, version, displayed_config
  )
end

#walk_statements(&block) ⇒ Object

Block will receive a Statement.


170
171
172
173
174
175
176
177
# File 'lib/fig/package.rb', line 170

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

  return
end