Module: OSPFv2::Common

Included in:
ExternalRoute_Base, InterfaceMtu, LSDB::Link, LSDB::LinkStateDatabase, Lsa, Lsa_Base, Metric, Neighbor, OspfPacket, RouterLink, TosMetric
Defined in:
lib/infra/ospf_common.rb,
lib/infra/ospf_common.rb

Instance Method Summary collapse

Instance Method Details

#ivar_to_klassname(ivar) ⇒ Object



200
201
202
# File 'lib/infra/ospf_common.rb', line 200

def ivar_to_klassname(ivar)
  ivar.to_s.to_camel.to_sym
end

#ivarsObject



272
273
274
# File 'lib/infra/ospf_common.rb', line 272

def ivars
  instance_variables.reject { |x| x =~ /^@_/ }.collect { |x| x[1..-1].to_sym }
end

#set(h) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/infra/ospf_common.rb', line 204

def set(h)
  # p "\n\n\n#{self.class}: IN SET(): ivars; #{ivars.inspect} - h: #{h.inspect}:"
  for key in [ivars].flatten
    # p key
    if h.has_key?(key) and ! h[key].nil?
      # puts "  h has key #{key}"
      begin
        klassname = key.to_klass
        if self.class.const_defined?(klassname)
          # puts "    class #{klassname} exists! => set @#{key.to_s} = #{klassname}.new(#{h[key]})"
          instance_variable_set("@#{key.to_s}", self.class.const_get(klassname).new(h[key]))
        elsif OSPFv2.const_defined?(klassname)
          # puts "    class OSPFv2::#{klassname} exists! => set @#{key.to_s} = #{klassname}.new(#{h[key]})"
          instance_variable_set("@#{key.to_s}", OSPFv2.const_get(klassname).new(h[key]))
        # elsif OSPFv2::LSDB::const_defined?(klassname)
        #   # puts "    class OSPFv2::LSDB::#{klassname} exists! => set @#{key.to_s} = #{klassname}.new(#{h[key]})"
        #   instance_variable_set("@#{key.to_s}", OSPFv2::LSDB::const_get(klassname).new(h[key]))
        elsif self.respond_to?(key.to_setter)
          # puts "    self respond to #{key.to_setter} => self.__send__ #{key.to_setter}, #{h[key]}"
          self.send key.to_setter, h[key]
        #elsif  has an instance variable of that name ? 
        #or create ivar on the fly ?
        else
          # puts "    just set ivar: @#{key} = #{h[key]}"
          instance_variable_set("@#{key.to_s}", h[key])
        end
      rescue ArgumentError => e
        raise
        # 
        # #FIXME: attr_writer_delegate generate a NameError (in link_state_database)
        # p "WE HAVE A NAME ERROR: #{e.inspect}"
        # # instance_variable_set("@#{key.to_s}", h[key])
      ensure
        h.delete(key)
      end
    else
      # puts "did not find #{key} in #{h.inspect}"
    end
  end
end

#to_hashObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/infra/ospf_common.rb', line 245

def to_hash
  h = {}
  for key in [ivars].flatten
    ivar = instance_variable_get("@#{key.to_s}")
    if ivar.respond_to?(:to_hash)
      # p "#{key} respond to hash"
      # p ivar
      # p "#{key} --"
      # p ivar
      h.store(key,ivar.to_hash)
    elsif ivar.is_a?(Array)
      h.store(key, ivar.collect { |x| x.to_hash })
    else
      # p "#{key} don't respond to hast"
      # p ivar
      # p ivar.class
      # p "#{key} --"
      #FIXME: ivar.to_s ? ivar.value ? ivar.hash_value ? 
      h.store(key,ivar) unless ivar.nil?
    end
  end
  h
end