Class: ThemeTranslationParser
- Inherits:
-
Object
- Object
- ThemeTranslationParser
- Defined in:
- lib/theme_translation_parser.rb
Defined Under Namespace
Classes: InvalidYaml
Constant Summary collapse
- INTERNAL_KEYS =
[:theme_metadata]
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(setting_field, internal: false) ⇒ ThemeTranslationParser
constructor
A new instance of ThemeTranslationParser.
- #load ⇒ Object
Constructor Details
#initialize(setting_field, internal: false) ⇒ ThemeTranslationParser
Returns a new instance of ThemeTranslationParser.
8 9 10 11 |
# File 'lib/theme_translation_parser.rb', line 8 def initialize(setting_field, internal: false) @setting_field = setting_field @internal = internal end |
Class Method Details
.check_contains_hashes(hash) ⇒ Object
13 14 15 16 17 |
# File 'lib/theme_translation_parser.rb', line 13 def self.check_contains_hashes(hash) hash.all? do |_key, value| value.is_a?(String) || (value.is_a?(Hash) && self.check_contains_hashes(value)) end end |
Instance Method Details
#load ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/theme_translation_parser.rb', line 19 def load return {} if @setting_field.value.blank? begin parsed = YAML.safe_load(@setting_field.value) rescue Psych::SyntaxError, Psych::DisallowedClass => e raise InvalidYaml.new(e.) end raise InvalidYaml.new(I18n.t("themes.locale_errors.invalid_yaml")) if !parsed.is_a?(Hash) if parsed.keys.length != 1 || parsed.keys.first != @setting_field.name raise InvalidYaml.new(I18n.t("themes.locale_errors.top_level_locale")) end key = @setting_field.name.to_sym parsed.deep_symbolize_keys! parsed[key] ||= {} if !ThemeTranslationParser.check_contains_hashes(parsed) raise InvalidYaml.new(I18n.t("themes.locale_errors.invalid_yaml")) end parsed[key].slice!(*INTERNAL_KEYS) if @internal parsed[key].except!(*INTERNAL_KEYS) if !@internal parsed end |