Class: Protobug::Field::BoolField

Inherits:
UInt64Field show all
Defined in:
lib/protobug/field.rb

Constant Summary

Constants inherited from Protobug::Field

PACKABLE_WIRE_TYPES

Instance Attribute Summary

Attributes inherited from Protobug::Field

#adder, #cardinality, #clearer, #haser, #ivar, #json_name, #name, #number, #oneof, #setter

Instance Method Summary collapse

Methods inherited from UInt64Field

#bit_length, #encoding, #signed, #wire_type

Methods inherited from IntegerField

#json_encode_one

Methods inherited from Protobug::Field

#adder_method_definition, #binary_decode, #binary_encode, #initialize, #json_decode, #json_encode, #json_key_encode, #method_definitions, #optional?, #packed?, #pretty_print, #proto3_optional?, #repeated?, #to_text

Constructor Details

This class inherits a constructor from Protobug::Field

Instance Method Details

#binary_decode_oneObject



747
748
749
# File 'lib/protobug/field.rb', line 747

def binary_decode_one(*)
  super != 0
end

#binary_encode_one(value, outbuf) ⇒ Object



751
752
753
# File 'lib/protobug/field.rb', line 751

def binary_encode_one(value, outbuf)
  super(value ? 1 : 0, outbuf)
end

#defaultObject



776
777
778
779
780
# File 'lib/protobug/field.rb', line 776

def default
  return [] if repeated?

  false
end

#json_decode_one(value, _ignore_unknown_fields, _registry) ⇒ Object



755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/protobug/field.rb', line 755

def json_decode_one(value, _ignore_unknown_fields, _registry)
  case value
  when TrueClass, FalseClass
    value
  when "true"
    true
  when "false"
    false
  when NilClass
    UNSET
  else
    raise DecodeError, "expected boolean, got #{value.inspect}"
  end
end

#validate!(value, message) ⇒ Object

Raises:



770
771
772
773
774
# File 'lib/protobug/field.rb', line 770

def validate!(value, message)
  raise InvalidValueError.new(message, self, value, "expected boolean") unless [true, false].include?(value)

  super(value ? 1 : 0, message)
end