Class: Virtus::AttributesAccessor

Inherits:
Module
  • Object
show all
Defined in:
lib/virtus/attributes_accessor.rb

Overview

Host attribute accessor methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ AttributesAccessor

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.

Initialize a module for hosting Attribute access methods

Parameters:

  • name (Symbol, String)


30
31
32
33
# File 'lib/virtus/attributes_accessor.rb', line 30

def initialize(name)
  super()
  @inspect = "#{name}::AttributesAccessor"
end

Instance Attribute Details

#inspectString (readonly)

The inspect value of this Module

This provides meaningful output when inspecting the ancestors of a class/module that includes this module

Examples:


class ClassWithAttributes
  include Virtus
end

mod = ClassWithAttributes.send(:virtus_setup_attributes_accessor_module)
mod.inspect

Returns:

  • (String)


23
24
25
# File 'lib/virtus/attributes_accessor.rb', line 23

def inspect
  @inspect
end

Instance Method Details

#define_reader_method(attribute, method_name, visibility) ⇒ 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.

Defines an attribute reader method

Parameters:

  • attribute (Attribute)
  • method_name (Symbol)
  • visibility (Symbol)

Returns:

  • (self)


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

def define_reader_method(attribute, method_name, visibility)
  define_method(method_name) { attribute.get(self) }
  send(visibility, method_name)
  self
end

#define_writer_method(attribute, method_name, visibility) ⇒ 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.

Defines an attribute writer method

Parameters:

  • attribute (Attribute)
  • method_name (Symbol)
  • visibility (Symbol)

Returns:

  • (self)


59
60
61
62
63
# File 'lib/virtus/attributes_accessor.rb', line 59

def define_writer_method(attribute, method_name, visibility)
  define_method(method_name) { |value| attribute.set(self, value) }
  send(visibility, method_name)
  self
end