Sha256: 6ec739101dc17ad21620aed3464b0f0b1695378bfb6e2943e2e8029cbb6ff338

Contents?: true

Size: 870 Bytes

Versions: 1

Compression:

Stored size: 870 Bytes

Contents

# frozen_string_literal: true

module ActiveFields
  module Field
    class Boolean < ActiveFields.config.field_base_class
      store_accessor :options, :required, :nullable

      # attribute :required, :boolean, default: false
      # attribute :nullable, :boolean, default: false

      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

1 entries across 1 versions & 1 rubygems

Version Path
active_fields-0.2.0 app/models/active_fields/field/boolean.rb