Class: DataAnon::Strategy::MongoDB::AnonymizeField

Inherits:
Object
  • Object
show all
Defined in:
lib/strategy/mongodb/anonymize_field.rb

Instance Method Summary collapse

Constructor Details

#initialize(field, field_strategy, anonymization_strategy) ⇒ AnonymizeField

Returns a new instance of AnonymizeField.


6
7
8
9
10
# File 'lib/strategy/mongodb/anonymize_field.rb', line 6

def initialize field, field_strategy, anonymization_strategy
  @field = field
  @field_strategy = field_strategy
  @anonymization_strategy = anonymization_strategy
end

Instance Method Details

#anonymizeObject


12
13
14
15
16
17
18
19
20
# File 'lib/strategy/mongodb/anonymize_field.rb', line 12

def anonymize
  if sub_document?
    @anonymization_strategy.anonymize_document(@field.value, @field.row_number, @field_strategy)
  elsif sub_documents?
    anonymize_sub_documents
  else
    anonymize_field
  end
end

#anonymize_fieldObject


26
27
28
29
30
# File 'lib/strategy/mongodb/anonymize_field.rb', line 26

def anonymize_field
  @field_strategy = @field_strategy || @anonymization_strategy.default_strategy(@field.name)
  raise "Improper fields strategy defined for '#{@field.name}' within document \n #{@field.ar_record}" unless @field_strategy.respond_to?(:anonymize)
  @field_strategy.anonymize(@field)
end

#anonymize_sub_documentsObject


22
23
24
# File 'lib/strategy/mongodb/anonymize_field.rb', line 22

def anonymize_sub_documents
  @field.value.collect { |value| @anonymization_strategy.anonymize_document(value, @field.row_number, @field_strategy) }
end

#sub_document?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/strategy/mongodb/anonymize_field.rb', line 36

def sub_document?
  @field.value.kind_of? Hash
end

#sub_documents?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/strategy/mongodb/anonymize_field.rb', line 32

def sub_documents?
  @field.value.kind_of?(Array) && @field.value[0].kind_of?(Hash)
end