class Gdsii::BnfRecords

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

Public Class Methods

new(parent_class) click to toggle source

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

# File lib/gdsii/bnf.rb, line 139
def initialize(parent_class)
  @parent_class = parent_class
  @bnf_records = {}
end

Public Instance Methods

[](key)
Alias for: get
[]=(key, value)
Alias for: set
add(key, value) click to toggle source

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.

# 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_keys() click to toggle source

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

# File lib/gdsii/bnf.rb, line 154
def bnf_keys()
  @bnf_records.keys
end
bnf_spec() click to toggle source

Returns the BnfSpec of the parent class.

# File lib/gdsii/bnf.rb, line 147
def bnf_spec()
  @parent_class.bnf_spec
end
delete_key(key) click to toggle source

Deletes the given key from the bnf_records hash.

# File lib/gdsii/bnf.rb, line 249
def delete_key(key)
  @bnf_records.delete(key)
end
get(key) click to toggle source

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.

# 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
Also aliased as: []
get_data(key) click to toggle source

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.

# 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) click to toggle source

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

# 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) { |value| ... } click to toggle source

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
}
# 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) click to toggle source

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.

# 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
Also aliased as: []=
write(file, alt_bnf=nil) click to toggle source

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.

# 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

Private Instance Methods

coerce_record_value(key, value) click to toggle source

Used by add and set to convert a raw value into the proper class given the key (if needed). This method is not used outside of this class.

# File lib/gdsii/bnf.rb, line 299
def coerce_record_value(key, value)
  if value.kind_of?(Record) or value.kind_of?(Group)
    value
  else
    Record.new(key, value)
  end
end