Class: BinaryBlocker::OneOfEncoder

Inherits:
Encoder
  • Object
show all
Defined in:
lib/blocker.rb

Instance Method Summary collapse

Methods inherited from Encoder

#block, #deblock, #key_value?, #me

Constructor Details

#initialize(classes, *opts) ⇒ OneOfEncoder

Returns a new instance of OneOfEncoder.



794
795
796
797
798
# File 'lib/blocker.rb', line 794

def initialize(classes, *opts)
  @classes = classes.map { |o| o.call(*opts) }
  @obj = nil
  super(*opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



826
827
828
829
830
831
832
# File 'lib/blocker.rb', line 826

def method_missing(sym, *args)
  if @obj
    @obj.send sym, *args
  else
    super
  end
end

Instance Method Details

#inspectObject



790
791
792
# File 'lib/blocker.rb', line 790

def inspect
  "OneOf #{@classes.join(',')} -> #{@obj.class} -> #{@obj.inspect}" 
end

#internal_block(val) ⇒ Object



800
801
802
803
804
805
806
# File 'lib/blocker.rb', line 800

def internal_block(val)
  if val
    val.block
  else
    @classes.first.block
  end
end

#internal_deblock(io) ⇒ Object



812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'lib/blocker.rb', line 812

def internal_deblock(io)
  @obj = nil
  with_guarded_io_pos(io) do
    @classes.each do |obj|
      if obj.deblock(io)
        @obj = obj
        break 
      end 
      false
    end
  end       
  @obj
end

#pretty_print(obj) ⇒ Object



808
809
810
# File 'lib/blocker.rb', line 808

def pretty_print(obj)
  @obj.pretty_print(obj)
end

#valid?Boolean

Returns:

  • (Boolean)


834
835
836
# File 'lib/blocker.rb', line 834

def valid?
  @obj && @obj.valid?
end