Sha256: 71fd31595b0fe0a6fe865a1a807e78054754567fbbe6dd2f8c2974733608d350

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8

class AbstractMapper

  # Defines attributes along with the DSL for setting them
  #
  module Attributes

    # @private
    def self.included(klass)
      klass.__send__ :extend, DSL
    end

    # @!attribute [r] attributes
    #
    # @return [Hash] The initialized attributes
    #
    attr_reader :attributes

    # Initializes the instance with the hash of attributes
    #
    # @param [Hash] attributes
    #
    def initialize(attributes)
      @attributes = Functions[:restrict, self.class.attributes][attributes]
    end

    # Attributes DSL
    #
    module DSL

      # Makes a attributes inheritable
      #
      # @private
      #
      def inherited(klass)
        attributes.each { |key, value| klass.attribute key, default: value }
      end

      # Default attributes for the node
      #
      # @return [Hash]
      #
      def attributes
        @attributes ||= {}
      end

      # Declares the attribute
      #
      # @param [#to_sym] name
      # @param [Hash] options
      # @option options [Object] :default
      #
      # @return [undefined]
      #
      def attribute(name, options = {})
        attributes[name.to_sym] = options[:default]
        define_method(name) { attributes[name.to_sym] }
      end

    end # module DSL

  end # module Attributes

end # class AbstractMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
abstract_mapper-0.0.2 lib/abstract_mapper/attributes.rb