All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gems.virtus-1.0.5.lib.virtus.attribute.accessor.rb Maven / Gradle / Ivy

The newest version!
module Virtus
  class Attribute

    # Accessor extension provides methods to read and write attributes
    #
    # @example
    #
    #   attribute = Virtus::Attribute.build(String, :name => :email)
    #   model     = Class.new { attr_reader :email }
    #   object    = model.new
    #
    #   attribute.set(object, '[email protected]')
    #   attribute.get(object) # => '[email protected]'
    #
    module Accessor

      # Return name of this accessor attribute
      #
      # @return [Symbol]
      #
      # @api public
      attr_reader :name

      # Return instance_variable_name used by this accessor
      #
      # @api private
      attr_reader :instance_variable_name

      # @api private
      def self.extended(descendant)
        super
        name = descendant.options.fetch(:name).to_sym
        descendant.instance_variable_set('@name', name)
        descendant.instance_variable_set('@instance_variable_name', "@#{name}")
      end

      # Return if attribute value is defined
      #
      # @param [Object] instance
      #
      # @return [Boolean]
      #
      # @api public
      def defined?(instance)
        instance.instance_variable_defined?(instance_variable_name)
      end

      # Return value of the attribute
      #
      # @param [Object] instance
      #
      # @return [Object]
      #
      # @api public
      def get(instance)
        instance.instance_variable_get(instance_variable_name)
      end

      # Set value of the attribute
      #
      # @param [Object] instance
      # @param [Object] value
      #
      # @return [Object] value that was set
      #
      # @api public
      def set(instance, value)
        instance.instance_variable_set(instance_variable_name, value)
      end

      # Set default value
      #
      # @param [Object] instance
      #
      # @return [Object] value that was set
      #
      # @api public
      def set_default_value(instance)
        set(instance, default_value.call(instance, self))
      end

      # Returns a Boolean indicating whether the reader method is public
      #
      # @return [Boolean]
      #
      # @api private
      def public_reader?
        options[:reader] == :public
      end

      # Returns a Boolean indicating whether the writer method is public
      #
      # @return [Boolean]
      #
      # @api private
      def public_writer?
        options[:writer] == :public
      end

    end # Accessor

  end # Attribute
end # Virtus




© 2015 - 2024 Weber Informatics LLC | Privacy Policy