Sha256: 378dd6b8ee5875cbc49f047cc989491f453eaed943622214a66c680094324ae1

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

motion_require 'accessor_method'

module Virtus
  class Attribute

    # Writer method object
    #
    # @api public
    class Writer < AccessorMethod

      # Return primitive
      #
      # @return [Class]
      #
      # @api private
      attr_reader :primitive

      # Return default value instance
      #
      # @return [DefaultValue]
      #
      # @api private
      attr_reader :default_value

      # Initialize a writer instance
      #
      # @param [#to_sym] name
      #
      # @param [Hash] options
      #
      # @return [undefined]
      #
      # @api private
      def initialize(name, options = {})
        super
        @name          = "#{name}=".to_sym
        @primitive     = options.fetch(:primitive, ::Object)
        @default_value = DefaultValue.build(options[:default])
      end

      # Sets instance variable of the attribute
      #
      # @example
      #   attribute.set!(instance, value)  # => value
      #
      # @return [self]
      #
      # @api public
      def call(instance, value)
        instance.instance_variable_set(instance_variable_name, value)
      end

      # @api private
      def set_default_value(instance, attribute)
        call(instance, default_value.call(instance, attribute))
      end

      # Creates an attribute writer method
      #
      # @param [Module] mod
      #
      # @return [self]
      #
      # @api private
      def define_method(accessor, mod)
        mod.define_writer_method(accessor, name, visibility)
        self
      end

    end # class Writer

  end # class Attribute
end # module Virtus

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motion_virtus-1.0.0.beta0.1 lib/project/attribute/writer.rb
motion_virtus-1.0.0.beta0 lib/project/attribute/writer.rb