Class: HashFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_filter.rb,
lib/hash_filter/version.rb

Defined Under Namespace

Classes: Operation

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ HashFilter

Returns a new instance of HashFilter.



5
6
7
8
9
# File 'lib/hash_filter.rb', line 5

def initialize(&block)
  @keeps      = []
  @operations = []
  instance_eval(&block) if block
end

Instance Method Details

#apply(hash) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hash_filter.rb', line 27

def apply(hash)
  dup = hash.dup
  dup.keys.each do |key|
    next if keep?(key)
    @operations.each do |operation|
      if operation.matches?(key)
        operation.execute(dup, key)
      end
    end
  end
  dup
end

#delete(key) ⇒ Object



15
16
17
# File 'lib/hash_filter.rb', line 15

def delete(key)
  operation Operation::Delete, key
end

#inject(filter) ⇒ Object



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

def inject(filter)
  @operations.concat filter.operations
end

#keep(key) ⇒ Object



23
24
25
# File 'lib/hash_filter.rb', line 23

def keep(key)
  @keeps << key
end

#operation(class_name, *args) ⇒ Object



40
41
42
# File 'lib/hash_filter.rb', line 40

def operation(class_name, *args)
  @operations << class_name.new(*args)
end

#rename(from, to) ⇒ Object



11
12
13
# File 'lib/hash_filter.rb', line 11

def rename(from, to)
  operation Operation::Rename, from, to
end