Class: LibConfig::Aggregate

Inherits:
BaseSetting show all
Defined in:
ext/rlibconfig.c

Direct Known Subclasses

Array, Group, List

Instance Method Summary collapse

Methods inherited from BaseSetting

#index, #line, #name, #parent, #root?

Constructor Details

#initialize(*args) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/rlibconfig.c', line 343

static VALUE rbConfigAggregate_initialize(int argc, VALUE* argv, VALUE self)
{
  VALUE setting = Qnil;
  if(rb_obj_class(self) == cConfigGroup || rb_obj_class(self) == cConfigList) {
    rb_scan_args(argc, argv, "01", &setting);
  } else if(rb_obj_class(self) == cConfigArray) {
    VALUE type = Qnil;
    rb_scan_args(argc, argv, "02", &type, &setting);
    
    if(type != Qnil && rb_ary_includes(aConfigScalars, type) != Qtrue)
      rb_raise(rb_eTypeError, "invalid setting array type %s", rb_class2name(type));
    
    rb_iv_set(self, "@type", type);
  } else {
    rb_raise(rb_eException, "never create Config::Aggregate itself");
  }

  rb_call_super(1, &setting);

  rb_iv_set(self, "@list", rb_ary_new());
  if(rb_obj_class(self) == cConfigGroup)
    rb_iv_set(self, "@hash", rb_hash_new());
  
  if(setting != Qnil && rb_obj_class(self) == cConfigArray) {
    config_setting_t* c_setting;
    Data_Get_Struct(setting, config_setting_t, c_setting);
    if(config_setting_length(c_setting) > 0)
      rb_iv_set(self, "@type", rb_obj_class(rbConfigAggregate_get(self, INT2FIX(0))));
  }

  return self;
}

Instance Method Details

#[](index) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/rlibconfig.c', line 388

static VALUE rbConfigAggregate_get(VALUE self, VALUE index)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  VALUE rbTarget = Qnil;
  
  if(TYPE(index) == T_STRING && rb_obj_class(self) == cConfigGroup) {
    if(setting) {
      config_setting_t* target = config_setting_get_member(setting, RSTRING_PTR(index));
      if(target)
        rbTarget = rconfig_wrap_setting(target);
    } else {
      rbTarget = rb_hash_aref(rb_iv_get(self, "@hash"), index);
    }
  } else if(TYPE(index) == T_FIXNUM) {
    if(setting) {
      config_setting_t* target = config_setting_get_elem(setting, FIX2INT(index));
      if(target)
        rbTarget = rconfig_wrap_setting(target);
    } else {
      rbTarget = rb_ary_entry(rb_iv_get(self, "@list"), FIX2INT(index));
    }
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_obj_classname(index));
  }
  
  if(rbTarget == Qnil)
    if(TYPE(index) == T_STRING)
      rb_raise(eSettingNotFoundError, "setting `%s' not found", RSTRING_PTR(index));
    else
      rb_raise(eSettingNotFoundError, "setting [%d] not found", FIX2INT(index));

  return rbTarget;
}

#delete(target) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'ext/rlibconfig.c', line 482

static VALUE rbConfigAggregate_delete(VALUE self, VALUE target)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  VALUE hash = rb_iv_get(self, "@hash"), list = rb_iv_get(self, "@list");
  
  if(TYPE(target) == T_STRING && rb_obj_class(self) == cConfigGroup) {
    if(setting)
      config_setting_remove(setting, RSTRING_PTR(target));
    
    rb_ary_delete_at(list, rb_hash_aref(hash, target));
    rb_hash_delete(hash, target);
  } else if(TYPE(target) == T_FIXNUM) {
    int index = FIX2INT(target);
    if(setting)
      config_setting_remove_elem(setting, index);

    if(rb_obj_class(self) == cConfigGroup)
      rb_hash_delete(hash, rbConfigBaseSetting_name(rb_ary_entry(list, index)));
    rb_ary_delete_at(list, index);
  } else if(rb_ary_includes(aConfigSettings, rb_obj_class(target)) == Qtrue) {
    VALUE name = rbConfigBaseSetting_name(target);
    if(setting)
      config_setting_remove(setting, RSTRING_PTR(name));

    if(rb_obj_class(self) == cConfigGroup)
      rb_hash_delete(hash, name);
    rb_ary_delete(list, target);
  } else {
    if(rb_obj_class(self) == cConfigGroup)
      rb_raise(rb_eTypeError, "wrong argument type %s (expected String, Fixnum or Config::BaseSetting)", rb_obj_classname(target));
    else
      rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Config::BaseSetting)", rb_obj_classname(target));
  }
  
  return Qnil;
}

#get(index) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/rlibconfig.c', line 388

static VALUE rbConfigAggregate_get(VALUE self, VALUE index)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  VALUE rbTarget = Qnil;
  
  if(TYPE(index) == T_STRING && rb_obj_class(self) == cConfigGroup) {
    if(setting) {
      config_setting_t* target = config_setting_get_member(setting, RSTRING_PTR(index));
      if(target)
        rbTarget = rconfig_wrap_setting(target);
    } else {
      rbTarget = rb_hash_aref(rb_iv_get(self, "@hash"), index);
    }
  } else if(TYPE(index) == T_FIXNUM) {
    if(setting) {
      config_setting_t* target = config_setting_get_elem(setting, FIX2INT(index));
      if(target)
        rbTarget = rconfig_wrap_setting(target);
    } else {
      rbTarget = rb_ary_entry(rb_iv_get(self, "@list"), FIX2INT(index));
    }
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_obj_classname(index));
  }
  
  if(rbTarget == Qnil)
    if(TYPE(index) == T_STRING)
      rb_raise(eSettingNotFoundError, "setting `%s' not found", RSTRING_PTR(index));
    else
      rb_raise(eSettingNotFoundError, "setting [%d] not found", FIX2INT(index));

  return rbTarget;
}

#sizeObject



376
377
378
379
380
381
382
383
384
385
386
# File 'ext/rlibconfig.c', line 376

static VALUE rbConfigAggregate_size(VALUE self)
{
  config_setting_t* setting = NULL;
  if(rb_iv_get(self, "@setting") != Qnil)
    Data_Get_Struct(rb_iv_get(self, "@setting"), config_setting_t, setting);
  
  if(setting)
    return INT2FIX(config_setting_length(setting));
  else
    return INT2FIX(RARRAY_LEN(rb_iv_get(self, "@list")));
}