Sha256: ae1088fdcc9541b342eae3617514d7c8696236c9554c4ceede39ab9eee14ae08

Contents?: true

Size: 1.85 KB

Versions: 29

Compression:

Stored size: 1.85 KB

Contents

# == PlatformManager::Struct
#
# A simple PlatformManager driver that queries a hash for results. Although
# not useful on its own, it's inherited by other drivers that provide
# platform-specific methods to query the system.
class AutomateIt::PlatformManager::Struct < AutomateIt::PlatformManager::BaseDriver
  depends_on :nothing

  # Hash mapping of keys that have many common names, e.g., "release" and "version"
  attr_accessor :key_aliases

  def suitability(method, *args) # :nodoc:
    return 1
  end

  # Options:
  # * :struct -- The hash to use for queries.
  def setup(opts={})
    super(opts)

    @@struct_cache ||= {}

    if opts[:struct]
      @struct = opts[:struct]
    else
      @struct ||= {}
    end

    # Generate bi-directional map
    @key_aliases ||= @@key_aliases ||= {
      :version => :release,
    }.inject({}){|s,v| s[v[0]] = v[1]; s[v[1]] = v[0]; s}
  end

  # See PlatformManager#query
  def query(search)
    result = ""
    for key in search.to_s.split(/#/)
      result << "_" unless result.empty?
      result << _query_key(key)
    end
    result
  end

  def _query_key(key)
    key = key.to_sym
    unless @struct.has_key?(key)
      key_alias = key_aliases[key]
      if @struct.has_key?(key_alias)
        key = key_alias
      else
        raise IndexError.new("platform doesn't provide key: #{key}")
      end
    end
    return @struct[key]
  end
  private :_query_key

  # See PlatformManager#single_vendor?
  def single_vendor?
    return false
  end

  # See PlatformManager#tags
  def tags
    results = []
    for key in %w(os arch distro os#arch)
      results << query(key) rescue IndexError
    end

    release_query = \
      if single_vendor?
        "os#release"     # E.g. windows_xp
      else
        "distro#release" # E.g. ubuntu_6.06
      end
    results << query(release_query) rescue IndexError
    return results
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
automate-it-0.9.2 lib/automateit/platform_manager/struct.rb
automate-it-0.9.1 lib/automateit/platform_manager/struct.rb
automate-it-0.9.0 lib/automateit/platform_manager/struct.rb
automateit-0.70930 lib/automateit/platform_manager/struct.rb
automateit-0.71003 lib/automateit/platform_manager/struct.rb
automateit-0.71021 lib/automateit/platform_manager/struct.rb
automateit-0.71030 lib/automateit/platform_manager/struct.rb
automateit-0.71031.1 lib/automateit/platform_manager/struct.rb
automateit-0.71031.2 lib/automateit/platform_manager/struct.rb
automateit-0.71017 lib/automateit/platform_manager/struct.rb
automateit-0.71012 lib/automateit/platform_manager/struct.rb
automateit-0.71006 lib/automateit/platform_manager/struct.rb
automateit-0.71101.2 lib/automateit/platform_manager/struct.rb
automateit-0.71102 lib/automateit/platform_manager/struct.rb
automateit-0.71103 lib/automateit/platform_manager/struct.rb
automateit-0.71031 lib/automateit/platform_manager/struct.rb
automateit-0.71101 lib/automateit/platform_manager/struct.rb
automateit-0.71101.1 lib/automateit/platform_manager/struct.rb
automateit-0.71220 lib/automateit/platform_manager/struct.rb
automateit-0.71221 lib/automateit/platform_manager/struct.rb