Module: Fig::Deparser

Included in:
V0, V1, V2
Defined in:
lib/fig/deparser.rb,
lib/fig/deparser/v0.rb,
lib/fig/deparser/v1.rb,
lib/fig/deparser/v2.rb,
lib/fig/deparser/v1_base.rb

Defined Under Namespace

Modules: V1Base Classes: V0, V1, V2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_for_statements(statements, emit_as_input_or_to_be_published_values) ⇒ Object

Determine the class of Deparser necessary for a set of Statements; the parameter can be a single statement or multiple. Returns both the class and a list of explanations of why the class was picked.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fig/deparser.rb', line 12

def self.class_for_statements(
  statements, emit_as_input_or_to_be_published_values
)
  statements = [statements].flatten

  versions =
    self.gather_versions statements, emit_as_input_or_to_be_published_values
  version = (versions.map {|version_info| version_info[0]}).max || 0
  explanations = (versions.collect {|v| v[1]}).reject {|e| e.nil?}

  case version
  when 0
    return Fig::Deparser::V0, explanations
  when 1
    return Fig::Deparser::V1, explanations
  when 2
    return Fig::Deparser::V2, explanations
  end

  raise "Unexpected version #{version}."
end

.determine_version_and_deparse(statements, emit_as_input_or_to_be_published_values) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/fig/deparser.rb', line 34

def self.determine_version_and_deparse(
  statements, emit_as_input_or_to_be_published_values
)
  deparser_class, explanations = self.class_for_statements(
    statements, emit_as_input_or_to_be_published_values
  )
  deparser = deparser_class.new emit_as_input_or_to_be_published_values

  return (deparser.deparse [statements].flatten), explanations
end

Instance Method Details

#archive(statement) ⇒ Object



114
115
116
117
118
# File 'lib/fig/deparser.rb', line 114

def archive(statement)
  asset 'archive', statement

  return
end

#command(statement) ⇒ Object



120
121
122
# File 'lib/fig/deparser.rb', line 120

def command(statement)
  raise NotImplementedError.new self
end

#configuration(configuration_statement) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fig/deparser.rb', line 124

def configuration(configuration_statement)
  if ! @text.empty?
    @text << "\n"
  end

  add_indent
  @text << 'config '
  @text << configuration_statement.name
  @text << "\n"

  @indent_level += 1
  begin
    configuration_statement.statements.each do
      |statement|

      statement.deparse_as_version(self)
    end
  ensure
    @indent_level -= 1
  end

  add_indent
  @text << "end\n"

  return
end

#deparse(statements) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fig/deparser.rb', line 99

def deparse(statements)
  # It's double dispatch time!

  @text         = ''
  @indent_level = @initial_indent_level

  statements.each { |statement| statement.deparse_as_version(self) }

  text          = @text
  @text         = nil
  @indent_level = nil

  return text
end

#grammar_descriptionObject



211
212
213
# File 'lib/fig/deparser.rb', line 211

def grammar_description
  raise NotImplementedError.new self
end

#grammar_version(statement) ⇒ Object



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

def grammar_version(statement)
  raise NotImplementedError.new self
end

#include(statement) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fig/deparser.rb', line 155

def include(statement)
  add_indent

  @text << 'include '
  @text << Fig::PackageDescriptor.format(
    statement.package_name, statement.version, statement.config_name
  )
  @text << "\n"

  return
end

#include_file(statement) ⇒ Object



167
168
169
# File 'lib/fig/deparser.rb', line 167

def include_file(statement)
  raise NotImplementedError.new self
end

#override(statement) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fig/deparser.rb', line 171

def override(statement)
  add_indent

  @text << 'override '
  @text << Fig::PackageDescriptor.format(
    statement.package_name, statement.version, nil
  )
  @text << "\n"

  return
end

#path(statement) ⇒ Object



183
184
185
186
187
# File 'lib/fig/deparser.rb', line 183

def path(statement)
  environment_variable(statement, 'add')

  return
end

#resource(statement) ⇒ Object



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

def resource(statement)
  asset 'resource', statement

  return
end

#retrieve(statement) ⇒ Object



195
196
197
# File 'lib/fig/deparser.rb', line 195

def retrieve(statement)
  raise NotImplementedError.new self
end

#set(statement) ⇒ Object



199
200
201
202
203
# File 'lib/fig/deparser.rb', line 199

def set(statement)
  environment_variable(statement, 'set')

  return
end

#synthetic_raw_text(statement) ⇒ Object



205
206
207
208
209
# File 'lib/fig/deparser.rb', line 205

def synthetic_raw_text(statement)
  @text << statement.text

  return
end