Class: CIM::Type

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

Overview

Type represents a CIM basic type

The following basic types are known:

uint8

Unsigned 8-bit integer

sint8

Signed 8-bit integer

uint16

Unsigned 16-bit integer

sint16

Signed 16-bit integer

uint32

Unsigned 32-bit integer

sint32

Signed 32-bit integer

uint64

Unsigned 64-bit integer

sint64

Signed 64-bit integer

string

UCS-2 string

boolean

Boolean

real32

IEEE 4-byte floating-point

real64

IEEE 8-byte floating-point

dateTime

A string containing a date-time

reference

Strongly typed reference

char16

16-bit UCS-2 character

void

– allowed for WMI only

Direct Known Subclasses

Array, ReferenceType

Constant Summary collapse

TYPES =
[:null,:void,:boolean,:char16,:string,:uint8,:sint8,:uint16,:sint16,:uint32,:sint32,:uint64,:sint64,:real32,:real64,:dateTime,:class,:reference,:array]
NORMALIZE =
{
  :bool => :boolean,
  :datetime => :dateTime
}
MATCHES =
{
  :null => [],
  :void => [], # WMI
  :boolean => [],
  :char16 => [ :null, :string ],
  :string => [ :null ],
  :uint8 => [ :null ],
  :sint8 => [ :null ],
  :uint16 => [ :null, :uint8 ],
  :sint16 => [ :null, :sint8 ],
  :uint32 => [ :null, :uint8, :uint16 ],
  :sint32 => [ :null, :sint8, :sint16 ],
  :uint64 => [ :null, :uint8, :uint16, :sint32 ],
  :sint64 => [ :null, :sint8, :sint16, :sint32 ],
  :real32 => [ :null ],
  :real64 => [ :null, :real32 ],
  :dateTime => [ :null ],
  :class => [ :null ],
  :reference => [ :null ],
  :array => [ :null ]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Type

Basic types are created by-symbol or by-name

CIM::Type.new(:boolean) == CIM::Type.new(“boolean”)

Raises:

  • (TypeError)


72
73
74
75
76
77
# File 'lib/cim/type.rb', line 72

def initialize type
  type.downcase! if type.is_a? String
  @type = type.to_sym
  @type = NORMALIZE[@type] || @type
  raise TypeError.new("#{type}") unless TYPES.include? @type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



66
67
68
# File 'lib/cim/type.rb', line 66

def type
  @type
end

Class Method Details

.normalize(type) ⇒ Object



36
37
38
# File 'lib/cim/type.rb', line 36

def self.normalize type
  (type.is_a? self) ? type : self.new(type)
end

Instance Method Details

#==(t) ⇒ Object

type equality



96
97
98
99
100
101
102
103
104
# File 'lib/cim/type.rb', line 96

def == t
  case t
  when Type then t.type == @type
  when Symbol then t == @type
  when String then t.downcase == @type.to_s
  else
	false
  end
end

#array?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/cim/type.rb', line 78

def array?
  false
end

#matches?(x) ⇒ Boolean

check if another Type or Variant matches

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cim/type.rb', line 154

def matches? x
  #	puts ">#{self}<{#{self.class}}.matches?>#{x.inspect}<{#{x.class}}"
  case x
  when CIM::Type, CIM::Variant
	return true if x.type == @type
	return true if MATCHES[@type].include? x.type
	false
  when ::Array
	return false unless self.is_a? CIM::Array
	x.each do |v|
	  return false unless matches_value @type,v
	end
	true
  else
	matches_value @type, x
  end
end

#to_sObject

returns a string representation in MOF syntax format



84
85
86
# File 'lib/cim/type.rb', line 84

def to_s
  @type.to_s
end

#to_symObject

returns a symbol representation of the type



90
91
92
# File 'lib/cim/type.rb', line 90

def to_sym
  @type
end