Class: CloudFlock::App::Common::PlatformAction

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflock/app/common/platform_action.rb

Overview

Public: The PlatformAction Class provides the template for actions to be taken dependant upon the platform targeted.

Direct Known Subclasses

Cleanup, Exclusions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cpe) ⇒ PlatformAction

Public: Set internal parameters and initialize an empty Hash to be used as a collection.

cpe - CPE object containing information off of which the platform

parameters will be based.


16
17
18
19
20
21
# File 'lib/cloudflock/app/common/platform_action.rb', line 16

def initialize(cpe)
  classname = self.class.name.downcase.split(/::/).last
  @prefix     = "cloudflock/app/common/#{classname}/"
  @platform   = ['unix', cpe.vendor, cpe.product + cpe.version]
  @collection = {}
end

Instance Attribute Details

#platformObject (readonly)

Public: Gets the Array containing the platform details.



6
7
8
# File 'lib/cloudflock/app/common/platform_action.rb', line 6

def platform
  @platform
end

#prefixObject (readonly)

Public: Gets the path prefix for including any applicable files.



9
10
11
# File 'lib/cloudflock/app/common/platform_action.rb', line 9

def prefix
  @prefix
end

Instance Method Details

#each_platform(&block) ⇒ Object

Public: Applies a block to each item in the platform Array.

Yields each item in platform (per each).



33
34
35
# File 'lib/cloudflock/app/common/platform_action.rb', line 33

def each_platform(&block)
  platform.each(&block)
end

#load_each(&block) ⇒ Object

Public: Loads each available file corresponding to a given platform in order of ascending specificity, then passes the Module to the block passed.

Yields each Module.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudflock/app/common/platform_action.rb', line 42

def load_each(&block)
  platforms = map_platforms { |name| name.gsub(/[^a-zA-Z0-9]/, '_') }

  paths = (0...platforms.length).map { |i| platforms[(0...i)].join('/') }
  mods  = (0...platforms.length).map do |i|
    platforms[(0...i)].map(&:capitalize)
  end

  (0...paths.length).each do |index|
    begin
      require prefix + paths[index]
      block.call(mods[index].reduce(self.class) { |c,e| c.const_get(e) })
    rescue LoadError
    end
  end
end

#map_platforms(&block) ⇒ Object

Public: Map a block to the platform Array.

Yields each item in platform (per map).



26
27
28
# File 'lib/cloudflock/app/common/platform_action.rb', line 26

def map_platforms(&block)
  platform.map(&block)
end