view · edit · sidebar · attach · print · history

20120620-debug-oddb-yaml

<< | Index | >>


Summary

  • Debuged yaml exporting of exportd
    • I tried hacking ODBA via Psych. But it dosen't work, expectedly.
    • found solution with Syck.

Index


Debug oddb_yaml export

Some object is returned as ODDA::Stub class directly.
Then this object dosen't have instance variables.

  module OddbYaml
    ...
    def encode_with coder
      self::class::EXPORT_PROPERTIES.each do |a| 
        p a 
        value = self.instance_variable_get(a)
        p value
        if value.is_a? String
          if value.encoding.to_s.downcase != 'utf-8'
            value.force_encoding('utf-8')
          end 
        end 
        coder[a[1..-1]] = value
      end 
      coder.tag = self.to_yaml_type
    end 
same odba_id
...
"@registrations"
#<ODBA::Stub:53792880#29751 @odba_class= @odba_container=53794600#508>
...
via bin/admin
ch.oddb> ODBA.cache.fetch(508).registrations
-> [#<ODBA::Stub:68693340#24604 @odba_class=ODDB::Registration @odba_container=68749820#29751>]
ch.oddb> ODBA.cache.fetch(508).instance_variable_get("@registrations")
-> [#<ODBA::Stub:68693340#24604 @odba_class=ODDB::Registration @odba_container=68749820#29751>]

We have to handle Array that is owned directly by ODDB Object.

--- !oddb.org,2003/ODDB::Company
oid: 123 
ean13: '7601001307001'
name: athenstaedt AG
business_area: ba_pharma
generic_type: 
registrations: array
url: 
email: 
addresses: array
contact: 
contact_email: 

YAML dump fails sometimes at dump of following 2 object. (It seems that it is caused, if these object has umlaute characters.)

  • Address2 (Company has in Array)
  • Registrations (sometimes, it comes as ODBA::Stub object)

hack ODBA

next, I tried to overwrite ODBA::Stub class.

module ODBA
  class Stub
    undef :to_yaml_properties
    undef :to_yaml_type
    undef :yaml_initialize
    def to_yaml(*args)
      odba_instance.to_yaml(*args)
    end 
    def encode_with coder
      self::class::EXPORT_PROPERTIES.each do |a| 
        value = self.odba_instance.instance_variable_get(a)
        if value.is_a? String
          if value.encoding.to_s.downcase != 'utf-8'
            value.force_encoding('utf-8')
          end 
        end 
        if value.class != Array and 
           value.class != Hash
          coder[a[1..-1]] = value
        else
          p value.keys
          p value
          p value.class
        end 
      end 
      #coder.tag = self.to_yaml_type
      coder.tag = nil 
    end 
  end 
end

I could export some values of missing ODDB::Registration object into yaml.
But Array and Hash objects are not. these are used as ODBA_SELIALIZABLE.

ODBA_SERIALIZABLE
$ grep -r ODBA_SERIALIZABLE src/model
...
src/model/package.rb:    ODBA_SERIALIZABLE = [ '@prices', '@ancestors', '@swissmedic_source' ]
src/model/company.rb:           ODBA_SERIALIZABLE = ['@addresses', '@invoice_dates', '@disabled_invoices',
src/model/indication.rb:                ODBA_SERIALIZABLE = [ '@descriptions', '@synonyms' ]
src/model/galenicform.rb:               ODBA_SERIALIZABLE = [ '@descriptions', '@synonyms' ]
...

Methods of Array and Hash are defined by ODBA.
It seems that this Yaml exporting fails by this re-definenig.

ODBA is created to suite syck engine (old YAML gem).

I try to use again syck engin. (back to syck engine)


try-yaml-syck-engine

I found deprecated unescape method.

/path/to/ruby/1.9.1/syck/encoding.rb
  def self.unescape( value )
              warn "#{caller[0]}: YAML.unescape is deprecated" if $VERBOSE
    value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { 
      if $3
        ["#$3".hex ].pack('U*')
      elsif $2
        [$2].pack( "H2" )
      else
        UNESCAPES[$1]
      end 
    }   
  end

then, tried to unescape after dumped via syck.

I could get correct "Umlaute" characters.
It works, expectedly !

require 'yaml'
require 'syck'
require 'syck/encoding'
YAML::ENGINE.yamler = "syck"

...
fh.puts Syck.unescape(YAML.dump(ODBA.cache.fetch(odba_id, nil)))
# =>
registrations: 
- !oddb.org,2003/ODDB::Registration 
  iksnr: "21109"
  registration_date: 1955-01-21
  revision_date: 
  expiration_date: 2015-01-06
  inactive_date: 
  sequences: 
    "01": !oddb.org,2003/ODDB::Sequence 
      seqnr: "01"
      name_base: Lecicarbon
      name_descr: "Suppositorien für Erwachsene"
      atc_class: &id003 !oddb.org,2003/ODDB::AtcClass 
        code: A06AB
        descriptions: !oddb.org,2003/ODDB::SimpleLanguage::Descriptions {}
view · edit · sidebar · attach · print · history
Page last modified on June 20, 2012, at 12:12 PM