Class: BetterSeo::Configuration::NestedConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/better_seo/configuration.rb

Overview

Nested configuration object

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ NestedConfiguration

Returns a new instance of NestedConfiguration.



240
241
242
243
244
245
246
247
# File 'lib/better_seo/configuration.rb', line 240

def initialize(hash = {})
  @data = hash.with_indifferent_access
  # Wrap nested hashes in NestedConfiguration objects for deep setter support
  @data.each do |key, value|
    @data[key] = NestedConfiguration.new(value) if value.is_a?(Hash) && !value.is_a?(NestedConfiguration)
  end
  define_accessors!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/better_seo/configuration.rb', line 273

def method_missing(method_name, *args, &block)
  method_str = method_name.to_s

  if method_str.end_with?("=")
    key = method_str.chomp("=").to_sym
    value = args.first
    @data[key] = value.is_a?(Hash) ? NestedConfiguration.new(value) : value
    define_accessor(key)
  elsif @data.key?(method_name)
    @data[method_name]
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



264
265
266
# File 'lib/better_seo/configuration.rb', line 264

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



268
269
270
271
# File 'lib/better_seo/configuration.rb', line 268

def []=(key, value)
  @data[key] = value.is_a?(Hash) ? NestedConfiguration.new(value) : value
  define_accessor(key)
end

#merge!(other_hash) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/better_seo/configuration.rb', line 249

def merge!(other_hash)
  @data.deep_merge!(other_hash.with_indifferent_access)
  # Wrap any new nested hashes in NestedConfiguration objects
  @data.each do |key, value|
    @data[key] = NestedConfiguration.new(value) if value.is_a?(Hash) && !value.is_a?(NestedConfiguration)
  end
  define_accessors!
  self
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/better_seo/configuration.rb', line 288

def respond_to_missing?(method_name, include_private = false)
  @data.key?(method_name.to_s.chomp("=").to_sym) || super
end

#to_hObject



259
260
261
262
# File 'lib/better_seo/configuration.rb', line 259

def to_h
  # Recursively convert NestedConfiguration objects back to plain hashes
  convert_to_hash(@data)
end