Sha256: 87937ec733081c357f71922f2c0ada3df595bb02a5deb2dc2242833f43209a1c

Contents?: true

Size: 1.2 KB

Versions: 16

Compression:

Stored size: 1.2 KB

Contents

require 'protobuf/field/varint_field'

module Protobuf
  module Field
    class BoolField < VarintField
      FALSE_ENCODE = [0].pack('C')
      FALSE_STRING = "false".freeze
      TRUE_ENCODE = [1].pack('C')
      TRUE_STRING = "true".freeze

      ##
      # Class Methods
      #

      def self.default
        false
      end

      ##
      # Public Instance Methods
      # #

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

      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

        fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'"
      end

      def decode(value)
        value == 1
      end

      def encode(value)
        value ? TRUE_ENCODE : FALSE_ENCODE
      end

      private

      ##
      # Private Instance Methods
      #

      def define_accessor(simple_field_name, _fully_qualified_field_name)
        super
        message_class.class_eval do
          alias_method "#{simple_field_name}?", simple_field_name
        end
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
protobuf-3.8.5 lib/protobuf/field/bool_field.rb
protobuf-3.8.5.pre0 lib/protobuf/field/bool_field.rb
protobuf-3.8.4 lib/protobuf/field/bool_field.rb
protobuf-3.8.3 lib/protobuf/field/bool_field.rb
protobuf-3.8.2 lib/protobuf/field/bool_field.rb
protobuf-3.8.1 lib/protobuf/field/bool_field.rb
protobuf-3.8.0 lib/protobuf/field/bool_field.rb
protobuf-3.8.0.pre1 lib/protobuf/field/bool_field.rb
protobuf-3.7.5 lib/protobuf/field/bool_field.rb
protobuf-3.7.4 lib/protobuf/field/bool_field.rb
protobuf-3.7.3 lib/protobuf/field/bool_field.rb
protobuf-3.7.2 lib/protobuf/field/bool_field.rb
protobuf-3.7.2.pre1 lib/protobuf/field/bool_field.rb
protobuf-3.7.1 lib/protobuf/field/bool_field.rb
protobuf-3.7.0 lib/protobuf/field/bool_field.rb
protobuf-3.7.0.pre3 lib/protobuf/field/bool_field.rb