cim 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ #
2
+ # cim/qualifier_scopes.rb - class CIM::QualifierScopes, class CIM::QualifierScopesError
3
+ #
4
+ # A pure-Ruby implementation of the CIM meta model.
5
+ #
6
+ # Copyright (c) 2010 Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the Ruby license
9
+ #
10
+ module CIM
11
+
12
+ class QualifierScopesError < ArgumentError
13
+ #
14
+ # Raised if wrong Scope passed
15
+ #
16
+ def initialize element, msg = nil
17
+ @element = element
18
+ super msg
19
+ end
20
+ def to_s
21
+ "#{@element} is not a valid meta element for scopes"
22
+ end
23
+ end
24
+
25
+ class QualifierScopes
26
+ META_ELEMENTS = [ :schema, :class, :association, :indication, :qualifier, :property, :reference, :method, :parameter, :any ]
27
+ attr_reader :elements
28
+ #
29
+ # call-seq:
30
+ # QualifierScopes.new => qualifier_scopes
31
+ # QualifierScopes.new(:association) => qualifier_scopes
32
+ # QualifierScopes.new("association") => qualifier_scopes
33
+ # QualifierScopes.new("Association") => qualifier_scopes
34
+ #
35
+ # raises QualifierScopesError
36
+ #
37
+ def initialize *elements
38
+ @elements = []
39
+ elements.flatten.each do |element|
40
+ self << element
41
+ end
42
+ end
43
+ #
44
+ # call-seq:
45
+ # qualifier_scopes << :association
46
+ # qualifier_scopes << "association"
47
+ # qualifier_scopes << "Association"
48
+ #
49
+ # raises QualifierScopesError
50
+ #
51
+ def << element
52
+ @elements << (normalize element)
53
+ self
54
+ end
55
+ #
56
+ # call-seq:
57
+ # qualifier_scopes.has? :association
58
+ # qualifier_scopes.has? "association"
59
+ # qualifier_scopes.has? "Association"
60
+ #
61
+ # raises QualifierScopesError
62
+ #
63
+ def include? element
64
+ @elements.include?(normalize element)
65
+ end
66
+ alias includes? include?
67
+ #
68
+ # Number of scopes
69
+ #
70
+ def size
71
+ @elements.size
72
+ end
73
+ #
74
+ # returns a string representation in MOF syntax format
75
+ #
76
+ def to_s
77
+ "Scope(#{@elements.join(', ')})"
78
+ end
79
+ private
80
+ def normalize element
81
+ element.downcase! if element.is_a?(String)
82
+ e = element.to_sym
83
+ raise QualifierScopesError.new(element) unless META_ELEMENTS.include?(e)
84
+ e
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,73 @@
1
+ #
2
+ # cim/qualifier_set.rb - class CIM::QualifierSet
3
+ #
4
+ # A pure-Ruby implementation of the CIM meta model.
5
+ #
6
+ # Copyright (c) 2010 Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the Ruby license
9
+ #
10
+ module CIM
11
+ #
12
+ # Set of Qualifier
13
+ #
14
+ class QualifierSet < ::Array
15
+ def initialize *args
16
+ super 0
17
+ args.flatten.each do |q|
18
+ case q
19
+ when QualifierDeclaration: q = Qualifier.new q
20
+ when Qualifier:
21
+ else
22
+ q = Qualifier.new(QualifierDeclaration.new q)
23
+ end
24
+ self << q
25
+ end
26
+ end
27
+ #
28
+ # check if qualifier exists
29
+ #
30
+ # call-seq:
31
+ # qualifier_set.includes?(qualifier) => bool
32
+ # qualifier_set.includes?(qualifier_declaration) => bool
33
+ # qualifier_set.includes?(:description) => bool
34
+ # qualifier_set.includes?("description", :string) => bool
35
+ #
36
+ def includes? qualifier,type=nil
37
+ (self[qualifier,type]) ? true : false
38
+ end
39
+ alias include? includes?
40
+ #
41
+ # get qualifier by name and type
42
+ #
43
+ def [] qualifier,type=nil
44
+ self.each do |q|
45
+ unless type.nil?
46
+ next unless q.type == type
47
+ end
48
+ # puts "#{q} == #{qualifier}<#{type}>"
49
+ return q if case qualifier
50
+ when CIM::Qualifier
51
+ q == qualifier.declaration
52
+ when CIM::QualifierDeclaration
53
+ q == qualifier
54
+ when String
55
+ q.declaration == qualifier
56
+ when Symbol
57
+ q.declaration == qualifier
58
+ else
59
+ raise "Unknown parameter in #{self.class}[]"
60
+ end
61
+ end
62
+ nil
63
+ end
64
+ #
65
+ # returns a string representation in MOF syntax format
66
+ #
67
+ def to_s
68
+ return "" if self.empty?
69
+ q = self.join(", ")
70
+ return "[#{q}]"
71
+ end
72
+ end
73
+ end
@@ -1,5 +1,23 @@
1
+ #
2
+ # cim/reference.rb - class CIM::Reference
3
+ #
4
+ # A pure-Ruby implementation of the CIM meta model.
5
+ #
6
+ # Copyright (c) 2010 Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the Ruby license
9
+ #
1
10
  module CIM
11
+ #
12
+ # Reference is a pointer (reference) to a Class
13
+ #
14
+ # A Reference is a special type of Property reserved for
15
+ # use in Associations
16
+ #
2
17
  class Reference < CIM::Property
18
+ #
19
+ # Identifies a Property as a Reference
20
+ #
3
21
  def reference?
4
22
  true
5
23
  end
@@ -1,4 +1,38 @@
1
+ #
2
+ # cim/type.rb - class CIM::Type, class CIM::Array, class CIM::ReferenceType
3
+ #
4
+ # A pure-Ruby implementation of the CIM meta model.
5
+ #
6
+ # Copyright (c) 2010 Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the Ruby license
9
+ #
10
+
11
+
1
12
  module CIM
13
+ #
14
+ # Type represents a CIM basic type
15
+ #
16
+ # The following basic types are known:
17
+ # uint8:: Unsigned 8-bit integer
18
+ # sint8:: Signed 8-bit integer
19
+ # uint16:: Unsigned 16-bit integer
20
+ # sint16:: Signed 16-bit integer
21
+ # uint32:: Unsigned 32-bit integer
22
+ # sint32:: Signed 32-bit integer
23
+ # uint64:: Unsigned 64-bit integer
24
+ # sint64:: Signed 64-bit integer
25
+ # string:: UCS-2 string
26
+ # bool:: Boolean
27
+ # real32:: IEEE 4-byte floating-point
28
+ # real64:: IEEE 8-byte floating-point
29
+ # datetime:: A string containing a date-time
30
+ # reference:: Strongly typed reference
31
+ # char16:: 16-bit UCS-2 character
32
+ # datetime:: Timestamp
33
+ # void:: -- allowed for WMI only
34
+ #
35
+ #
2
36
  class Type
3
37
  TYPES = [:null,:void,:bool,:char16,:string,:uint8,:sint8,:uint16,:sint16,:uint32,:sint32,:uint64,:sint64,:real32,:real64,:datetime,:class,:reference,:array]
4
38
  MATCHES = {
@@ -23,21 +57,36 @@ module CIM
23
57
  :array => [ :null ]
24
58
  }
25
59
  attr_reader :type
60
+ #
61
+ # Basic types are created by-symbol or by-name
62
+ #
63
+ # CIM::Type.new(:bool) == CIM::Type.new("bool")
64
+ #
26
65
  def initialize type
27
66
  type.downcase! if type.is_a? String
28
67
  @type = type.to_sym
29
68
  raise TypeError.new("#{type}") unless TYPES.include? @type
30
69
  end
70
+ #
71
+ # returns a string representation in MOF syntax format
72
+ #
31
73
  def to_s
32
74
  @type.to_s
33
75
  end
76
+ #
77
+ # returns a symbol representation of the type
78
+ #
34
79
  def to_sym
35
80
  @type
36
81
  end
82
+ #
83
+ # type equality
84
+ #
37
85
  def == t
38
86
  case t
39
87
  when Type: t.type == @type
40
88
  when Symbol: t == @type
89
+ when String: t.downcase == @type.to_s
41
90
  else
42
91
  false
43
92
  end
@@ -77,6 +126,9 @@ module CIM
77
126
  end
78
127
  end
79
128
  public
129
+ #
130
+ # check if another Type or Variant matches
131
+ #
80
132
  def matches? x
81
133
  # puts ">#{self}<{#{self.class}}.matches?>#{x.inspect}<{#{x.class}}"
82
134
  case x
@@ -95,12 +147,30 @@ module CIM
95
147
  end
96
148
  end
97
149
  end
150
+
151
+ #
152
+ # Array represents an array of identical typed value
153
+ #
154
+ #
98
155
  class Array < Type
99
156
  attr_reader :size
157
+ #
158
+ # Arrays are initialized by size and type
159
+ # Passing 0 (zero) as the size creates an unlimited array
160
+ #
100
161
  def initialize size, type
101
162
  @size = size
102
163
  super type
103
164
  end
165
+ #
166
+ # An array is equal to any other array, regardless of the enclosed type
167
+ #
168
+ def == t
169
+ t == :array
170
+ end
171
+ #
172
+ # returns a string representation of the array type
173
+ #
104
174
  def to_s
105
175
  if @size > 0
106
176
  "#{super}[#{@size}]"
@@ -109,12 +179,23 @@ module CIM
109
179
  end
110
180
  end
111
181
  end
182
+
183
+ #
184
+ # ReferenceType is a strongly typed reference to a class
185
+ #
186
+ #
112
187
  class ReferenceType < Type
113
188
  attr_reader :name
189
+ #
190
+ # Creates a strongly typed reference to class +name+
191
+ #
114
192
  def initialize name
115
193
  @name = name
116
- super :class
194
+ super "class"
117
195
  end
196
+ #
197
+ # returns a string representation of the reference type
198
+ #
118
199
  def to_s
119
200
  "#{@name} ref"
120
201
  end
@@ -1,11 +1,50 @@
1
+ #
2
+ # cim/variant.rb - class CIM::Variant
3
+ #
4
+ # A pure-Ruby implementation of the CIM meta model.
5
+ #
6
+ # Copyright (c) 2010 Klaus Kämpf <kkaempf@suse.de>
7
+ #
8
+ # Licensed under the Ruby license
9
+ #
1
10
  module CIM
11
+ #
2
12
  # A Variant is a typed value
13
+ #
3
14
  class Variant
4
15
  attr_reader :type, :value
16
+ #
17
+ # Creates a typed value
18
+ #
19
+ # +type+: See CIM::Type
20
+ # +value+: A Ruby value
21
+ #
22
+ # No attempt is made to check if the type matches the value.
23
+ #
5
24
  def initialize type = :null, value = nil
6
25
  @type = (type.kind_of? CIM::Type) ? type : CIM::Type.new(type)
7
26
  @value = value unless value == :null
8
27
  end
28
+ #
29
+ # Checks equality of the Variant with a Ruby value or another Variant
30
+ #
31
+ def == v
32
+ # $stderr.puts "<#{@type}>#{self} == #{v.class}"
33
+ case v
34
+ when NilClass: @type == :null && @value.nil?
35
+ when FalseClass: @type == :bool && !@value
36
+ when TrueClass: @type == :bool && @value
37
+ when String: @type == :string && @value == v
38
+ when Integer: @type == :int && @value == v
39
+ when Float: @type == :real && @value == v
40
+ when CIM::Variant: @type == v.type && @value == v.value
41
+ else
42
+ false
43
+ end
44
+ end
45
+ #
46
+ # returns a string representation in MOF syntax format
47
+ #
9
48
  def to_s
10
49
  if @type == :null
11
50
  "null"
@@ -0,0 +1,9 @@
1
+ task :clean do
2
+ `rm -rf *~`
3
+ `rm -rf */*~`
4
+ `rm -rf */*/*~`
5
+ `rm -f Gemfile.lock`
6
+ `rm -rf doc`
7
+ `rm -rf .yardoc`
8
+ `rm -rf pkg`
9
+ end
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'yard'
3
+ YARD::Rake::YardocTask.new(:doc) do |t|
4
+ t.files = ['lib/**/*.rb']
5
+ t.options = ['--no-private']
6
+ end
7
+ rescue LoadError
8
+ STDERR.puts "Install yard if you want prettier docs"
9
+ require 'rdoc/task'
10
+ Rake::RDocTask.new(:doc) do |rdoc|
11
+ rdoc.rdoc_dir = "doc"
12
+ rdoc.title = "dm-keeper-adapter #{CIM::VERSION}"
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ Rake::TestTask.new do |t|
2
+ t.libs << File.expand_path('../test', __FILE__)
3
+ t.libs << File.expand_path('../', __FILE__)
4
+ t.test_files = FileList['test/test*.rb']
5
+ t.verbose = true
6
+ end
@@ -9,7 +9,8 @@ class MethodTest < Test::Unit::TestCase
9
9
  assert m
10
10
  assert_equal "Foo", m.name
11
11
  assert m.qualifiers.size > 0
12
- # assert m.qualifiers.include?( :description, :string )
12
+ assert m.qualifiers.include?( :description )
13
+ assert m.qualifiers.include?( :description, :string )
13
14
  # assert_equal "This is a foo method", m.description
14
15
  assert_equal false, m.to_s.empty?
15
16
  end
@@ -0,0 +1,39 @@
1
+ $: << File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require "test/unit"
4
+ require "cim"
5
+
6
+ class QualifierSetTest < Test::Unit::TestCase
7
+ def setup
8
+ qbool = CIM::QualifierDeclaration.new("flag")
9
+ qint = CIM::QualifierDeclaration.new("value", :uint32)
10
+ qstring = CIM::QualifierDeclaration.new("description", :string, "This is a description")
11
+ @qualifiers = CIM::QualifierSet.new
12
+ @qualifiers << CIM::Qualifier.new(qbool)
13
+ @qualifiers << CIM::Qualifier.new(qint)
14
+ @qualifiers << CIM::Qualifier.new(qstring)
15
+ end
16
+ def test_key
17
+ assert_equal 3, @qualifiers.size
18
+ end
19
+ def test_prefill
20
+ q = CIM::QualifierSet.new "a", :b
21
+ assert 2, q.size
22
+ assert q.include?( "a" )
23
+ assert q.include?( "b", :bool )
24
+ end
25
+ def test_include
26
+ assert @qualifiers.include?( :flag )
27
+ assert @qualifiers.include?( "flag" )
28
+ assert @qualifiers.include?( "flag", :bool )
29
+ assert @qualifiers.include?( "flag", "bool" )
30
+ assert !@qualifiers.include?( "flag", :string )
31
+ end
32
+ def test_access
33
+ assert @qualifiers[:flag]
34
+ assert @qualifiers["flag"]
35
+ assert @qualifiers["flag", :bool]
36
+ assert @qualifiers["flag", "bool"]
37
+ assert !@qualifiers["flag", :string]
38
+ end
39
+ end