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.



791
792
793
794
795
# File 'lib/blocker.rb', line 791

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



823
824
825
826
827
828
829
# File 'lib/blocker.rb', line 823

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

Instance Method Details

#inspectObject



787
788
789
# File 'lib/blocker.rb', line 787

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

#internal_block(val) ⇒ Object



797
798
799
800
801
802
803
# File 'lib/blocker.rb', line 797

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

#internal_deblock(io) ⇒ Object



809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/blocker.rb', line 809

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



805
806
807
# File 'lib/blocker.rb', line 805

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

#valid?Boolean

Returns:

  • (Boolean)


831
832
833
# File 'lib/blocker.rb', line 831

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