Class: RightScale::MetadataWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/clouds/metadata_writer.rb

Overview

Base implementation for a metadata writer. By default writes a raw metadata format.

Constant Summary collapse

DEFAULT_FILE_MODE =
0640

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MetadataWriter

Initializer

Parameters

options(String)

output file extension

options(String)

output file name sans extension

options(String)

output directory, defaults to RS spool dir

options(Proc(reader, subpath)

read override or nil

options(Proc(writer, metadata subpath)

write override or nil

Return

always true

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
# File 'lib/clouds/metadata_writer.rb', line 27

def initialize(options)
  raise ArgumentError.new("options[:file_name_prefix] is required") unless @file_name_prefix = options[:file_name_prefix]
  raise ArgumentError.new("options[:output_dir_path] is required") unless @output_dir_path = options[:output_dir_path]
  @file_extension = options[:file_extension] || '.raw'
  @read_override = options[:read_override]
  @write_override = options[:write_override]
end

Instance Attribute Details

#file_extensionObject

Returns the value of attribute file_extension.



14
15
16
# File 'lib/clouds/metadata_writer.rb', line 14

def file_extension
  @file_extension
end

#file_name_prefixObject

Returns the value of attribute file_name_prefix.



14
15
16
# File 'lib/clouds/metadata_writer.rb', line 14

def file_name_prefix
  @file_name_prefix
end

#output_dir_pathObject

Returns the value of attribute output_dir_path.



14
15
16
# File 'lib/clouds/metadata_writer.rb', line 14

def output_dir_path
  @output_dir_path
end

Class Method Details

.escape_double_quotes(value) ⇒ Object

Escapes double-quotes (and literal backslashes since they are escape characters) in the given string.



62
63
64
# File 'lib/clouds/metadata_writer.rb', line 62

def self.escape_double_quotes(value)
  return value.gsub(/\\|"/) { |c| "\\#{c}" }
end

.escape_single_quotes(value) ⇒ Object

Escapes single-quotes (and literal backslashes since they are escape characters) in the given string.



68
69
70
# File 'lib/clouds/metadata_writer.rb', line 68

def self.escape_single_quotes(value)
  return value.gsub(/\\|'/) { |c| "\\#{c}" }
end

.first_line_of(value) ⇒ Object

Determines the first line of text (or the only line) for the given value.

Parameters

value(Object)

any value

Return

result(String)

first line or empty



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/clouds/metadata_writer.rb', line 79

def self.first_line_of(value)
  # flatten any value which supports it
  value = value.flatten if value.respond_to?(:flatten)

  # note that the active_support gem redefines String.first from being the
  # same as .lines.first to returning the .first(n=1) characters.
  if value.respond_to?(:lines)
    value = value.lines.first
  elsif value.respond_to?(:first)
    value = value.first
  end
  return value.to_s.strip
end

Instance Method Details

#read(subpath = nil) ⇒ Object

Reads metadata from file.

Parameters

subpath(Array|String)

subpath or nil

Return

result(String)

contents of generated file



42
43
44
45
# File 'lib/clouds/metadata_writer.rb', line 42

def read(subpath = nil)
  return @read_override.call(self, subpath) if @read_override
  return read_file(subpath)
end

#write(metadata, subpath = nil) ⇒ Object

Writes given metadata to file.

Parameters

metadata(Hash)

Hash-like metadata

subpath(Array|String)

subpath if deeper than root or nil

Return

always true



55
56
57
58
# File 'lib/clouds/metadata_writer.rb', line 55

def write(, subpath = nil)
  return @write_override.call(self, , subpath) if @write_override
  return write_file(, subpath)
end