Class: SpecTools::Attr

Inherits:
Object
  • Object
show all
Extended by:
SpecToolsExtensions
Includes:
SpecToolsExtensions
Defined in:
lib/spectools.rb,
lib/vnmsh.rb

Overview

SpecTools::Attr

Represents a Spectrum Attribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SpecToolsExtensions

call_default_extension, call_extension, method_missing, method_missing

Constructor Details

#initialize(id = nil, name = nil, value = nil, value_table = nil, enums = nil, list = false, type = nil, external = false, readable = false, writeable = false, shared = false, guaranteed = false, global = false, memory = false, database = false, polled = false, logged = false, preserve = false) ⇒ Attr

Returns a new instance of Attr.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/spectools.rb', line 147

def initialize(id = nil, name = nil, value = nil, value_table = nil, enums = nil, list = false, type = nil, external = false, readable = false, writeable = false, shared = false, guaranteed = false, global = false, memory = false, database = false, polled = false, logged = false, preserve = false)
  if id.nil? || id.hex?
    @id = id
  else
    raise ArgumentError, "Id is not a hex code"
  end
  @name = name
  @enums = enums
  @type = type
  @external = external
  @readable = readable
  @writeable = writeable
  @shared = shared
  @list = list
  @guaranteed = guaranteed
  @global = global
  @memory = memory
  @database = database
  @polled = polled
  @logged = logged
  @preserve = preserve
  if list
    if value_table 
      @value_table = value_table
    else
      value_table = Hash.new
    end
    @value = nil
  else
    @value_table = nil
    @value = value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SpecToolsExtensions

Instance Attribute Details

#databaseObject

Set to true if the attribute is a database attribute



139
140
141
# File 'lib/spectools.rb', line 139

def database
  @database
end

#enumsObject

A hash of the attribute’s available enumerations



119
120
121
# File 'lib/spectools.rb', line 119

def enums
  @enums
end

#externalObject

Set to true if the attribute is external



123
124
125
# File 'lib/spectools.rb', line 123

def external
  @external
end

#globalObject

Set to true of the attribute is global



135
136
137
# File 'lib/spectools.rb', line 135

def global
  @global
end

#guaranteedObject

Set to true if the attribute is guaranteed



133
134
135
# File 'lib/spectools.rb', line 133

def guaranteed
  @guaranteed
end

#idObject

The attribute ID



107
108
109
# File 'lib/spectools.rb', line 107

def id
  @id
end

#listObject

Set to true if the attribute is a list attribute



131
132
133
# File 'lib/spectools.rb', line 131

def list
  @list
end

#loggedObject

Set to true if the attribute is logged



143
144
145
# File 'lib/spectools.rb', line 143

def logged
  @logged
end

#memoryObject

Set to true if the attribute is in memory



137
138
139
# File 'lib/spectools.rb', line 137

def memory
  @memory
end

#nameObject

The attribute name



109
110
111
# File 'lib/spectools.rb', line 109

def name
  @name
end

#polledObject

Set to true if the attribute is polled



141
142
143
# File 'lib/spectools.rb', line 141

def polled
  @polled
end

#preserveObject

Set to true if the attribute is preserved



145
146
147
# File 'lib/spectools.rb', line 145

def preserve
  @preserve
end

#readableObject

Set to true if the attribute is readable



125
126
127
# File 'lib/spectools.rb', line 125

def readable
  @readable
end

#sharedObject

Set to true if the attribute is shared



129
130
131
# File 'lib/spectools.rb', line 129

def shared
  @shared
end

#typeObject

The primitive type of the attribute



121
122
123
# File 'lib/spectools.rb', line 121

def type
  @type
end

#valueObject

Override the default value accessor. If we’re a list attribute, return the first value in the list.



114
115
116
# File 'lib/spectools.rb', line 114

def value
  @value
end

#value_tableObject

For list attributes, a hash of instance-value pairs. Can only be set if the attribute is a list attribute.



117
118
119
# File 'lib/spectools.rb', line 117

def value_table
  @value_table
end

#writeableObject

Set to true if the attribute is writeable



127
128
129
# File 'lib/spectools.rb', line 127

def writeable
  @writeable
end

Class Method Details

.cli_parse_mh(line) ⇒ Object

Take a line of CLI show attributes mh= output and populate a new Attr object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vnmsh.rb', line 8

def self.cli_parse_mh(line)
  specattr = Attr.new()
  #Because the instance identifier may not fit into the column space allocated, we can't reliably use unpack
  #to parse the data. Instead, we'll use a regex. This means that the attr name can't contain whitespace,
  #but it should be okay.
  if line.chomp =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/
    specattr.id = $1 
    specattr.name = $2
    specattr.list = true
    specattr.value_table[$3] = $4
  elsif line.chomp =~ /^(\S+)\s+(\S+)\s+(.+)$/
    specattr.id = $1
    specattr.name = $2
    specattr.value = $3
  end
  return specattr
end

.cli_parse_mth(line) ⇒ Object

Take a line of CLI show attributes mth= output and populate a new Attr object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vnmsh.rb', line 27

def self.cli_parse_mth(line)
  attr = Attr.new
  attr.id,attr.name,attr.type,flags = line.chomp.unpack('A12A33A18A23')
  flags.split(/,/).each do |flag|
    case flag
      when 'E'
        attr.external = true
      when 'R'
        attr.readable = true
      when 'W'
        attr.writeable = true
      when 'S'
        attr.shared = true
      when 'T'
        attr.list = true
      when 'G'
        attr.guaranteed = true
      when 'O' 
        attr.global = true
      when 'M'
        attr.memory = true
      when 'D'
        attr.database = true
      when 'P'
        attr.polled = true
      when 'L'
        attr.logged = true
      when 'V'
        attr.preserve = true
    end
  end
  return attr
end

Instance Method Details

#cli_get_enums(session = nil) ⇒ Object

Use CLI to retrieve the enumerations for the Attr. Returns a new hash of enumerations.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vnmsh.rb', line 63

def cli_get_enums(session=nil)
  unless self.id.hex?
    raise ArgumentError, 'To enumerate this Attr, the Attr must have a valid id.'
  end
  enums = Hash.new
  session = VNMSH.get_session(session)
  enum_output = session.show_enumerations(:attr,self)
  if enum_output != nil
    enum_output.each do |line|
      line.chomp!
      id,str,val = line.unpack('A12A34A12')
      val.sub!(/\s+/, '')
      str.sub!(/\s+$/, '')
      enums[val] = str
    end
  else
    return nil
  end
  return enums
end

#cli_get_enums!(session = nil) ⇒ Object

Execute cli_get_enums and modify the Attr object in place.



86
87
88
89
# File 'lib/vnmsh.rb', line 86

def cli_get_enums!(session=nil)
  new_enums = cli_get_enums(session)
  self.enums = new_enums
end

#enumerate(*args) ⇒ Object

Look up the value of the given attribute and return its enumerated counterpart. If the Attr does not have a populated enums hash, will attempt to use SpecToolsExtensions#call_default_extension to obtain the enumerations via the get_enums method. If enumeration fails, the original value is returned. All args are used for the call to get_enums, if necessary. Note: Errors raised by a call to get_enums will not be caught.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/spectools.rb', line 227

def enumerate(*args)
  if enums.kind_of?(Hash)
    avail_enums = enums
  else
    begin
      avail_enums = call_default_extension(:get_enums, *args)
    rescue NoAccessMethodError, NoMethodError
      avail_enums = {}
    end
  end
  if avail_enums[value]
    return avail_enums[value]
  else
    return value
  end

end

#enumerate!(*args) ⇒ Object

Change the value of the attribute to its enumerated string. If enumeration fails, the value is left unchanged. All arguments are passed through to enumerate.



248
249
250
251
# File 'lib/spectools.rb', line 248

def enumerate!(*args)
  enum = enumerate(*args)
  self.value = enum unless enum == value
end