Module: OSPFv2::Common

Instance Method Summary collapse

Instance Method Details

#ivar_to_klassname(ivar) ⇒ Object



189
190
191
# File 'lib/infra/ospf_common.rb', line 189

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

#ivarsObject



261
262
263
# File 'lib/infra/ospf_common.rb', line 261

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

#set(h) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
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
# File 'lib/infra/ospf_common.rb', line 193

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/infra/ospf_common.rb', line 234

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.respond_to?(:to_hash) ? x.to_hash : x })
    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