Sha256: 663c435ce66148d340159b95d17337f8f0fc8543542be08418e247b092691dcf

Contents?: true

Size: 892 Bytes

Versions: 12

Compression:

Stored size: 892 Bytes

Contents

require 'protobuf/field/varint_field'

module Protobuf
  module Field
    class BoolField < VarintField

      ##
      # Class Methods
      #

      def self.default
        false
      end

      ##
      # Public Instance Methods
      # #

      def acceptable?(val)
        [true, false].include?(val) || %w(true false).include?(val)
      end

      def coerce!(val)
        if val == 'true'
          true
        elsif val == 'false'
          false
        else
          val
        end
      end

      def decode(value)
        value == 1
      end

      def encode(value)
        [value ? 1 : 0].pack('C')
      end

      private

      ##
      # Private Instance Methods
      #

      def define_getter
        super

        field = self

        message_class.class_eval do
          alias_method "#{field.getter}?", field.getter
        end
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
protobuf-core-3.5.0 lib/protobuf/field/bool_field.rb
protobuf-3.6.2 lib/protobuf/field/bool_field.rb
protobuf-3.6.1 lib/protobuf/field/bool_field.rb
protobuf-3.6.0 lib/protobuf/field/bool_field.rb
protobuf-3.5.5 lib/protobuf/field/bool_field.rb
protobuf-3.5.4 lib/protobuf/field/bool_field.rb
protobuf-3.5.3 lib/protobuf/field/bool_field.rb
protobuf-3.5.2 lib/protobuf/field/bool_field.rb
protobuf-3.5.1 lib/protobuf/field/bool_field.rb
prepor-protobuf-3.5.1 lib/protobuf/field/bool_field.rb
prepor-protobuf-3.5.0 lib/protobuf/field/bool_field.rb
protobuf-3.5.0 lib/protobuf/field/bool_field.rb