class SnakyHash::Extensions

Manages extensions that can modify values in a chain of transformations

@example Adding and running an extension

extensions = Extensions.new
extensions.add(:upcase) { |value| value.to_s.upcase }
extensions.run("hello") #=> "HELLO"

Public Class Methods

new() click to toggle source

Initializes a new Extensions instance with an empty extensions registry

   # File lib/snaky_hash/extensions.rb
13 def initialize
14   reset
15 end

Public Instance Methods

add(name, &block) click to toggle source

Adds a new extension with the given name

@param name [String, Symbol] the name of the extension @yield [value] block that will be called with a value to transform @raise [SnakyHash::Error] if an extension with the given name already exists @return [Proc] the added extension block

   # File lib/snaky_hash/extensions.rb
30 def add(name, &block)
31   if has?(name)
32     raise Error, "Extension already defined named '#{name}'"
33   end
34 
35   @extensions[name.to_sym] = block
36 end
has?(name) click to toggle source

Checks if an extension with the given name exists

@param name [String, Symbol] the name of the extension to check @return [Boolean] true if the extension exists, false otherwise

   # File lib/snaky_hash/extensions.rb
42 def has?(name)
43   @extensions.key?(name.to_sym)
44 end
reset() click to toggle source

Reset the registry of extensions to an empty state

@return [Hash] an empty hash of extensions

   # File lib/snaky_hash/extensions.rb
20 def reset
21   @extensions = {}
22 end
run(value) click to toggle source

Runs all registered extensions in sequence on the given value

@param value [Object] the value to transform through all extensions @return [Object] the final transformed value after running all extensions

   # File lib/snaky_hash/extensions.rb
50 def run(value)
51   @extensions.each_value do |block|
52     value = block.call(value)
53   end
54   value
55 end