Class: Arachni::Platform::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/arachni/platform/list.rb

Overview

Represents a collection of applicable platforms.

Author:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid_platforms) ⇒ List

Returns a new instance of List.

Parameters:

  • valid_platforms (Array<String, Symbol>)

    Valid platforms for this list.



21
22
23
24
# File 'lib/arachni/platform/list.rb', line 21

def initialize( valid_platforms )
    @valid_platforms = normalize!( valid_platforms )
    @platforms       = []
end

Class Method Details

.normalize(platforms) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/arachni/platform/list.rb', line 217

def self.normalize( platforms )
    case platforms
        when self.class, Symbol
            platforms
        when String
            platforms.to_sym
        when Hash
            platforms.my_symbolize_keys
        when Enumerable, Array
            platforms.to_a.flatten.map( &:to_sym ).uniq.sort
    end
end

Instance Method Details

#<<(platform) ⇒ Platform

Returns ‘self`.

Parameters:

  • platform (Symbol, String)

    Platform to add to the list.

Returns:

Raises:



132
133
134
135
# File 'lib/arachni/platform/list.rb', line 132

def <<( platform )
    @platforms |= [normalize( platform )]
    self
end

#any?Boolean

Returns ‘true` if there are applicable platforms, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` if there are applicable platforms, `false` otherwise.



121
122
123
# File 'lib/arachni/platform/list.rb', line 121

def any?
    !empty?
end

#clearObject

Clears platforms.



201
202
203
# File 'lib/arachni/platform/list.rb', line 201

def clear
    @platforms.clear
end

#dupPlatform

Returns Copy of ‘self`.

Returns:



207
208
209
# File 'lib/arachni/platform/list.rb', line 207

def dup
    self.class.new( @valid_platforms ).tap { |p| p.platforms = @platforms }
end

#each(&block) ⇒ Enumerator, Platform

Returns ‘Enumerator` if no `block` is given, `self` otherwise.

Parameters:

  • block (Block)

    Block to be passed each platform.

Returns:

  • (Enumerator, Platform)

    ‘Enumerator` if no `block` is given, `self` otherwise.



169
170
171
172
173
# File 'lib/arachni/platform/list.rb', line 169

def each( &block )
    return enum_for( __method__ ) if !block_given?
    @platforms.each( &block )
    self
end

#empty?Boolean

Returns ‘true` if there are no applicable platforms, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` if there are no applicable platforms, `false` otherwise.



115
116
117
# File 'lib/arachni/platform/list.rb', line 115

def empty?
    @platforms.empty?
end

#hierarchical?Boolean

Returns ‘true` if the list has a hierarchy, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` if the list has a hierarchy, `false` otherwise.



213
214
215
# File 'lib/arachni/platform/list.rb', line 213

def hierarchical?
    @valid_platforms.is_a? Hash
end

#include?(platform) ⇒ Boolean

Returns ‘true` if `platform` applies to the given resource, `false` otherwise.

Parameters:

  • platform (Symbol, String)

    Platform to check.

Returns:

  • (Boolean)

    ‘true` if `platform` applies to the given resource, `false` otherwise.

Raises:



183
184
185
# File 'lib/arachni/platform/list.rb', line 183

def include?( platform )
    @platforms.include? normalize( platform )
end

#include_any?(platforms) ⇒ Boolean

Returns ‘true` if any platform in `platforms` applies to the given resource, `false` otherwise.

Parameters:

  • platforms (Array<Symbol, String>)

    Platform to check.

Returns:

  • (Boolean)

    ‘true` if any platform in `platforms` applies to the given resource, `false` otherwise.

Raises:



196
197
198
# File 'lib/arachni/platform/list.rb', line 196

def include_any?( platforms )
    (@platforms & normalize( platforms )).any?
end

#invalid?(platforms) ⇒ Boolean

Returns ‘true` if platforms are invalid (i.e. not in #valid), `false` otherwise.

Parameters:

Returns:

  • (Boolean)

    ‘true` if platforms are invalid (i.e. not in #valid), `false` otherwise.

See Also:



109
110
111
# File 'lib/arachni/platform/list.rb', line 109

def invalid?( platforms )
    !valid?( platforms )
end

#merge(enum) ⇒ Object Also known as: +, |

Parameters:

  • enum (Platform, Enumerable)

    Enumerable object containing platforms. New Arachni::Platform built by merging ‘self` and the elements of the given enumerable object.

Raises:



144
145
146
# File 'lib/arachni/platform/list.rb', line 144

def merge( enum )
    dup.merge!( enum )
end

#merge!(enum) ⇒ Platform Also known as: update

Returns Updated ‘self`.

Parameters:

  • enum (Enumerable)

    Enumerable object containing platforms.

Returns:

Raises:



158
159
160
161
# File 'lib/arachni/platform/list.rb', line 158

def merge!( enum )
    @platforms |= normalize( enum )
    self
end

#pick(data_per_platform) ⇒ Hash

Selects appropriate data depending on the applicable platforms from ‘data_per_platform`.

Parameters:

  • data_per_platform (Hash{<Symbol, String> => Object})

    Hash with platform names as keys and arbitrary data as values.

Returns:

  • (Hash)

    ‘data_per_platform` with non-applicable entries removed.

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/arachni/platform/list.rb', line 43

def pick( data_per_platform )
    orig_data_per_platform = data_per_platform.dup
    data_per_platform      = data_per_platform.dup

    data_per_platform.select! { |k, v| include? k }

    # Bail out if the valid platforms are just a flat array, without hierarchy.
    return data_per_platform if !hierarchical?

    # Keep track of parents which will be removed due to the existence of
    # their children.
    specified_parents = []

    # Remove parents if we have children.
    data_per_platform.keys.each do |platform|
        specified_parents |= parents = find_parents( platform )
        data_per_platform.reject! { |k, _| parents.include? k }
    end

    # Include all of the parents' children if parents are specified but no
    # children for them.

    children = {}
    children_for = valid & @platforms.to_a
    children_for.each do |platform|
        next if specified_parents.include? platform
        c = find_children( platform )
        children.merge! orig_data_per_platform.select { |k, _| c.include? k }
    end

    data_per_platform.merge! children

    # Include the nearest parent data there is a child platform but there
    # are no data for it.

    ignore = data_per_platform.keys | specified_parents
    orig_data_per_platform.each do |platform, data|
        next if ignore.include?( platform ) ||
            !include_any?( find_children( platform ) )
        data_per_platform[platform] = data
    end

    data_per_platform
end

#validArray<Symbol>

Returns Supported platforms.

Returns:

  • (Array<Symbol>)

    Supported platforms.



28
29
30
# File 'lib/arachni/platform/list.rb', line 28

def valid
    hierarchical? ? @valid_platforms.find_symbol_keys_recursively : @valid_platforms
end

#valid?(platforms) ⇒ Boolean

Returns ‘true` if platforms are valid (i.e. in #valid), `false` otherwise.

Parameters:

Returns:

  • (Boolean)

    ‘true` if platforms are valid (i.e. in #valid), `false` otherwise.

See Also:



95
96
97
98
99
100
# File 'lib/arachni/platform/list.rb', line 95

def valid?( platforms )
    normalize( platforms )
    true
rescue
    false
end