Module: Dradis::Plugins::Mappings::Base::ClassMethods

Defined in:
lib/dradis/plugins/mappings/base.rb

Instance Method Summary collapse

Instance Method Details

#componentObject



17
18
19
# File 'lib/dradis/plugins/mappings/base.rb', line 17

def component
  meta[:name].to_s
end

#default_mapping(source) ⇒ Object



35
36
37
38
39
# File 'lib/dradis/plugins/mappings/base.rb', line 35

def default_mapping(source)
  if mapping_sources.include?(source.to_sym)
    self::Mapping::DEFAULT_MAPPING[source.to_sym]
  end
end

#default_mapping_fields(source) ⇒ Object



11
12
13
14
15
# File 'lib/dradis/plugins/mappings/base.rb', line 11

def default_mapping_fields(source)
  default_mapping(source).map do |destination_field, content|
    MappingField.new(destination_field: destination_field, content: content)
  end
end

#field_names(source:, destination: nil, field_type: 'destination') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dradis/plugins/mappings/base.rb', line 21

def field_names(source:, destination: nil, field_type: 'destination')
  mappings = mappings(source: source, destination: destination)

  mapping_fields = if mappings.any?
    mappings.map(&:mapping_fields).flatten
  end

  if mapping_fields && mapping_fields.any?
    mapping_fields.pluck("#{field_type}_field").uniq
  else
    default_mapping(source).keys
  end
end

#get_mapping(source:, destination:) ⇒ Object

returns single matching mapping given source & destination or default



53
54
55
56
57
58
59
# File 'lib/dradis/plugins/mappings/base.rb', line 53

def get_mapping(source:, destination:)
  mapping = Mapping.includes(:mapping_fields).find_by(
    component: component,
    source: source,
    destination: destination
  )
end

#mapping_fields(source:, destination:) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/dradis/plugins/mappings/base.rb', line 61

def mapping_fields(source:, destination:)
  mapping = get_mapping(source: source, destination: destination)

  if mapping
    mapping.mapping_fields
  else
    default_mapping_fields(source)
  end
end

#mapping_sourcesObject



71
72
73
# File 'lib/dradis/plugins/mappings/base.rb', line 71

def mapping_sources
  self::Mapping::SOURCE_FIELDS.keys
end

#mappings(source: nil, destination: nil) ⇒ Object

given the params returns all matching mappings will accept source and/or destination or no args



43
44
45
46
47
48
49
50
# File 'lib/dradis/plugins/mappings/base.rb', line 43

def mappings(source: nil, destination: nil)
  mappings = Mapping.includes(:mapping_fields).where(
    component: component
  )
  mappings = mappings.where(source: source) if source
  mappings = mappings.where(destination: destination) if destination
  mappings
end

#source_fields(source) ⇒ Object



75
76
77
78
79
# File 'lib/dradis/plugins/mappings/base.rb', line 75

def source_fields(source)
  if mapping_sources.include?(source.to_sym)
    self::Mapping::SOURCE_FIELDS[source.to_sym]
  end
end