Sha256: e42330c81862a6622c65801610ac5392e79be503b4408373b9b6d42b737e10cf

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 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

        val
      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

2 entries across 2 versions & 1 rubygems

Version Path
protobuffy-4.0.1 lib/protobuf/field/bool_field.rb
protobuffy-4.0.0 lib/protobuf/field/bool_field.rb