Class: ChefLicensing::LicenseFile::Base
- Inherits:
-
Object
- Object
- ChefLicensing::LicenseFile::Base
- Defined in:
- lib/chef-licensing/license_key_fetcher/license_file/base.rb
Constant Summary collapse
- EXPECTED_STRUCTURE =
{ file_format_version: "0.0.0", licenses: [ { license_key: String, license_type: Symbol, update_time: String, }, ], }.freeze
Class Method Summary collapse
-
.load_primary_structure ⇒ Hash
The primary structure of the license file, without nested structures.
-
.load_structure ⇒ Hash
The complete structure of the license file, including nested structures.
-
.migrate_structure(contents, version) ⇒ Hash
The contents of the license file after migration.
-
.verify_structure(data, expected_structure = self::EXPECTED_STRUCTURE) ⇒ Boolean
True if the data matches the expected structure, false otherwise.
Class Method Details
.load_primary_structure ⇒ Hash
Returns The primary structure of the license file, without nested structures.
44 45 46 47 48 |
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 44 def self.load_primary_structure expected_structure_dup = self::EXPECTED_STRUCTURE.dup expected_structure_dup[:licenses] = [] expected_structure_dup end |
.load_structure ⇒ Hash
Returns The complete structure of the license file, including nested structures.
51 52 53 |
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 51 def self.load_structure self::EXPECTED_STRUCTURE end |
.migrate_structure(contents, version) ⇒ Hash
Returns The contents of the license file after migration.
58 59 60 |
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 58 def self.migrate_structure(contents, version) raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'" end |
.verify_structure(data, expected_structure = self::EXPECTED_STRUCTURE) ⇒ Boolean
Note:
This method ignores extra keys in the data that are not in the expected structure
Returns true if the data matches the expected structure, false otherwise.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 19 def self.verify_structure(data, expected_structure = self::EXPECTED_STRUCTURE) return false unless data.is_a?(Hash) expected_structure.each do |key, value| return false unless data.key?(key) if value.is_a?(Hash) return false unless verify_structure(data[key], value) elsif value.is_a?(Array) return false unless data[key].is_a?(Array) data[key].each do |item| return false unless verify_structure(item, value[0]) end elsif value.is_a?(Class) return false unless data[key].is_a?(value) else return false unless data[key] == value end end true end |