Module: Yast::Convert Deprecated
- Defined in:
- src/ruby/yast/convert.rb
Overview
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
-
.convert(object, options) ⇒ Object
Converts object from given type to target one.
-
.to_boolean(object) ⇒ Boolean?
object, or
nil
if it is nottrue
orfalse
. -
.to_float(object) ⇒ Float?
object, or
nil
if it is not a Float. -
.to_integer(object) ⇒ Integer?
object, or
nil
if it is not a Integer. -
.to_list(object) ⇒ Array?
object, or
nil
if it is not a Array. -
.to_locale(object) ⇒ String?
object, or
nil
if it is not a String. -
.to_map(object) ⇒ Hash?
object, or
nil
if it is not a Hash. -
.to_path(object) ⇒ Path?
object, or
nil
if it is not a Path. -
.to_string(object) ⇒ String?
object, or
nil
if it is not a String. -
.to_symbol(object) ⇒ Symbol?
object, or
nil
if it is not a Symbol. -
.to_term(object) ⇒ Term?
object, or
nil
if it is not a Term.
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, ) from = [:from].dup to = [: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
.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |