Class: Virtus::Attribute::Boolean

Inherits:
Virtus::Attribute show all
Defined in:
lib/virtus/attribute/boolean.rb

Overview

Boolean attribute allows true or false values to be set Additionally it adds boolean reader method, like “admin?”

Examples:

class Post
  include Virtus

  attribute :published, Boolean
end

post = Post.new(:published => false)
post.published?  # => false

Constant Summary

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary

Attributes inherited from Virtus::Attribute

#coercer, #default_value, #options, #primitive, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

build, build_coercer, coerce, #coerce, #coercible?, #finalize, #finalized?, #initialize, #lazy?, merge_options!, #rename, #required?, #strict?

Methods included from TypeLookup

#determine_type, extended, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Constructor Details

This class inherits a constructor from Virtus::Attribute

Class Method Details

.build_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/virtus/attribute/boolean.rb', line 21

def self.build_type(*)
  Axiom::Types::Boolean
end

Instance Method Details

#define_accessor_methods(attribute_set) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an attribute reader method as a query

Parameters:

  • mod (Module)

Returns:

  • (undefined)


48
49
50
51
# File 'lib/virtus/attribute/boolean.rb', line 48

def define_accessor_methods(attribute_set)
  super
  attribute_set.define_reader_method(self, "#{name}?", options[:reader])
end

#value_coerced?(value) ⇒ Boolean

Returns if the given value is either true or false

Examples:

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

Returns:



37
38
39
# File 'lib/virtus/attribute/boolean.rb', line 37

def value_coerced?(value)
  value.equal?(true) || value.equal?(false)
end