Class: Gdsii::BnfRecords

Inherits:
Object
  • Object
show all
Defined in:
lib/gdsii/bnf.rb

Overview

Used to store records for a record grouping (i.e. classes descending from Group). Only records that are in the BnfSpec may be added.

Instance Method Summary collapse

Constructor Details

#initialize(parent_class) ⇒ BnfRecords

Create a new BnfRecords object. The parent class is stored so that entries may be compared against the BnfSpec.



139
140
141
142
# File 'lib/gdsii/bnf.rb', line 139

def initialize(parent_class)
  @parent_class = parent_class
  @bnf_records = {}
end

Instance Method Details

#add(key, value) ⇒ Object

Adds the record or record value to the bnf_records hash (applicable only for records with multiple entries). Returns the updated list of values. Used internally by various grouping accessor methods. Returns the object being set.



219
220
221
222
223
224
225
226
# File 'lib/gdsii/bnf.rb', line 219

def add(key, value)
  if bnf_spec[key].multiple?
    value = coerce_record_value(key, value)
    get(key).push value
  else
    raise TypeError, "BNF for key #{key} is singular in class #{parent_class}; use #set instead"
  end
end

#bnf_keysObject

Returns the BNF record keys that exist in this grouping of BNF records.



154
155
156
# File 'lib/gdsii/bnf.rb', line 154

def bnf_keys()
  @bnf_records.keys
end

#bnf_specObject

Returns the BnfSpec of the parent class.



147
148
149
# File 'lib/gdsii/bnf.rb', line 147

def bnf_spec()
  @parent_class.bnf_spec
end

#delete_key(key) ⇒ Object

Deletes the given key from the bnf_records hash.



249
250
251
# File 'lib/gdsii/bnf.rb', line 249

def delete_key(key)
  @bnf_records.delete(key)
end

#get(key) ⇒ Object Also known as: []

Retrieves the Record for a given BNF item key. If the record is multiple according to the BNF description, then an array of Record objects is returned. Used internally by various grouping accessor methods.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gdsii/bnf.rb', line 164

def get(key)
  bnf_item = bnf_spec[key]
  if bnf_item.multiple?
    @bnf_records[key] = [] unless @bnf_records.has_key? key
    @bnf_records[key]
  else
    if key.class == Class and @bnf_records[key].nil?
      @bnf_records[key] = key.new
    end
    @bnf_records[key]
  end
end

#get_data(key) ⇒ Object

Retrieves the Record data (i.e value) for a given BNF item key. If the record is multiple according to the BNF description, then an array of Record data is returned. Used internally by various grouping accessor methods.



185
186
187
188
189
190
191
# File 'lib/gdsii/bnf.rb', line 185

def get_data(key)
  if bnf_spec[key].multiple?
    get(key).map {|record| record.data_value}
  else
    ((record = get(key)).nil?) ? nil : record.data_value
  end
end

#has_data?(key) ⇒ Boolean

True if the record item is not nil (if the BnfItem is singular) or an empty array (if the BnfItem is multiple).

Returns:

  • (Boolean)


257
258
259
260
261
262
263
264
265
# File 'lib/gdsii/bnf.rb', line 257

def has_data?(key)
  bnf_item = bnf_spec[key]
  if (bnf_item.multiple? and get(key).empty?) or
      (not bnf_item.multiple? and get(key).nil?)
    false
  else
    true
  end
end

#reject!(key) ⇒ Object

Accepts a code block which should return true or false. If the return value is true, then the value meeting the criteria is removed. This is used internally by various grouping accessor methods. Note, this is only applicable for records with multiple values.

object.reject!(Property) {|property|
  property.attr == 1
}


238
239
240
241
242
243
244
# File 'lib/gdsii/bnf.rb', line 238

def reject!(key)
  if bnf_spec[key].multiple?
    @bnf_records[key].reject! {|value| yield value}
  else
    raise TypeError, "BNF for key #{key} is singular in class #{parent_class}; use #set to nil"
  end
end

#set(key, value) ⇒ Object Also known as: []=

Sets the record data for a given BNF item key. The value may be the of the class to be added or can be a raw value which is automatically coerced into the proper class for the given key. Used internally by various grouping accessor methods. Returns the object being set.



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gdsii/bnf.rb', line 199

def set(key, value)
  if key.class == Class
    @bnf_records[key] = value
  elsif value.nil?
    @bnf_records[key] = value
  else
    value = coerce_record_value(key, value)
    value = [value] if bnf_spec[key].multiple? and value.class != Array
    @bnf_records[key] = value
  end
end

#write(file, alt_bnf = nil) ⇒ Object

Write the BNF records to file. Ensures that the required records exist according to the BnfSpec (otherwise an error is raised). A file object is expected.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/gdsii/bnf.rb', line 272

def write(file, alt_bnf=nil)
  # Loop through each BNF item
  bnf = alt_bnf ? alt_bnf : bnf_spec
  bnf.each_item do |bnf_item|
    if has_data?(bnf_item.key)
      if bnf_item.multiple?
        get(bnf_item.key).each {|record| record.write(file)}
      else
        get(bnf_item.key).write(file)
      end
    elsif not bnf_item.optional?
      raise "Required data in BNF are not set: #{bnf_item}"
    end
  end
end