Class: Y2Packager::Resolvable

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/packages/src/lib/y2packager/resolvable.rb

Overview

Note:

The returned Resolvables might not be valid anymore after changing the package manager status (installing/removing packages, changing repositories, etc.). After such a change you need to load the resolvables again, avoid storing them for later if possible.

This class represents a libzypp resolvable object (package, pattern, patch, product, source package)

Examples:

All installed packages

Y2Packager::Resolvable.find(kind: :package, status: :installed)

Available (not installed) "yast2" packages

Y2Packager::Resolvable.find(kind: :package, status: :available, name: "yast2")

Lazy loading

res = Y2Packager::Resolvable.find(kind: :package, status: :installed)
# the `summary` attribute is loaded from libzypp when needed
res.each {|r| puts "#{r.name} - {r.summary}"}

Preloading the attributes

# the `summary` attribute is loaded from libzypp already at the initial state
res = Y2Packager::Resolvable.find(kind: :package, status: :installed, [:summary])
# this returns the cached `summary` attribute, this is much more efficient
res.each {|r| puts "#{r.name} - {r.summary}"}

Since:

  • 4.2.6

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Resolvable

Constructor, initialize the object from a pkg-bindings resolvable hash.

Parameters:

  • hash (Hash<Symbol,Object>)

    A pkg-bindings resolvable hash.

Since:

  • 4.2.6


79
80
81
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 79

def initialize(hash)
  from_hash(hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Dynamically load the missing attributes from libzypp.

Parameters:

  • method (Symbol)

    the method called

  • args

    not used so far, raises ArgumentError if anything is passed

Returns:

  • the loaded value from libzypp

Raises:

  • (ArgumentError)

Since:

  • 4.2.6


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 103

def method_missing(method, *args)
  if instance_variable_defined?("@#{method}")
    raise ArgumentError, "Method #{method} does not accept arguments" unless args.empty?

    return instance_variable_get("@#{method}")
  end

  # load a missing attribute
  raise "Missing attributes for identifying the resolvable." if !UNIQUE_ATTRIBUTES.all? { |a| instance_variable_defined?("@#{a}") }

  load_attribute(method)
  super unless instance_variable_defined?("@#{method}")
  raise ArgumentError, "Method #{method} does not accept arguments" unless args.empty?

  instance_variable_get("@#{method}")
end

Class Method Details

.any?(params) ⇒ Boolean

Is there any resolvable matching the requested parameters? This is similar to the .find method, just instead of a resolvable list it returns a simple Boolean.

Parameters:

  • params (Hash<Symbol,Object>)

    The requested attributes

Returns:

  • (Boolean)

    true if any matching resolvable is found, false otherwise.

See Also:

Since:

  • 4.2.6


71
72
73
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 71

def self.any?(params)
  Yast::Pkg.AnyResolvable(params)
end

.find(params, preload = []) ⇒ Array<Y2Packager::Resolvable>

Find the resolvables which match the input parameters. See Yast::Pkg.Resolvables

Parameters:

  • params (Hash<Symbol,Object>)

    The search filter, only the matching resolvables are returned.

  • preload (Array<Symbol>) (defaults to: [])

    The list of attributes which should be preloaded. The missing attributes are lazy loaded, however for performance reasons you might ask to preload the attributes right at the beginning and avoid querying libzypp again later.

Returns:

See Also:

Since:

  • 4.2.6


59
60
61
62
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 59

def self.find(params, preload = [])
  attrs = (preload + UNIQUE_ATTRIBUTES).uniq
  Yast::Pkg.Resolvables(params, attrs).map { |r| new(r) }
end

Instance Method Details

#[](key) ⇒ Object

Backward compatibility method to access resolvable like hash.

Since:

  • 4.2.6


84
85
86
87
88
89
90
91
92
93
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 84

def [](key)
  log.info "Calling [] with #{key}. It is deprecated. Use method name " \
    "directly. Called from #{caller(1).first}"

  public_send(key.to_sym)
# key not found, so return nil to be compatible
rescue NameError
  log.warn "attribute #{key} not defined, returning nil."
  nil
end

#respond_to_missing?(method, _private) ⇒ Boolean

defines for dynamic methods also respond_to?

Returns:

  • (Boolean)

Since:

  • 4.2.6


121
122
123
124
125
126
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 121

def respond_to_missing?(method, _private)
  return true if instance_variable_defined?("@#{method}")
  return true if UNIQUE_ATTRIBUTES.include?(method.to_sym)

  false
end