Class: Protobuf::Field::BoolField

Inherits:
VarintField show all
Defined in:
lib/protobuf/field/bool_field.rb

Constant Summary collapse

FALSE_ENCODE =
[0].pack('C')
FALSE_STRING =
"false".freeze
TRUE_ENCODE =
[1].pack('C')
TRUE_STRING =
"true".freeze

Constants inherited from VarintField

VarintField::CACHE_LIMIT, VarintField::INT32_MAX, VarintField::INT32_MIN, VarintField::INT64_MAX, VarintField::INT64_MIN, VarintField::UINT32_MAX, VarintField::UINT64_MAX

Constants inherited from BaseField

Protobuf::Field::BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

#fully_qualified_name, #message_class, #name, #options, #rule, #tag, #type_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VarintField

cached_varint, encode, #wire_type

Methods inherited from BaseField

#default, #default_value, #deprecated?, #enum?, #extension?, #fully_qualified_name_only!, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #tag_encoded, #to_s, #wire_type

Methods included from Logging

initialize_logger, #log_exception, #log_signature, #logger, #sign_message

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Class Method Details

.defaultObject

Class Methods



15
16
17
# File 'lib/protobuf/field/bool_field.rb', line 15

def self.default
  false
end

Instance Method Details

#acceptable?(val) ⇒ Boolean

Public Instance Methods

Returns:

  • (Boolean)


23
24
25
# File 'lib/protobuf/field/bool_field.rb', line 23

def acceptable?(val)
  val == true || val == false || val == TRUE_STRING || val == FALSE_STRING
end

#coerce!(val) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/protobuf/field/bool_field.rb', line 27

def coerce!(val)
  return true if val == true
  return false if val == false
  return true if val == TRUE_STRING
  return false if val == FALSE_STRING

  val
end

#decode(value) ⇒ Object



36
37
38
# File 'lib/protobuf/field/bool_field.rb', line 36

def decode(value)
  value == 1
end

#encode(value) ⇒ Object



40
41
42
# File 'lib/protobuf/field/bool_field.rb', line 40

def encode(value)
  value ? TRUE_ENCODE : FALSE_ENCODE
end