Sha256: 8b7e920cb90a0dedbf91bd3e72da4778f615d88048883bfed50925c5b7e915ac

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module Virtus

  # Host attribute accessor methods
  class AttributesAccessor < Module

    # 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
    attr_reader :inspect

    # Initialize a module for hosting Attribute access methods
    #
    # @param [Symbol, String] name
    #
    # @api private
    def initialize(name)
      super()
      @inspect = "#{name}::AttributesAccessor"
    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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
virtus-0.3.0 lib/virtus/attributes_accessor.rb
virtus-0.2.0 lib/virtus/attributes_accessor.rb
virtus-0.1.0 lib/virtus/attributes_accessor.rb