Sha256: d627e1fca423b6c580f66cbf87f4807b80e54f136fae54770c1b42791043602b

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

module Virtus

  # Virtus module that can define attributes for later inclusion
  #
  module ModuleExtensions
    include ConstMissingExtensions

    # Define an attribute in the module
    #
    # @see Virtus::Extensions#attribute
    #
    # @return [self]
    #
    # @api private
    def attribute(*args)
      attribute_definitions << args
      self
    end

  private

    # Extend an object with Virtus methods and define attributes
    #
    # @param [Object] object
    #
    # @return [undefined]
    #
    # @api private
    def extended(object)
      super
      object.extend(Virtus)
      define_attributes(object)
      object.set_default_attributes
    end

    # Extend a class with Virtus methods and define attributes
    #
    # @param [Object] object
    #
    # @return [undefined]
    #
    # @api private
    def included(object)
      super
      object.module_eval { include Virtus }
      define_attributes(object)
    end

    # Return attribute definitions
    #
    # @return [Array<Hash>]
    #
    # @api private
    def attribute_definitions
      @_attribute_definitions ||= []
    end

    # Define attributes on a class or instance
    #
    # @param [Object,Class] object
    #
    # @return [undefined]
    #
    # @api private
    def define_attributes(object)
      attribute_definitions.each do |attribute_args|
        object.attribute(*attribute_args)
      end
    end

  end # module ModuleExtensions
end # module Virtus

Version data entries

2 entries across 2 versions & 1 rubygems

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