Class: Virtus::AttributesAccessor

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

Overview

Host attribute accessor methods

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)


11
12
13
14
# File 'lib/virtus/attributes_accessor.rb', line 11

def initialize(name)
  super()
  @name = name
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)


25
26
27
28
29
# File 'lib/virtus/attributes_accessor.rb', line 25

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)


40
41
42
43
44
# File 'lib/virtus/attributes_accessor.rb', line 40

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

#inspectString

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)


63
64
65
# File 'lib/virtus/attributes_accessor.rb', line 63

def inspect
  "#{@name}::AttributesAccessor"
end