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.

Since:

  • 4.2.6



97
98
99
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 97

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.

Raises:

  • (ArgumentError)

Since:

  • 4.2.6



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 121

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.

See Also:

Since:

  • 4.2.6



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

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

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

Note:

The regular expressions used in the RPM dependency filters (e.g. "provides_regexp", "supplements_regexp") are POSIX extended regular expressions, not Ruby regular expressions!

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

Raises:

  • (ArgumentError)

    Raises ArgumentError when the passed regular expression is invalid

See Also:

Since:

  • 4.2.6



62
63
64
65
66
67
68
69
70
71
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 62

def self.find(params, preload = [])
  attrs = (preload + UNIQUE_ATTRIBUTES + OPTIONAL_ATTRIBUTES).uniq
  resolvables = Yast::Pkg.Resolvables(params, attrs)

  # currently nil is returned only when an invalid regular expression
  # is passed in a RPM dependency filter (like supplements_regexp: "autoyast(")
  raise ArgumentError, Yast::Pkg.LastError if resolvables.nil?

  resolvables.map { |r| new(r) }
end

.none?(params) ⇒ Boolean

Return true when there is no resolvable matching the requested parameters or false otherwise.

Since:

  • 4.2.6



89
90
91
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 89

def self.none?(params)
  !any?(params)
end

Instance Method Details

#[](key) ⇒ Object

Backward compatibility method to access resolvable like hash.

Since:

  • 4.2.6



102
103
104
105
106
107
108
109
110
111
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 102

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?

Since:

  • 4.2.6



139
140
141
142
143
144
# File 'library/packages/src/lib/y2packager/resolvable.rb', line 139

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

  false
end