Sha256: 9226b9f2b10b7397d97a811994bd42c2877fcfe50dc53bcb784ab66da8d05c75

Contents?: true

Size: 1.37 KB

Versions: 27

Compression:

Stored size: 1.37 KB

Contents

module Virtus
  class Attribute

    # Boolean attribute allows true or false values to be set
    # Additionally it adds boolean reader method, like "admin?"
    #
    # @example
    #   class Post
    #     include Virtus
    #
    #     attribute :published, Boolean
    #   end
    #
    #   post = Post.new(:published => false)
    #   post.published?  # => false
    #
    class Boolean < Attribute
      primitive TrueClass

      # @api private
      def self.build_type(*)
        Axiom::Types::Boolean
      end

      # Returns if the given value is either true or false
      #
      # @example
      #   boolean = Virtus::Attribute::Boolean.new(:bool)
      #   boolean.value_coerced?(true)    # => true
      #   boolean.value_coerced?(false)   # => true
      #   boolean.value_coerced?(1)       # => false
      #   boolean.value_coerced?('true')  # => false
      #
      # @return [Boolean]
      #
      # @api public
      def value_coerced?(value)
        value.equal?(true) || value.equal?(false)
      end

      # Creates an attribute reader method as a query
      #
      # @param [Module] mod
      #
      # @return [undefined]
      #
      # @api private
      def define_accessor_methods(attribute_set)
        super
        attribute_set.define_reader_method(self, "#{name}?", options[:reader])
      end

    end # class Boolean
  end # class Attribute
end # module Virtus

Version data entries

27 entries across 25 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/lib/virtus/attribute/boolean.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/lib/virtus/attribute/boolean.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/lib/virtus/attribute/boolean.rb
virtus2-2.1.0 lib/virtus/attribute/boolean.rb
virtus2-2.0.2 lib/virtus/attribute/boolean.rb
virtus2-2.0.1 lib/virtus/attribute/boolean.rb
virtus-2.0.0 lib/virtus/attribute/boolean.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/virtus-1.0.5/lib/virtus/attribute/boolean.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/virtus-1.0.5/lib/virtus/attribute/boolean.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/lib/virtus/attribute/boolean.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/lib/virtus/attribute/boolean.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/lib/virtus/attribute/boolean.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/lib/virtus/attribute/boolean.rb
virtus-1.0.5 lib/virtus/attribute/boolean.rb
virtus-1.0.4 lib/virtus/attribute/boolean.rb
virtus-1.0.3 lib/virtus/attribute/boolean.rb
virtus-1.0.2 lib/virtus/attribute/boolean.rb
virtus-1.0.1 lib/virtus/attribute/boolean.rb
virtus-1.0.0 lib/virtus/attribute/boolean.rb
virtus-1.0.0.rc2 lib/virtus/attribute/boolean.rb