Module: Classy::Yaml

Defined in:
lib/classy/yaml.rb,
lib/classy/yaml/engine.rb,
lib/classy/yaml/helpers.rb,
lib/classy/yaml/version.rb,
lib/classy/yaml/tag_helper.rb,
lib/classy/yaml/icon_helper.rb,
lib/classy/yaml/component_helpers.rb,
lib/classy/yaml/invalid_key_error.rb

Defined Under Namespace

Modules: ComponentHelpers, Helpers, IconHelper, TagHelper Classes: Engine, InvalidKeyError

Constant Summary collapse

VERSION =
"1.7.0"
@@default_file =
"config/utility_classes.yml"
@@engine_files =
[]
@@extra_files =
[]
@@override_tag_helpers =
false

Class Method Summary collapse

Class Method Details

.apply_icon_helper_overrideObject



100
101
102
103
104
105
106
# File 'lib/classy/yaml.rb', line 100

def self.apply_icon_helper_override
  return unless defined?(RailsIcons::Helpers::IconHelper)

  require "classy/yaml/icon_helper"

  RailsIcons::Helpers::IconHelper.prepend(Classy::Yaml::IconHelper)
end

.apply_tag_helper_overrideObject



90
91
92
93
94
95
96
97
98
# File 'lib/classy/yaml.rb', line 90

def self.apply_tag_helper_override
  require "classy/yaml/tag_helper"

  ActiveSupport.on_load(:action_view) do
    ActionView::Helpers::TagHelper::TagBuilder.prepend(Classy::Yaml::TagHelper)
  end

  apply_icon_helper_override
end

.cached_default_yamlObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/classy/yaml.rb', line 65

def self.cached_default_yaml
  # Bypass cache in development and test environments
  return load_default_yaml if Rails.env.development? || Rails.env.test?

  # Use cache in other environments
  return @cached_default_yaml if @cached_default_yaml

  @load_lock.synchronize do
    return @cached_default_yaml if @cached_default_yaml
    @cached_default_yaml = load_default_yaml
  end
end

.cached_engine_yamlsObject

-- Cached Data Accessors (Lazy Loading) --



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/classy/yaml.rb', line 51

def self.cached_engine_yamls
  # Bypass cache in development and test environments
  return load_engine_yamls if Rails.env.development? || Rails.env.test?

  # Use cache in other environments
  return @cached_engine_yamls if @cached_engine_yamls

  @load_lock.synchronize do
    # Double-check idiom to ensure loading happens only once
    return @cached_engine_yamls if @cached_engine_yamls
    @cached_engine_yamls = load_engine_yamls
  end
end

.default_file=(value) ⇒ Object



40
41
42
43
# File 'lib/classy/yaml.rb', line 40

def self.default_file=(value)
  @@default_file = value
  @cached_default_yaml = nil # Clear cache on reassignment
end

.engine_files=(value) ⇒ Object

-- Configuration Setters with Path Resolution --



30
31
32
33
# File 'lib/classy/yaml.rb', line 30

def self.engine_files=(value)
  @@engine_files = Array.wrap(value).reject(&:blank?).map { |file| Rails.root.join(file) }
  @cached_engine_yamls = nil # Clear cache on reassignment
end

.extra_files=(value) ⇒ Object



35
36
37
38
# File 'lib/classy/yaml.rb', line 35

def self.extra_files=(value)
  @@extra_files = Array.wrap(value).reject(&:blank?).map { |file| Rails.root.join(file) }
  # Note: extra_files are not cached globally by default
end

.load_default_yamlObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/classy/yaml.rb', line 126

def self.load_default_yaml
  default_path = Rails.root.join(self.default_file)
  begin
    if File.exist?(default_path)
      content = File.read(default_path, encoding: "UTF-8")
      parsed_yaml = YAML.safe_load(content, permitted_classes: [ Symbol, String, Array, Hash ], aliases: true)
      return parsed_yaml if parsed_yaml && parsed_yaml.is_a?(Hash)
    end
  rescue Psych::SyntaxError => e
    Rails.logger.error "Classy::Yaml: Failed to parse default YAML file #{default_path}: #{e.message}"
  rescue => e
    Rails.logger.error "Classy::Yaml: Error loading default YAML file #{default_path}: #{e.message}"
  end
  nil # Return nil if file doesn't exist or fails to load/parse
end

.load_engine_yamlsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/classy/yaml.rb', line 108

def self.load_engine_yamls
  yamls = []
  self.engine_files.each do |file_path|
    begin
      if File.exist?(file_path)
        content = File.read(file_path, encoding: "UTF-8")
        parsed_yaml = YAML.safe_load(content, permitted_classes: [ Symbol, String, Array, Hash ], aliases: true)
        yamls << parsed_yaml if parsed_yaml && parsed_yaml.is_a?(Hash)
      end
    rescue Psych::SyntaxError => e
      Rails.logger.error "Classy::Yaml: Failed to parse engine YAML file #{file_path}: #{e.message}"
    rescue => e
      Rails.logger.error "Classy::Yaml: Error loading engine YAML file #{file_path}: #{e.message}"
    end
  end
  yamls
end

.override_tag_helpers=(value) ⇒ Object



45
46
47
48
# File 'lib/classy/yaml.rb', line 45

def self.override_tag_helpers=(value)
  @@override_tag_helpers = value
  apply_tag_helper_override if value
end

.setup {|_self| ... } ⇒ Object

-- Setup Method --

Yields:

  • (_self)

Yield Parameters:

  • _self (Classy::Yaml)

    the object that the method was called on



79
80
81
82
83
84
85
86
# File 'lib/classy/yaml.rb', line 79

def self.setup
  yield self
  # Clear all caches when configuration changes
  @cached_engine_yamls = nil
  @cached_default_yaml = nil
  # Apply tag helper override if enabled
  apply_tag_helper_override if @@override_tag_helpers
end