Sha256: 074ec6c1d35c403872f683a85987e075f9e922c0e5fd7653be4d5882de7a9d3f

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module ActiveFields
  module Field
    class Boolean < ActiveFields.config.field_base_class
      acts_as_active_field(
        validator: {
          class_name: "ActiveFields::Validators::BooleanValidator",
          options: -> { { required: required?, nullable: nullable? } },
        },
        caster: {
          class_name: "ActiveFields::Casters::BooleanCaster",
        },
      )

      store_accessor :options, :required, :nullable

      validates :required, exclusion: [nil]
      validates :nullable, exclusion: [nil]

      %i[required nullable].each do |column|
        define_method(column) do
          Casters::BooleanCaster.new.deserialize(super())
        end

        define_method(:"#{column}?") do
          !!public_send(column)
        end

        define_method(:"#{column}=") do |other|
          super(Casters::BooleanCaster.new.serialize(other))
        end
      end

      private

      def set_defaults
        self.required ||= false
        self.nullable ||= false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_fields-1.1.0 app/models/active_fields/field/boolean.rb
active_fields-1.0.0 app/models/active_fields/field/boolean.rb