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 inherited from Virtus::Attribute

DEFAULT_ACCESSOR, OPTIONS

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary

Attributes inherited from Virtus::Attribute

#coercion_method, #default, #instance_variable_name, #name, #options, #reader_visibility, #writer_visibility

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

#coerce, #define_writer_method, #get, #get!, #initialize, #inspect, #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

Class Method Details

.primitive?(value) ⇒ Boolean

Returns if the given value is either true or false

Examples:

Virtus::Attribute::Boolean.primitive?(true)    # => true
Virtus::Attribute::Boolean.primitive?(false)   # => true
Virtus::Attribute::Boolean.primitive?(1)       # => false
Virtus::Attribute::Boolean.primitive?('true')  # => false

Returns:



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

def self.primitive?(value)
  value.equal?(true) || value.equal?(false)
end

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 standard and boolean attribute reader methods

Parameters:

  • mod (Module)

Returns:

  • (self)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/virtus/attribute/boolean.rb', line 43

def define_reader_method(mod)
  super

  reader_method_name = "#{name}?"
  attribute          = self

  mod.send(:define_method,    reader_method_name) { attribute.get(self) }
  mod.send(reader_visibility, reader_method_name)

  self
end