Class: HashFilter
- Inherits:
-
Object
- Object
- HashFilter
- Defined in:
- lib/hash_filter.rb,
lib/hash_filter/version.rb,
lib/hash_filter/operation.rb,
lib/hash_filter/operation/delete.rb,
lib/hash_filter/operation/rename.rb
Overview
Easy hash filtering via simple operations
Defined Under Namespace
Classes: Operation
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Method Summary collapse
-
#apply(hash) ⇒ Object
Apply this filter to hash.
-
#delete(key) ⇒ Object
Delete a key.
-
#initialize {|HashFilter| ... } ⇒ HashFilter
constructor
Initialize a hash filter.
-
#inject(filter) ⇒ Object
Combine other filters.
-
#keep(key) ⇒ Object
Keep given key.
-
#operation(class_name, *args) ⇒ Object
Add a custom operation.
-
#rename(from, to) ⇒ Object
Rename a key.
Constructor Details
#initialize {|HashFilter| ... } ⇒ HashFilter
Initialize a hash filter
28 29 30 31 32 |
# File 'lib/hash_filter.rb', line 28 def initialize(&block) @keeps = [] @operations = [] instance_eval(&block) if block end |
Instance Method Details
#apply(hash) ⇒ Object
Apply this filter to hash
86 87 88 |
# File 'lib/hash_filter.rb', line 86 def apply(hash) apply_operations(@operations, hash.dup) end |
#delete(key) ⇒ Object
Delete a key
57 58 59 |
# File 'lib/hash_filter.rb', line 57 def delete(key) operation Operation::Delete, key end |
#inject(filter) ⇒ Object
Combine other filters
64 65 66 67 |
# File 'lib/hash_filter.rb', line 64 def inject(filter) @keeps.concat filter.keeps @operations.concat filter.operations end |
#keep(key) ⇒ Object
Keep given key
79 80 81 |
# File 'lib/hash_filter.rb', line 79 def keep(key) @keeps << key end |
#operation(class_name, *args) ⇒ Object
Add a custom operation
105 106 107 |
# File 'lib/hash_filter.rb', line 105 def operation(class_name, *args) @operations << class_name.new(*args) end |