Class: Virtus::Attribute::Boolean

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

Overview

Bolean 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

#coercion_method, #default, #name, #options

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

build, #coerce, #define_accessor_methods, #define_writer_method, determine_type, #get, #get!, #initialize, #inspect, merge_options, #public_reader?, #public_writer?, #set, #set!

Methods included from DescendantsTracker

#add_descendant, #descendants

Methods included from TypeLookup

#determine_type, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Constructor Details

This class inherits a constructor from Virtus::Attribute

Instance Method Details

#define_reader_method(mod) ⇒ self

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:

  • (self)


44
45
46
47
48
# File 'lib/virtus/attribute/boolean.rb', line 44

def define_reader_method(mod)
  super
  mod.define_reader_method(self, "#{name}?", @reader_visibility)
  self
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:



33
34
35
# File 'lib/virtus/attribute/boolean.rb', line 33

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