Class: Chem::CDX::CDXObject

Inherits:
Object
  • Object
show all
Defined in:
lib/chem/db/cdx.rb

Constant Summary collapse

Justification =
{
  -1=>"Right",
  0=>'Left',
  1=>'Center',
  2=>'Full',
  3=>'Above',
  4=>'Below',
  5=>'Auto'
}
Label_alignment =
{
  0=>"Auto",
  1=>"Left",
  2=>"Center",
  3=>"Right",
  4=>"Above",
  5=>"Below",
  6=>"Best"
}
Bs =
{
  0=>'U',
  1=>'N',
  2=>'D',
  3=>'Z'
}
As =
{
  0=>'U',
  1=>'N',
  2=>'R',
  3=>'S',
  4=>'r',
  5=>'s',
  6=>'u'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, object_id) ⇒ CDXObject

Returns a new instance of CDXObject.


1057
1058
1059
1060
1061
1062
# File 'lib/chem/db/cdx.rb', line 1057

def initialize name, object_id
  @name, @object_id = name, object_id
  @tag = Cdx_name2value[name]
  @properties = Hash.new
  @objects = Hash.new
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


1017
1018
1019
# File 'lib/chem/db/cdx.rb', line 1017

def name
  @name
end

#object_idObject

Returns the value of attribute object_id


1017
1018
1019
# File 'lib/chem/db/cdx.rb', line 1017

def object_id
  @object_id
end

#objectsObject (readonly)

Returns the value of attribute objects


1018
1019
1020
# File 'lib/chem/db/cdx.rb', line 1018

def objects
  @objects
end

#parentObject

Returns the value of attribute parent


1017
1018
1019
# File 'lib/chem/db/cdx.rb', line 1017

def parent
  @parent
end

#propertiesObject (readonly)

Returns the value of attribute properties


1018
1019
1020
# File 'lib/chem/db/cdx.rb', line 1018

def properties
  @properties
end

#tagObject

Returns the value of attribute tag


1017
1018
1019
# File 'lib/chem/db/cdx.rb', line 1017

def tag
  @tag
end

Instance Method Details

#prop(type, byte) ⇒ Object


1152
1153
1154
# File 'lib/chem/db/cdx.rb', line 1152

def prop type, byte
  return [Cdx_name2value[type], byte.length].pack('ss') + byte
end

#to_binObject


1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/chem/db/cdx.rb', line 1132

def to_bin
  str = ''
  str += [@tag].pack('s') + [@object_id].pack('V')
  @properties.each do |key, prop|
    value = Cdx_name2value[key]
    if value == nil
      puts key
      exit
    end
    str += [value].pack('s')
    prop_bin = prop.to_bin
    str += [prop_bin.length].pack('v')
    str += prop_bin
  end
  @objects.each do |key, obj|
    str += obj.to_bin 
  end
  str + [0x0000].pack('s')
end

#to_xmlObject


1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/chem/db/cdx.rb', line 1064

def to_xml
  str = "<%s\n" % [@name]
  obj = []
  str += " id=\"%d\"\n" % [@object_id] if @object_id != 0
  
  @properties.each do |k, p|
    case k
    when 'temp_LabelStyle'
      ;
    when 'temp_CaptionStyle'
      ;
    when 'temp_Text'
      obj.push(p.to_xml)
    when 'colortable'
      obj.push(p.to_xml)
    when 'fonttable'
      obj.push(p.to_xml)
    when 'LabelAlignment'
      str += " %s=\"%s\"\n" % [k, Label_alignment[p.num]]
    when 'BS'
      str += " %s=\"%s\"\n" % [k, Bs[p.num]]
    when 'AS'
      p(p)
      str += " %s=\"%s\"\n" % [k, As[p.num]]
    when 'Justification'
      str += " %s=\"%s\"\n" % [k, Justification[p.num]]
    when 'LabelJustification'
      str += " %s=\"%s\"\n" % [k, Justification[p.num]]
    when 'ChainAngle'
      str += " %s=\"%s\"\n" % [k, p.num / 65536.0]
    when 'BondSpacing'
      str += " %s=\"%s\"\n" % [k, p.num / 10.0]
    when 'LineHeight'
      if p == 1
        str += " %s=\"%s\"\n" % [k, 'auto']
      elsif p == 0
        ;#fix me
      else
        str += " %s=\"%s\"\n" % [k, p.num]
      end
    when 'LabelLineHeight'
      if p == 1
        str += " %s=\"%s\"\n" % [k, 'auto']
      elsif p == 0
        ;#fix me
      else
        str += " %s=\"%s\"\n" % [k, p]
      end
    else
      if !p.respond_to?("to_xml")
        p p
      end
      str += " %s=\"%s\"\n" % [k, p.to_xml]
    end
  end
  str += ">"
  obj.each do |o|
    str += o
  end
  #      @objects.to_a.sort{|a, b| p a[0] ; a[0]<=>b[0]}.each do |k, o|
  @objects.to_a.each do |k, o|
    str += o.to_xml
  end
  if('s' == @name)
    str += fixme#fix me
  end
  str + "</%s>" % [@name]
end