module Fig::Deparser

Public Class Methods

class_for_statements( statements, emit_as_input_or_to_be_published_values ) click to toggle source

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.

# 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 ) click to toggle source
# 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

Private Class Methods

expand_version_and_explanation(statement, version_info) click to toggle source
# File lib/fig/deparser.rb, line 84
def self.expand_version_and_explanation(statement, version_info)
  version, explanation = *version_info
  if explanation.nil?
    return [version]
  end

  return [
    version,
    "Grammar v#{version} is required because the #{statement.statement_type} statement#{statement.position_string} #{explanation}."
  ]
end
gather_all_statements(statements) click to toggle source
# File lib/fig/deparser.rb, line 69
def self.gather_all_statements(statements)
  all_statements = []
  statements.each do
    |statement|

    all_statements << statement

    if statement.is_a? Fig::Statement::Configuration
      all_statements << statement.statements
    end
  end

  return all_statements.flatten
end
gather_versions(statements, emit_as_input_or_to_be_published_values) click to toggle source
# File lib/fig/deparser.rb, line 47
def self.gather_versions(statements, emit_as_input_or_to_be_published_values)
  all_statements = gather_all_statements statements

  if emit_as_input_or_to_be_published_values == :emit_as_input
    return all_statements.map {
      |statement|

      self.expand_version_and_explanation(
        statement, statement.minimum_grammar_for_emitting_input
      )
    }
  end

  return all_statements.map {
    |statement|

    self.expand_version_and_explanation(
      statement, statement.minimum_grammar_for_publishing
    )
  }
end

Public Instance Methods

archive(statement) click to toggle source
# File lib/fig/deparser.rb, line 114
def archive(statement)
  asset 'archive', statement

  return
end
command(statement) click to toggle source
# File lib/fig/deparser.rb, line 120
def command(statement)
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end
configuration(configuration_statement) click to toggle source
# File lib/fig/deparser.rb, line 126
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) click to toggle source
# 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_description() click to toggle source
# File lib/fig/deparser.rb, line 219
def grammar_description
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end
grammar_version(statement) click to toggle source
# File lib/fig/deparser.rb, line 153
def grammar_version(statement)
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end
include(statement) click to toggle source
# File lib/fig/deparser.rb, line 159
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) click to toggle source
# File lib/fig/deparser.rb, line 171
def include_file(statement)
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end
override(statement) click to toggle source
# File lib/fig/deparser.rb, line 177
def override(statement)
  add_indent

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

  return
end
path(statement) click to toggle source
# File lib/fig/deparser.rb, line 189
def path(statement)
  environment_variable(statement, 'add')

  return
end
resource(statement) click to toggle source
# File lib/fig/deparser.rb, line 195
def resource(statement)
  asset 'resource', statement

  return
end
retrieve(statement) click to toggle source
# File lib/fig/deparser.rb, line 201
def retrieve(statement)
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end
set(statement) click to toggle source
# File lib/fig/deparser.rb, line 207
def set(statement)
  environment_variable(statement, 'set')

  return
end
synthetic_raw_text(statement) click to toggle source
# File lib/fig/deparser.rb, line 213
def synthetic_raw_text(statement)
  @text << statement.text

  return
end

Private Instance Methods

add_indent(indent_level = @indent_level) click to toggle source
# File lib/fig/deparser.rb, line 247
def add_indent(indent_level = @indent_level)
  @text << @indent_string * indent_level

  return
end
asset(keyword, statement) click to toggle source
# File lib/fig/deparser.rb, line 227
def asset(keyword, statement)
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end
asset_path(statement) click to toggle source
# File lib/fig/deparser.rb, line 233
def asset_path(statement)
  if @emit_as_input_or_to_be_published_values == :emit_as_input
    return statement.location
  end

  return statement.asset_name
end
environment_variable(statement, keyword) click to toggle source
# File lib/fig/deparser.rb, line 241
def environment_variable(statement, keyword)
  raise NotImplementedError.new(
    "#{__callee__}() not implemented on #{self.class}."
  )
end