Sha256: 47373acb26885c0b9a8848fd55c75501dcd33ec31e7a27e9226e3c59fdc2b87f

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
virtus-0.5.1 lib/virtus/attributes_accessor.rb
virtus-0.5.0 lib/virtus/attributes_accessor.rb
virtus-0.4.2 lib/virtus/attributes_accessor.rb
virtus-0.4.1 lib/virtus/attributes_accessor.rb
virtus-0.4.0 lib/virtus/attributes_accessor.rb