Module: Yast::Convert Deprecated

Defined in:
src/ruby/yast/convert.rb

Overview

Deprecated.

ruby need not type conversion and int<->float conversion is explicit

Wrapper to simulate behavior of type conversion in YCP. there is generated shortcuts for conversion to_

Class Method Summary collapse

Class Method Details

.convert(object, options) ⇒ Object

Converts object from given type to target one.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'src/ruby/yast/convert.rb', line 46

def self.convert(object, options)
  from = options[:from].dup
  to = options[:to].dup

  # ignore whitespaces and specialization in types
  to.gsub!(/<.*>/, "")
  to.gsub!(/\s+/, "")
  from.gsub!(/<.*>/, "")
  from.gsub!(/\s+/, "")

  # reference to function
  to = "function" if to =~ /\(.*\)/

  raise "missing parameter :from" unless from
  raise "missing parameter :to" unless to

  return nil if object.nil?
  return object if from == to

  return object if from == "any" && allowed_type(object, to)
  if to == "float"
    return nil unless object.is_a?(::Integer)
    return object.to_f
  elsif to == "integer"
    return nil unless object.is_a? Float
    Yast.y2warning "Conversion from integer to float lead to loose precision."
    return object.to_i
  end

  return object if to == "locale" && from == "string"
  return object if to == "string" && from == "locale"

  Yast.y2warning(-1, "Cannot convert #{object.class} from '#{from}' to '#{to}'")
  nil
end

.to_boolean(object) ⇒ Boolean?

Returns object, or nil if it is not true or false.

Returns:

  • (Boolean, nil)

    object, or nil if it is not true or false



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_float(object) ⇒ Float?

Returns object, or nil if it is not a Float.

Returns:

  • (Float, nil)

    object, or nil if it is not a Float

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_integer(object) ⇒ Integer?

Returns object, or nil if it is not a Integer.

Returns:

  • (Integer, nil)

    object, or nil if it is not a Integer

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_list(object) ⇒ Array?

Returns object, or nil if it is not a Array.

Returns:

  • (Array, nil)

    object, or nil if it is not a Array

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_locale(object) ⇒ String?

Returns object, or nil if it is not a String.

Returns:

  • (String, nil)

    object, or nil if it is not a String



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_map(object) ⇒ Hash?

Returns object, or nil if it is not a Hash.

Returns:

  • (Hash, nil)

    object, or nil if it is not a Hash

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_path(object) ⇒ Path?

Returns object, or nil if it is not a Path.

Returns:

  • (Path, nil)

    object, or nil if it is not a Path

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_string(object) ⇒ String?

Returns object, or nil if it is not a String.

Returns:

  • (String, nil)

    object, or nil if it is not a String

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_symbol(object) ⇒ Symbol?

Returns object, or nil if it is not a Symbol.

Returns:

  • (Symbol, nil)

    object, or nil if it is not a Symbol

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end

.to_term(object) ⇒ Term?

Returns object, or nil if it is not a Term.

Returns:

  • (Term, nil)

    object, or nil if it is not a Term

See Also:



39
40
41
42
43
# File 'src/ruby/yast/convert.rb', line 39

Ops::SHORTCUT_TYPES.each do |type|
  define_singleton_method("to_#{type}") do |object|
    convert object, :from => "any", :to => type
  end
end