Sha256: 7e788968c32c1f62b6489a6bfaaa8caed5217e4e2dcef6ff5819331513c5a8ba

Contents?: true

Size: 1.31 KB

Versions: 29

Compression:

Stored size: 1.31 KB

Contents

require 'protobuf/field/varint_field'

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

      ##
      # Class Methods
      #

      def self.default
        false
      end

      ##
      # Public Instance Methods
      # #

      def acceptable?(val)
        ACCEPTABLES.include?(val)
      end

      def coerce!(val)
        if TRUE_VALUES.include?(val)
          true
        elsif FALSE_VALUES.include?(val)
          false
        else
          fail TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'"
        end
      end

      def decode(value)
        value == ONE
      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

29 entries across 29 versions & 3 rubygems

Version Path
protobuf-3.10.9 lib/protobuf/field/bool_field.rb
protobuf-3.10.8 lib/protobuf/field/bool_field.rb
contrast-agent-6.7.0 lib/protobuf/field/bool_field.rb
protobuf-3.10.7 lib/protobuf/field/bool_field.rb
contrast-agent-6.6.5 lib/protobuf/field/bool_field.rb
contrast-agent-6.6.4 lib/protobuf/field/bool_field.rb
contrast-agent-6.6.3 lib/protobuf/field/bool_field.rb
contrast-agent-6.6.2 lib/protobuf/field/bool_field.rb
contrast-agent-6.6.1 lib/protobuf/field/bool_field.rb
contrast-agent-6.6.0 lib/protobuf/field/bool_field.rb
contrast-agent-6.5.1 lib/protobuf/field/bool_field.rb
contrast-agent-6.5.0 lib/protobuf/field/bool_field.rb
contrast-agent-6.4.0 lib/protobuf/field/bool_field.rb
protobuf-3.10.6 lib/protobuf/field/bool_field.rb
protobuf-3.10.5 lib/protobuf/field/bool_field.rb
protobuf-3.10.4 lib/protobuf/field/bool_field.rb
protobuf-cucumber-3.10.8 lib/protobuf/field/bool_field.rb
protobuf-cucumber-3.10.7 lib/protobuf/field/bool_field.rb
protobuf-cucumber-3.10.6 lib/protobuf/field/bool_field.rb
protobuf-cucumber-3.10.5 lib/protobuf/field/bool_field.rb