Class: HashFilter::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_filter/operation.rb,
lib/hash_filter/operation/delete.rb,
lib/hash_filter/operation/rename.rb

Overview

An abstract operation on a hash during the filter process

Examples:

class SwapKeyValue < HashFilter::Operation
  def execute(hash, key)
    value = hash.delete(key)
    hash[value] = key
  end
end

HashFilter.new do
  operation SwapKeyValue
end

Direct Known Subclasses

Delete, Rename

Defined Under Namespace

Classes: Delete, Rename

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Operation

A new operation

Parameters:

  • key (String, Regexp, Object)


19
20
21
# File 'lib/hash_filter/operation.rb', line 19

def initialize(key)
  @key = key
end

Instance Method Details

#execute(hash, key) ⇒ Object

Execute the defined operation

Parameters:

  • hash (Hash)

    target hash

  • key (String)

    key

Raises:

  • (NotImplementedError)

See Also:



29
30
31
# File 'lib/hash_filter/operation.rb', line 29

def execute(hash, key)
  raise NotImplementedError
end

#matches?(key) ⇒ Boolean

Should this operation be executed?

Parameters:

  • key (Regexp, String, Object)

    key

Returns:

  • (Boolean)


36
37
38
# File 'lib/hash_filter/operation.rb', line 36

def matches?(key)
  @key === key
end