Sha256: 8cb5eed5fa508da228ad1fd4fd25278f6e65218ae72673cb8405da7783f6b8fc

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 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 < Object
      primitive       TrueClass
      coercion_method :to_boolean

      # 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) or value.equal?(false)
      end

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

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

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
virtus-0.5.5 lib/virtus/attribute/boolean.rb
virtus-0.5.4 lib/virtus/attribute/boolean.rb
virtus-0.5.3 lib/virtus/attribute/boolean.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/virtus-0.5.2/lib/virtus/attribute/boolean.rb
virtus-0.5.2 lib/virtus/attribute/boolean.rb