Class: RightScale::MetadataWriter
- Defined in:
- lib/clouds/metadata_writer.rb
Overview
Base implementation for a metadata writer. By default writes a raw metadata format.
Direct Known Subclasses
RightScale::MetadataWriters::DictionaryMetadataWriter, RightScale::MetadataWriters::RubyMetadataWriter, RightScale::MetadataWriters::ShellMetadataWriter
Constant Summary collapse
- DEFAULT_FILE_MODE =
0640
Instance Attribute Summary collapse
-
#file_extension ⇒ Object
Returns the value of attribute file_extension.
-
#file_name_prefix ⇒ Object
Returns the value of attribute file_name_prefix.
-
#output_dir_path ⇒ Object
Returns the value of attribute output_dir_path.
Class Method Summary collapse
-
.escape_double_quotes(value) ⇒ Object
Escapes double-quotes (and literal backslashes since they are escape characters) in the given string.
-
.escape_single_quotes(value) ⇒ Object
Escapes single-quotes (and literal backslashes since they are escape characters) in the given string.
-
.first_line_of(value) ⇒ Object
Determines the first line of text (or the only line) for the given value.
Instance Method Summary collapse
-
#initialize(options) ⇒ MetadataWriter
constructor
Initializer.
-
#read(subpath = nil) ⇒ Object
Reads metadata from file.
-
#write(metadata, subpath = nil) ⇒ Object
Writes given metadata to file.
Constructor Details
#initialize(options) ⇒ MetadataWriter
27 28 29 30 31 32 33 |
# File 'lib/clouds/metadata_writer.rb', line 27 def initialize() raise ArgumentError.new("options[:file_name_prefix] is required") unless @file_name_prefix = [:file_name_prefix] raise ArgumentError.new("options[:output_dir_path] is required") unless @output_dir_path = [:output_dir_path] @file_extension = [:file_extension] || '.raw' @read_override = [:read_override] @write_override = [:write_override] end |
Instance Attribute Details
#file_extension ⇒ Object
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_prefix ⇒ Object
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_path ⇒ Object
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 |