module Virtus # Host attribute accessor methods class AttributesAccessor < Module # Initialize a module for hosting Attribute access methods # # @param [Symbol, String] name # # @api private def initialize(name) super() @name = name end # Defines an attribute reader method # # @param [Attribute] attribute # @param [Symbol] method_name # @param [Symbol] visibility # # @return [self] # # @api private def define_reader_method(attribute, method_name, visibility) define_method(method_name) { attribute.get(self) } send(visibility, method_name) self end # Defines an attribute writer method # # @param [Attribute] attribute # @param [Symbol] method_name # @param [Symbol] visibility # # @return [self] # # @api private def define_writer_method(attribute, method_name, visibility) define_method(method_name) { |value| attribute.set(self, value) } send(visibility, method_name) self end # The inspect value of this Module # # This provides meaningful output when inspecting the ancestors # of a class/module that includes this module # # @example # # class ClassWithAttributes # include Virtus # end # # mod = ClassWithAttributes.send(:virtus_setup_attributes_accessor_module) # mod.inspect # # @return [String] # # @api public def inspect "#{@name}::AttributesAccessor" end end end