Module: Yast

Included in:
Client, Module
Defined in:
src/binary/Yast.cc,
src/ruby/yast/ui.rb,
src/ruby/yast/ops.rb,
src/ruby/yast/scr.rb,
src/ruby/yast/wfm.rb,
src/ruby/yast/i18n.rb,
src/ruby/yast/path.rb,
src/ruby/yast/term.rb,
src/ruby/yast/yast.rb,
src/ruby/yast/break.rb,
src/ruby/yast/client.rb,
src/ruby/yast/logger.rb,
src/ruby/yast/module.rb,
src/ruby/yast/arg_ref.rb,
src/ruby/yast/convert.rb,
src/ruby/yast/fun_ref.rb,
src/ruby/yast/builtins.rb,
src/ruby/yast/debugger.rb,
src/ruby/yast/external.rb,
src/ruby/yast/profiler.rb,
src/ruby/yast/y2logger.rb,
src/ruby/yast/rspec/scr.rb,
src/ruby/yast/exportable.rb,
src/ruby/yast/ui_shortcuts.rb,
src/ruby/yast/rspec/matchers.rb,
src/ruby/yast/rspec/shortcuts.rb,
src/ruby/yast/y2start_helpers.rb,
src/ruby/yast/core_ext/ansi_string.rb

Overview


The YaST module encloses all Yast related code.


Defined Under Namespace

Modules: Builtins, Convert, CoreExt, Exportable, I18n, Logger, Ops, RSpec, SCR, UIShortcuts, WFM, Y2StartHelpers Classes: ArgRef, Break, Byteblock, Client, Debugger, External, FunRef, Module, Path, Profiler, Term, Y2Logger, YReference

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.%%Object

.------------- Backtrace begin -------------Object

.------------- Backtrace end ---------------Object

.deep_copy(object, options = {}) ⇒ Object

Note:

String, Yast::Path and Yast::Byteblock is also immutable in sense of Yast because there is no builtin operation for string modify that do not copy it. Use :full option to copy it also.

Makes deep copy of object. Difference to #dup or #clone is that it copy all elements of Array, Hash, Yast::Term. Immutable classes is just returned.

Examples:

how to refactor generated code

# old method where a is not need to copy
# and b is needed, but we plan to use full ruby features to modify strings
def old(a, b)
  a = copy_arg(a)
  b = copy_arg(b)
  ...
end

# refactored method
def new(a, b)
  b = copy_arg b, :full => true
  ...
end

Options Hash (options):

  • :full (TrueClass, FalseClass) — default: false

    make full copy even for types that is immutable in Yast builtins context



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'src/ruby/yast/yast.rb', line 70

def self.deep_copy(object, options = {})
  case object
  when Numeric, TrueClass, FalseClass, NilClass, Symbol # immutable
    object
  when ::String, Yast::Path, Yast::Byteblock # immutable in sense of yast buildins
    options[:full] ? object.clone : object
  when Yast::FunRef, Yast::ArgRef, Yast::External, Yast::YReference, Yast::YCode # contains only reference somewhere
    object
  when ::Hash
    object.each_with_object({}) do |kv, acc|
      acc[deep_copy(kv[0])] = deep_copy(kv[1])
    end
  when ::Array
    object.each_with_object([]) do |v, acc|
      acc << deep_copy(v)
    end
  else
    object.clone # deep copy
  end
end

.import(mname) ⇒ Object

Note:

for components written in ruby just require it and it is used directly without component system.

imports component module with given name and create wrapper for it.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'src/ruby/yast/yast.rb', line 157

def self.import(mname)
  modules = mname.split("::")

  base = Yast
  # Handle multilevel modules like YaPI::Network
  modules[0..-2].each do |module_|
    tmp_m = if base.constants.include?(module_.to_sym)
      base.const_get(module_)
    else
      base.const_set(module_, ::Module.new)
    end
    base = tmp_m
  end

  # do not reimport if already imported and contain some methods
  # ( in case namespace contain some methods )
  if base.constants.include?(modules.last.to_sym) &&
      !(base.const_get(modules.last).methods - Object.methods).empty?
    return
  end

  import_pure(mname)

  # do not create wrapper if module is in ruby and define itself object
  if base.constants.include?(modules.last.to_sym) &&
      !(base.const_get(modules.last).methods - Object.methods).empty?
    return
  end

  m = if base.constants.include?(modules.last.to_sym)
    base.const_get(modules.last)
  else
    ::Module.new
  end
  symbols(mname).each do |sname, stype|
    next if sname.empty?
    if stype == :function
      m.module_eval <<-"END"
        def self.#{sname}(*args)
          caller(1,1).first.match BACKTRACE_REGEXP
          return Yast::call_yast_function("#{mname}", :#{sname}, $1, $2.to_i, *args)
        end
      END
    elsif stype == :variable
      m.module_eval <<-"END"
        def self.#{sname}
          return Yast::call_yast_function("#{mname}", :#{sname})
        end
        def self.#{sname}= (value)
          return Yast::call_yast_function("#{mname}", :#{sname}, value)
        end
      END
    end
  end

  base.const_set(modules.last, m) unless base.constants.include?(modules.last.to_sym)
end

.include(target, path) ⇒ Object

Deprecated.

use "lib" directory where you can place common ruby code without any special handling.

includes module from include directory. given include must satisfied few restrictions. 1) file must contain module enclosed in Yast namespace with name constructed from path and its name it is constructed that all parts is upcased and also all [-_.] is replaced and next character must be upper case. At the end is appended Include string example in file network/udev_lan.source.rb must be module Yast::NetworkUdevLanSourceInclude 2) initialization of module must be in method with prefix initialize and rest is translated path, where all [-./] is replaced by underscore. Method take one parameter that is propagated target param. example in file network/udev_lan.source.rb initialization method will be initialize_network_udev_lan_source



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'src/ruby/yast/yast.rb', line 115

def self.include(target, path)
  path_without_suffix = path.sub(/\.rb$/, "")
  module_name = path_without_suffix
                .gsub(/^./, &:upcase)
                .gsub(/\/./)    { |s| s[1].upcase }
                .gsub(/[-_.]./) { |s| s[1].upcase } +
    "Include"

  loaded = Yast.constants.include? module_name.to_sym

  unless loaded
    path = find_include_file path
    require path
  end

  mod = Yast.const_get module_name

  # if never included, then include
  target.class.send(:include, mod) unless target.class.include?(mod)

  encoded_name = path_without_suffix.gsub(/[-.\/]/, "_")
  initialized_variable = "@" + encoded_name + "initialized"
  method_name = "initialize_" + encoded_name

  # tricky condition. Here collide two yast features that had old ycp
  # 1) in clients reapeated call results in new client object, but client base class
  #    is already defined, so not needed to include again, but it's
  #    needed to be reinitialized, so we need to call initialization method
  #    even if module is already included
  # 2) if there is multi include, then only first one must call initialization
  #    because other calls are ignored
  if target.respond_to?(method_name.to_sym) &&
      !target.instance_variable_defined?(initialized_variable)
    # allways set initialized before method call otherwise endless loop in
    # circle include calls
    target.instance_variable_set(initialized_variable, true)
    target.send(method_name.to_sym, target)
  end
end

.replaceObject

.RubyObject

.Object

Instance Method Details

#deep_copy(object) ⇒ Object Also known as: copy_arg

Shortcut for Yast::deep_copy

See Also:



93
94
95
# File 'src/ruby/yast/yast.rb', line 93

def deep_copy(object)
  Yast.deep_copy(object)
end