Class: StructWithType

Inherits:
Struct
  • Object
show all
Defined in:
lib/el4r/el4r-sub.rb

Overview

StructWithType

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ StructWithType

Returns a new instance of StructWithType.



662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/el4r/el4r-sub.rb', line 662

def initialize(*args)
  args.each_with_index do |x, i|
    args[i] = __convert__(@@__keys__[i], x)
  end

  class << self
    @@__keys__.each do |k|
      define_method("#{k}="){|v| self[k]=v}
    end
  end

  super *args
end

Class Method Details

.new(*args) ⇒ Object

TODO: document



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/el4r/el4r-sub.rb', line 632

def self.new(*args)

  keys = []
  types = []
  args.each_with_index do |x,i|
    if i%2 == 0
      keys << x
    else
      types << x
    end
  end

  unless keys.length > 0 &&
      types.length > 0   &&
      keys.length == types.length
    raise ArgumentError, "#{self}: args.length must be even"
  end
      

  klass = super(*keys)

  klass.instance_eval do
    @@__type_dic__ = {}
    @@__keys__ = keys
    keys.each_with_index{|k,i| @@__type_dic__[k] = types[i]}
  end

  klass
end

Instance Method Details

#[]=(k, v) ⇒ Object



681
682
683
684
# File 'lib/el4r/el4r-sub.rb', line 681

def []=(k,v)
  v = __convert__(k,v)
  super(k,v)
end