Class: CIM::Variant

Inherits:
Object
  • Object
show all
Defined in:
lib/cim/variant.rb

Overview

A Variant is a typed value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :null, value = nil) ⇒ Variant

Creates a typed value

type: See CIM::Type value: A Ruby value

No attempt is made to check if the type matches the value.



24
25
26
27
# File 'lib/cim/variant.rb', line 24

def initialize type = :null, value = nil
  @type = (type.kind_of? CIM::Type) ? type : CIM::Type.new(type)
  @value = value unless value == :null
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/cim/variant.rb', line 15

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/cim/variant.rb', line 15

def value
  @value
end

Instance Method Details

#==(v) ⇒ Object

Checks equality of the Variant with a Ruby value or another Variant



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cim/variant.rb', line 31

def == v
#      $stderr.puts "<#{@type}>#{self} == #{v.class}"
  return false unless self.is_a?(v)
  case v
  when NilClass then     @value.nil?
  when FalseClass then   !@value
  when TrueClass then    @value
  when String then       @value == v
  when Integer then      @value == v
  when Float then        @value == v
  when CIM::Variant then @value == v.value
  else
	false
  end			
end

#is_a?(klass) ⇒ Boolean

Check type against Ruby class

Returns:

  • (Boolean)


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
# File 'lib/cim/variant.rb', line 49

def is_a? klass
#      puts "Variant#is_a? : #{self.inspect} is_a #{klass.class}:#{klass.inspect}"
  case klass
  when NilClass      then @type.matches? :null
  when FalseClass    then @type.matches? :boolean
  when TrueClass     then @type.matches? :boolean
  when Integer       then @type.matches?(:uint64) || @type.matches?(:sint64) 
  when Float         then @type.matches? :real32
  when Symbol        then @type.matches? klass
  when String        then
    @type.matches? :string
  when CIM::Variant  then @type.matches? klass.type
  else
    if klass == Integer
      @type.matches?(:uint64) || @type.matches?(:sint64) 
    elsif klass == Float
      @type.matches? :real32
    elsif klass == String
      @type.matches? :string
    else
#          puts "Nothing matches #{klass}:#{klass.inspect}"
      false
    end
  end
end

#to_sObject

returns a string representation in MOF syntax format



77
78
79
80
81
82
83
# File 'lib/cim/variant.rb', line 77

def to_s
  if @type == :null
	"null"
  else
	"#{@value.inspect}"
  end
end