Module: OSPFv2::Common

Included in:
ExternalRoute_Base, InterfaceMtu, LSDB::Link, LSDB::LinkStateDatabase, Lsa, 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



211
212
213
# File 'lib/infra/ospf_common.rb', line 211

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

#ivarsObject



283
284
285
# File 'lib/infra/ospf_common.rb', line 283

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

#set(h) ⇒ Object



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
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/infra/ospf_common.rb', line 215

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/infra/ospf_common.rb', line 256

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