Class: LaunchDarkly::Impl::FileData::Document Private
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::FileData::Document
- Defined in:
- lib/ldclient-rb/impl/file_data/document.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The parsed form of one data file. A document may contain full flag definitions, flag key to value entries, and segment definitions. Every hash has symbol keys.
Constant Summary collapse
- NONE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The document of YAML content that is only a null, such as an empty file, a comment, or "---": a document with no entries.
new(flags: {}.freeze, flag_values: {}.freeze, segments: {}.freeze).freeze
Instance Attribute Summary collapse
- #flag_values ⇒ Hash{Symbol => Object} readonly private
- #flags ⇒ Hash{Symbol => Hash} readonly private
- #segments ⇒ Hash{Symbol => Hash} readonly private
Class Method Summary collapse
-
.parse(content, format:) ⇒ Document
private
Parses the content of a data file in the given format.
-
.read(path) ⇒ Document
private
Reads and parses one data file.
Instance Method Summary collapse
-
#initialize(flags: {}, flag_values: {}, segments: {}) ⇒ Document
constructor
private
A new instance of Document.
Constructor Details
#initialize(flags: {}, flag_values: {}, segments: {}) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Document.
59 60 61 62 63 |
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 59 def initialize(flags: {}, flag_values: {}, segments: {}) @flags = flags @flag_values = flag_values @segments = segments end |
Instance Attribute Details
#flag_values ⇒ Hash{Symbol => Object} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
49 50 51 |
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 49 def flag_values @flag_values end |
#flags ⇒ Hash{Symbol => Hash} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
46 47 48 |
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 46 def flags @flags end |
#segments ⇒ Hash{Symbol => Hash} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 |
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 52 def segments @segments end |
Class Method Details
.parse(content, format:) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses the content of a data file in the given format. LaunchDarkly::Impl::FileData.format_of chooses the format of a file from its name.
Whether content with no entries is valid depends on the format, like the rest of the content. Content that is empty or only white space is not a JSON document, so it fails to parse. In YAML it is a document with no entries, NONE, and so is a YAML document that is only a null, such as one with only a comment. A document that is not a mapping, or whose "flags", "flagValues", or "segments" member is not a mapping, is an error, and so is content that is not valid UTF-8, in either format.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 86 def self.parse(content, format:) FileData.check_encoding(content) case format when :json # A file that is being rewritten in place can be empty for a moment. Failing to parse # keeps its last good data until it is complete. raise ArgumentError, "the file has no content, which is not a JSON document" if FileData.blank?(content) raw = JSON.parse(content) when :yaml # The YAML parser rejects some white space, such as a tab, that is still no content. return NONE if FileData.blank?(content) raw = YAML.safe_load(content) return NONE if raw.nil? else raise ArgumentError, "unknown file format #{format.inspect}" end raise ArgumentError, "file content must be an object" unless raw.is_a?(Hash) data = FileData.symbolize_keys(raw) Document.new( flags: section(data, :flags), flag_values: section(data, :flagValues), segments: section(data, :segments) ) end |
.read(path) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reads and parses one data file.
121 122 123 124 |
# File 'lib/ldclient-rb/impl/file_data/document.rb', line 121 def self.read(path) content = FileData.read_file(path) FileData.parse_file(path, content) end |